refactor: plotter bitmap

This commit is contained in:
Ilia Denisov
2025-09-11 01:34:55 +03:00
parent 311320d30f
commit 371453fac5
13 changed files with 222 additions and 301 deletions
+56 -4
View File
@@ -1,8 +1,10 @@
package bitmap
import (
"errors"
"fmt"
"math"
"math/bits"
)
const intSize = 32
@@ -35,14 +37,61 @@ func (p bitmap) isSet(number uint32) bool {
return p.bitVector[number/intSize]&(0b1<<(number%intSize)) > 0
}
func (p bitmap) Circle(x, y int, r float64) {
func (p bitmap) FreeCount() (result int) {
result = int(p.width) * int(p.height)
for i := range p.bitVector {
result -= bits.OnesCount32(p.bitVector[i])
}
return
}
func (p bitmap) GetFreeN(number int) (int, int, error) {
if p.FreeCount() == 0 {
return 0, 0, errors.New("no free pixels left")
}
fc := 0
n := 0
for ; n < int(p.width)*int(p.height); n++ {
if p.isSet(uint32(n)) {
continue
}
if fc == number {
y := n / int(p.height)
x := n - int(p.height)*y
return x, y, nil
}
fc++
}
return 0, 0, fmt.Errorf("get free pixel: no such number=%d, max=%d", number, n)
}
func (p bitmap) SetFreeN(number int) error {
if p.FreeCount() == 0 {
return errors.New("no free pixels left")
}
fc := 0
n := 0
for ; n < int(p.width)*int(p.height); n++ {
if p.isSet(uint32(n)) {
continue
}
if fc == number {
p.set(uint32(n))
return nil
}
fc++
}
return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n)
}
func (p bitmap) Circle(x, y int, r float64, fill bool) {
plotX := 0
plotY := int(math.Ceil(r))
delta := 3 - 2*plotY
lastY := plotY
for plotX <= plotY {
p.octant(x, y, plotX, plotY)
if plotY < lastY {
if fill && plotY < lastY {
for lineX := 0; lineX < plotX; lineX++ {
p.octant(x, y, lineX, plotY)
}
@@ -56,6 +105,9 @@ func (p bitmap) Circle(x, y int, r float64) {
}
plotX += 1
}
if !fill {
return
}
for fillX := 0; fillX < plotX; fillX++ {
for fillY := 0; fillY <= fillX; fillY++ {
p.octant(x, y, fillX, fillY)
@@ -63,7 +115,7 @@ func (p bitmap) Circle(x, y int, r float64) {
}
}
func (p bitmap) CircleAdjacent(x, y int, r float64) {
func (p bitmap) circleAdjacent(x, y int, r float64) {
plotX := 0
plotY := int(math.Ceil(r))
delta := 1 - 2*plotY
@@ -110,7 +162,7 @@ func (p bitmap) String() string {
cnt := 0
for i := 0; i < len(p.bitVector); i++ {
for bit := 0; bit < intSize && cnt < int(p.width*p.height); bit++ {
result += fmt.Sprintf("%s", px[p.bitVector[i]&(0b1<<bit) > 0])
result += px[p.bitVector[i]&(0b1<<bit) > 0]
cnt++
if cnt%int(p.width) == 0 {
result += "\n"