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
@@ -0,0 +1,6 @@
░░██████░░░░
██░░░░░░██░░
██░░░░░░██░░
██░░░░░░██░░
░░██████░░░░
░░░░░░░░░░░░
@@ -0,0 +1,6 @@
████░░░░░░██
██████░░████
██████░░████
██████░░████
████░░░░░░██
░░░░░░░░░░░░
+12
View File
@@ -0,0 +1,12 @@
░░░░░░██████████░░░░░░░░
░░░░██████████████░░░░░░
░░██████████████████░░░░
██████████████████████░░
██████████████████████░░
██████████████████████░░
██████████████████████░░
██████████████████████░░
░░██████████████████░░░░
░░░░██████████████░░░░░░
░░░░░░██████████░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░
+12
View File
@@ -0,0 +1,12 @@
██░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░██░░
░░██░░░░░░░░░░░░░░██░░░░
░░░░██░░░░░░░░░░██░░░░░░
░░░░░░██████████░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░██████████░░░░░░░░
░░░░██░░░░░░░░░░██░░░░░░
░░██░░░░░░░░░░░░░░██░░░░
██░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░██░░
+15
View File
@@ -0,0 +1,15 @@
░░░░░░░░░░██████████░░░░░░░░░░
░░░░░░████░░░░░░░░░░████░░░░░░
░░░░██░░░░░░░░░░░░░░░░░░██░░░░
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
░░░░██░░░░░░░░░░░░░░░░░░██░░░░
░░░░░░████░░░░░░░░░░████░░░░░░
░░░░░░░░░░██████████░░░░░░░░░░
+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"
+85 -20
View File
@@ -7,7 +7,7 @@ import (
"strings"
"testing"
"bitmap"
"github.com/iliadenisov/galaxy/pkg/bitmap"
)
func TestBitVectorSize(t *testing.T) {
@@ -126,6 +126,76 @@ func TestSetPixel(t *testing.T) {
}
}
func TestFreeCount(t *testing.T) {
bm := bitmap.NewBitmap(10, 10)
type testCase struct {
x, y, expect int
}
for _, tc := range []testCase{
{0, 0, 99},
{5, 5, 98},
{9, 9, 97},
{10, 10, 97},
{15, 6, 96},
{9, 9, 96},
{3, 8, 95},
} {
t.Run(fmt.Sprintf("x=%d,y=%d", tc.x, tc.y), func(t *testing.T) {
bm.Set(tc.x, tc.y)
count := bm.FreeCount()
if tc.expect != count {
t.Errorf("expected: %d, actual %d", tc.expect, count)
}
})
}
}
func TestSetFreeN(t *testing.T) {
bm := bitmap.NewBitmap(5, 5)
type testCase struct {
x, y, number int
}
for i, tc := range []testCase{
{0, 0, 0},
{1, 0, 0},
{4, 0, 2},
{2, 2, 9},
{1, 1, 3},
{3, 1, 4},
{3, 2, 7},
{4, 4, 17},
{3, 0, 1},
} {
t.Run(fmt.Sprintf("tc#%d", i), func(t *testing.T) {
if x, y, err := bm.GetFreeN(tc.number); err != nil {
t.Errorf("get free by number=%d: %s", tc.number, err)
} else if tc.x != x || tc.y != y {
t.Fatalf("expected: x=%d, y=%d by number=%d, got: x=%d, y=%d", tc.x, tc.y, tc.number, x, y)
}
if err := bm.SetFreeN(tc.number); err != nil {
t.Errorf("set free by number=%d: %s", tc.number, err)
} else if !bm.IsSet(tc.x, tc.y) {
t.Errorf("expected to be set: free_number=%d @ x=%d,y=%d bitmap:\n%s", tc.number, tc.x, tc.y, bm)
}
})
}
bm = bitmap.NewBitmap(2, 2)
bm.Set(0, 0)
bm.Set(1, 0)
bm.Set(0, 1)
if err := bm.SetFreeN(1); err == nil {
t.Errorf("expected: error when free pixel number greater than free pixels count")
}
if _, _, err := bm.GetFreeN(1); err == nil {
t.Errorf("expected: error when free pixel number greater than free pixels count")
}
bm.Set(1, 1)
if err := bm.SetFreeN(0); err == nil {
t.Errorf("expected: error when no free pixels left")
}
}
func TestClear(t *testing.T) {
var bm = bitmap.NewBitmap(10, 10)
for range 50 {
@@ -148,37 +218,32 @@ func TestClear(t *testing.T) {
}
}
func TestCircle5x5(t *testing.T) {
func TestCircle(t *testing.T) {
type testCase struct {
x, y int
r float64
filled bool
}
bm := bitmap.NewBitmap(80, 80)
for i, tc := range []testCase{
{3, 3, 0.9, false},
{2, 2, 2, false},
{0, 2, 2, true},
{5, 5, 5, true},
{5, 0, 5, false},
{7, 7, 6.6, false},
} {
file := fmt.Sprintf("assets_test/circle_case_%02d.txt", i)
_ = file
t.Run(file, func(t *testing.T) {
bm.CircleAdjacent(tc.x, tc.y, tc.r)
b, err := os.ReadFile(file)
exampleFile := fmt.Sprintf("assets_test/circle_case_%02d.txt", i)
t.Run(exampleFile, func(t *testing.T) {
size := uint32(tc.r*2) + 2
bm := bitmap.NewBitmap(size, size)
bm.Circle(tc.x, tc.y, tc.r, tc.filled)
b, err := os.ReadFile(exampleFile)
if err != nil {
t.Fatal(err)
}
expect := strings.TrimSpace(string(b))
if expect != bm.String() {
t.Errorf("expect:\n%s\ngot:\n%s\n", expect, bm.String())
if expect != strings.TrimSpace(bm.String()) {
t.Errorf("expect:\n%s\ngot:\n%s\n", expect, bm)
}
})
}
}
func TestCircle(t *testing.T) {
var size int = 40
var bm1 = bitmap.NewBitmap(uint32(size), uint32(size))
bm1.Circle(size/2+5, size/2-5, float64(size/2)-3)
fmt.Println(bm1)
var bm2 = bitmap.NewBitmap(uint32(size), uint32(size))
bm2.CircleAdjacent(size/2+20, size/2, float64(size/2)-5)
}
-3
View File
@@ -1,3 +0,0 @@
module bitmap
go 1.24.5