refactor: plotter, generator

This commit is contained in:
Ilia Denisov
2025-09-12 21:32:50 +03:00
parent d3b00b5c8d
commit 05999687aa
11 changed files with 262 additions and 140 deletions
+15 -15
View File
@@ -9,35 +9,35 @@ import (
const intSize = 32
type bitmap struct {
type Bitmap struct {
width uint32
height uint32
bitVector []uint32
}
func NewBitmap(width uint32, height uint32) bitmap {
return bitmap{width: width, height: height, bitVector: make([]uint32, int(math.Ceil(float64(width*height)/intSize)))}
func NewBitmap(width uint32, height uint32) Bitmap {
return Bitmap{width: width, height: height, bitVector: make([]uint32, int(math.Ceil(float64(width*height)/intSize)))}
}
func (p bitmap) Set(x, y int) {
func (p Bitmap) Set(x, y int) {
boundX := (p.width + uint32(x)) % p.width
boundY := (p.height + uint32(y)) % p.height
p.set(boundX + boundY*p.width)
}
func (p bitmap) set(number uint32) {
func (p Bitmap) set(number uint32) {
p.bitVector[number/intSize] |= (0b1 << (number % intSize))
}
func (p bitmap) IsSet(x, y int) bool {
func (p Bitmap) IsSet(x, y int) bool {
return p.isSet(uint32(x) + uint32(y)*p.width)
}
func (p bitmap) isSet(number uint32) bool {
func (p Bitmap) isSet(number uint32) bool {
return p.bitVector[number/intSize]&(0b1<<(number%intSize)) > 0
}
func (p bitmap) FreeCount() (result int) {
func (p Bitmap) FreeCount() (result int) {
result = int(p.width) * int(p.height)
for i := range p.bitVector {
result -= bits.OnesCount32(p.bitVector[i])
@@ -45,7 +45,7 @@ func (p bitmap) FreeCount() (result int) {
return
}
func (p bitmap) GetFreeN(number int) (int, int, error) {
func (p Bitmap) GetFreeN(number int) (int, int, error) {
if p.FreeCount() == 0 {
return 0, 0, errors.New("no free pixels left")
}
@@ -65,7 +65,7 @@ func (p bitmap) GetFreeN(number int) (int, int, error) {
return 0, 0, fmt.Errorf("get free pixel: no such number=%d, max=%d", number, n)
}
func (p bitmap) SetFreeN(number int) error {
func (p Bitmap) SetFreeN(number int) error {
if p.FreeCount() == 0 {
return errors.New("no free pixels left")
}
@@ -84,7 +84,7 @@ func (p bitmap) SetFreeN(number int) error {
return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n)
}
func (p bitmap) Circle(x, y int, r float32, fill bool) {
func (p Bitmap) Circle(x, y int, r float32, fill bool) {
plotX := 0
plotY := int(math.Ceil(float64(r)))
delta := 3 - 2*plotY
@@ -115,7 +115,7 @@ func (p bitmap) Circle(x, y int, r float32, fill bool) {
}
}
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
@@ -139,7 +139,7 @@ func (p bitmap) circleAdjacent(x, y int, r float64) {
}
}
func (p bitmap) octant(x, y int, plotX, plotY int) {
func (p Bitmap) octant(x, y int, plotX, plotY int) {
p.Set(x+plotX, y+plotY)
p.Set(x+plotX, y-plotY)
p.Set(x-plotX, y+plotY)
@@ -150,13 +150,13 @@ func (p bitmap) octant(x, y int, plotX, plotY int) {
p.Set(x-plotY, y-plotX)
}
func (p bitmap) Clear() {
func (p Bitmap) Clear() {
for i := range p.bitVector {
p.bitVector[i] &= 0
}
}
func (p bitmap) String() string {
func (p Bitmap) String() string {
px := map[bool]string{true: "██", false: "░░"}
var result string
cnt := 0
+2 -2
View File
@@ -2,8 +2,8 @@ package bitmap
import "slices"
func (p bitmap) value() []uint32 {
func (p Bitmap) value() []uint32 {
return slices.Clone(p.bitVector)
}
var Value = (bitmap).value
var Value = (Bitmap).value