122 lines
2.4 KiB
Go
122 lines
2.4 KiB
Go
package bitmap
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
const intSize = 32
|
|
|
|
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 (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) {
|
|
p.bitVector[number/intSize] |= (0b1 << (number % intSize))
|
|
}
|
|
|
|
func (p bitmap) IsSet(x, y int) bool {
|
|
return p.isSet(uint32(x) + uint32(y)*p.width)
|
|
}
|
|
|
|
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) {
|
|
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 {
|
|
for lineX := 0; lineX < plotX; lineX++ {
|
|
p.octant(x, y, lineX, plotY)
|
|
}
|
|
lastY = plotY
|
|
}
|
|
if delta < 0 {
|
|
delta += 4*plotX + 6
|
|
} else {
|
|
delta += 4*(plotX-plotY) + 10
|
|
plotY -= 1
|
|
}
|
|
plotX += 1
|
|
}
|
|
for fillX := 0; fillX < plotX; fillX++ {
|
|
for fillY := 0; fillY <= fillX; fillY++ {
|
|
p.octant(x, y, fillX, fillY)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p bitmap) CircleAdjacent(x, y int, r float64) {
|
|
plotX := 0
|
|
plotY := int(math.Ceil(r))
|
|
delta := 1 - 2*plotY
|
|
err := 0
|
|
for plotX <= plotY {
|
|
p.octant(x, y, plotX, plotY)
|
|
err = 2*(delta+plotY) - 1
|
|
if delta < 0 && err <= 0 {
|
|
plotX += 1
|
|
delta += 2*plotX + 1
|
|
continue
|
|
}
|
|
if delta > 0 && err > 0 {
|
|
plotY -= 1
|
|
delta -= 2*plotY + 1
|
|
continue
|
|
}
|
|
plotX += 1
|
|
plotY -= 1
|
|
delta += 2 * (plotX - plotY)
|
|
}
|
|
}
|
|
|
|
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)
|
|
p.Set(x-plotX, y-plotY)
|
|
p.Set(x+plotY, y+plotX)
|
|
p.Set(x+plotY, y-plotX)
|
|
p.Set(x-plotY, y+plotX)
|
|
p.Set(x-plotY, y-plotX)
|
|
}
|
|
|
|
func (p bitmap) Clear() {
|
|
for i := range p.bitVector {
|
|
p.bitVector[i] &= 0
|
|
}
|
|
}
|
|
|
|
func (p bitmap) String() string {
|
|
px := map[bool]string{true: "██", false: "░░"}
|
|
var result 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])
|
|
cnt++
|
|
if cnt%int(p.width) == 0 {
|
|
result += "\n"
|
|
}
|
|
}
|
|
}
|
|
return result
|
|
}
|