refactor: plotter, generator
This commit is contained in:
+15
-15
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user