149 lines
3.0 KiB
Go
149 lines
3.0 KiB
Go
package draw
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
const batchSize = 32
|
|
|
|
type Plane struct {
|
|
Width uint
|
|
Height uint
|
|
pixels []uint32
|
|
}
|
|
|
|
func (p *Plane) pixelsValue() []uint32 {
|
|
return p.pixels
|
|
}
|
|
|
|
func New(width uint, height uint) Plane {
|
|
elements := int(math.Ceil(float64(width*height) / batchSize))
|
|
return Plane{Width: width, Height: height, pixels: make([]uint32, elements)}
|
|
}
|
|
|
|
func (p Plane) Mark(x, y int) {
|
|
boundX := (p.Width + uint(x)) % p.Width
|
|
boundY := (p.Height + uint(y)) % p.Height
|
|
p.mark(boundX + boundY*p.Width)
|
|
|
|
// p.mark(x + y*p.Width)
|
|
|
|
// number := x + y*p.width
|
|
// index := number / batchSize
|
|
// bit := number % batchSize
|
|
// p.pixels[index] |= (0b1 << bit)
|
|
}
|
|
|
|
func (p Plane) mark(number uint) {
|
|
// index := number / batchSize
|
|
// bit := number % batchSize
|
|
p.pixels[number/batchSize] |= (0b1 << (number % batchSize))
|
|
}
|
|
|
|
func (p Plane) Marked(x, y uint) bool {
|
|
return p.marked(x + y*p.Width)
|
|
// number := x + y*p.width
|
|
// index := number / batchSize
|
|
// bit := number % batchSize
|
|
// return p.pixels[index]&(0b1<<bit) > 0
|
|
}
|
|
|
|
func (p Plane) marked(number uint) bool {
|
|
// index := number / batchSize
|
|
// bit := number % batchSize
|
|
return p.pixels[number/batchSize]&(0b1<<(number%batchSize)) > 0
|
|
}
|
|
|
|
func (p Plane) 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 Plane) 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 Plane) octant(x, y int, plotX, plotY int) {
|
|
p.Mark(x+plotX, y+plotY)
|
|
p.Mark(x+plotX, y-plotY)
|
|
p.Mark(x-plotX, y+plotY)
|
|
p.Mark(x-plotX, y-plotY)
|
|
p.Mark(x+plotY, y+plotX)
|
|
p.Mark(x+plotY, y-plotX)
|
|
p.Mark(x-plotY, y+plotX)
|
|
p.Mark(x-plotY, y-plotX)
|
|
}
|
|
|
|
func (p Plane) Clear() {
|
|
for i := range p.pixels {
|
|
p.pixels[i] &= 0
|
|
}
|
|
}
|
|
|
|
func (p Plane) String() string {
|
|
px := map[bool]string{true: "x", false: "_"}
|
|
var result string
|
|
// for y := 0; y < int(p.height); y++ {
|
|
// for x := 0; x < int(p.width); x++ {
|
|
// result += px[p.Marked(uint(x), uint(y))] + " "
|
|
// }
|
|
// result += "\n"
|
|
// }
|
|
// result += "\n"
|
|
cnt := 0
|
|
for i := 0; i < len(p.pixels); i++ {
|
|
for bit := 0; bit < batchSize && cnt < int(p.Width*p.Height); bit++ {
|
|
result += fmt.Sprintf("%s ", px[p.pixels[i]&(0b1<<bit) > 0])
|
|
cnt++
|
|
if cnt%int(p.Width) == 0 {
|
|
result += "\n"
|
|
}
|
|
}
|
|
}
|
|
return result
|
|
}
|