refactor: plotter, generator
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package plotter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/bitmap"
|
||||
)
|
||||
|
||||
type Plotter struct {
|
||||
factor float32
|
||||
clearFn func()
|
||||
circleFn func(x, y int, r float32)
|
||||
freeCountFn func() int
|
||||
freeNumberToCoordFn func(int) (int, int, error)
|
||||
}
|
||||
|
||||
func NewPlotter(width, height uint32, factor float32) (Plotter, error) {
|
||||
return NewBitmapPlotter(NewBitmap(width, height, factor), factor)
|
||||
}
|
||||
|
||||
func NewBitmap(width, height uint32, factor float32) bitmap.Bitmap {
|
||||
return bitmap.NewBitmap(AsPlotterSize(width, height, factor))
|
||||
}
|
||||
|
||||
func NewBitmapPlotter(bm bitmap.Bitmap, factor float32) (Plotter, error) {
|
||||
if factor > 1 || factor <= 0 {
|
||||
return Plotter{}, fmt.Errorf("factor should be: 0 > F <= 1")
|
||||
}
|
||||
return Plotter{
|
||||
factor: factor,
|
||||
clearFn: bm.Clear,
|
||||
circleFn: func(x, y int, r float32) { bm.Circle(x, y, r, true) },
|
||||
freeCountFn: bm.FreeCount,
|
||||
freeNumberToCoordFn: bm.GetFreeN,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, error) {
|
||||
if deadZoneRaduis <= 0. {
|
||||
return 0, 0, fmt.Errorf("radius must be positive value: %f", deadZoneRaduis)
|
||||
}
|
||||
fsCount := p.freeCountFn()
|
||||
if fsCount == 0 {
|
||||
return 0, 0, errors.New("RandomFreePoint: no free space left")
|
||||
}
|
||||
next := rand.Intn(fsCount)
|
||||
x, y, err := p.freeNumberToCoordFn(next)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("RandomFreePoint: freeNumberToCoordFn: %s", err)
|
||||
}
|
||||
p.plotDeadZone(x, y, deadZoneRaduis)
|
||||
planetX := float32(x)*p.factor + rand.Float32()*p.factor
|
||||
planetY := float32(y)*p.factor + rand.Float32()*p.factor
|
||||
return planetX, planetY, nil
|
||||
}
|
||||
|
||||
func (p Plotter) MarkDeadZone(x, y float32, radius float32) {
|
||||
p.plotDeadZone(int(x/p.factor), int(y/p.factor), radius)
|
||||
}
|
||||
|
||||
func (p Plotter) plotDeadZone(x, y int, radius float32) {
|
||||
p.circleFn(x, y, radius/p.factor)
|
||||
}
|
||||
|
||||
func (p Plotter) Clear() { p.clearFn() }
|
||||
|
||||
func AsPlotterSize(width, height uint32, factor float32) (uint32, uint32) {
|
||||
if factor > 1 || factor <= 0 {
|
||||
return width, height
|
||||
}
|
||||
return uint32(math.Ceil(float64(width) / float64(factor))), uint32(math.Ceil(float64(height) / float64(factor)))
|
||||
}
|
||||
Reference in New Issue
Block a user