Files
galaxy-game/pkg/bitmap/bitmap.go
T
2025-09-11 23:26:32 +03:00

174 lines
3.4 KiB
Go

package bitmap
import (
"errors"
"fmt"
"math"
"math/bits"
)
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) 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 float32, fill bool) {
plotX := 0
plotY := int(math.Ceil(float64(r)))
delta := 3 - 2*plotY
lastY := plotY
for plotX <= plotY {
p.octant(x, y, plotX, plotY)
if fill && 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
}
if !fill {
return
}
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 += px[p.bitVector[i]&(0b1<<bit) > 0]
cnt++
if cnt%int(p.width) == 0 {
result += "\n"
}
}
}
return result
}