use float64

This commit is contained in:
Ilia Denisov
2025-09-23 22:27:09 +03:00
parent 1a1bd0af17
commit 7d93176031
9 changed files with 60 additions and 60 deletions
+12 -12
View File
@@ -10,35 +10,35 @@ import (
)
type Plotter struct {
factor float32
factor float64
clearFn func()
circleFn func(x, y int, r float32)
circleFn func(x, y int, r float64)
freeCountFn func() int
freeNumberToCoordFn func(int) (int, int, error)
}
func NewPlotter(width, height uint32, factor float32) (Plotter, error) {
func NewPlotter(width, height uint32, factor float64) (Plotter, error) {
return NewBitmapPlotter(NewBitmap(width, height, factor), factor)
}
func NewBitmap(width, height uint32, factor float32) bitmap.Bitmap {
func NewBitmap(width, height uint32, factor float64) bitmap.Bitmap {
return bitmap.NewBitmap(AsPlotterSize(width, height, factor))
}
func NewBitmapPlotter(bm bitmap.Bitmap, factor float32) (Plotter, error) {
func NewBitmapPlotter(bm bitmap.Bitmap, factor float64) (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) },
circleFn: func(x, y int, r float64) { bm.Circle(x, y, r, true) },
freeCountFn: bm.FreeCount,
freeNumberToCoordFn: bm.GetFreeN,
}, nil
}
func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, error) {
func (p Plotter) RandomFreePoint(deadZoneRaduis float64) (float64, float64, error) {
fsCount := p.freeCountFn()
if fsCount == 0 {
return 0, 0, errors.New("RandomFreePoint: no free space left")
@@ -51,22 +51,22 @@ func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, erro
if deadZoneRaduis > 0 {
p.plotDeadZone(x, y, deadZoneRaduis)
}
planetX := float32(x)*p.factor + rand.Float32()*p.factor
planetY := float32(y)*p.factor + rand.Float32()*p.factor
planetX := float64(x)*p.factor + rand.Float64()*p.factor
planetY := float64(y)*p.factor + rand.Float64()*p.factor
return planetX, planetY, nil
}
func (p Plotter) MarkDeadZone(x, y float32, radius float32) {
func (p Plotter) MarkDeadZone(x, y float64, radius float64) {
p.plotDeadZone(int(x/p.factor), int(y/p.factor), radius)
}
func (p Plotter) plotDeadZone(x, y int, radius float32) {
func (p Plotter) plotDeadZone(x, y int, radius float64) {
p.circleFn(x, y, radius/p.factor)
}
func (p Plotter) Clear() { p.clearFn() }
func AsPlotterSize(width, height uint32, factor float32) (uint32, uint32) {
func AsPlotterSize(width, height uint32, factor float64) (uint32, uint32) {
if factor > 1 || factor <= 0 {
return width, height
}
+4 -4
View File
@@ -28,7 +28,7 @@ func TestNewPlotter(t *testing.T) {
func TestAsPlotterSize(t *testing.T) {
for _, tc := range []struct {
w, h uint32
f float32
f float64
ew, eh uint32
}{
{10, 10, 0, 10, 10},
@@ -46,7 +46,7 @@ func TestAsPlotterSize(t *testing.T) {
}
func TestRandomFreePoint(t *testing.T) {
var factor float32 = 0.25
var factor float64 = 0.25
var w, h uint32 = 20, 20
bm := plotter.NewBitmap(w, h, factor)
p, err := plotter.NewBitmapPlotter(bm, factor) // 80x80
@@ -58,7 +58,7 @@ func TestRandomFreePoint(t *testing.T) {
if err != nil {
t.Errorf("expect: no error getting random point, got: %s", err)
}
if x > float32(w) || y > float32(w) {
if x > float64(w) || y > float64(w) {
t.Errorf("expect: point coordinates within map size %dx%d, got: x=%f y=%f", w, h, x, y)
}
@@ -67,7 +67,7 @@ func TestRandomFreePoint(t *testing.T) {
t.Errorf("expect: no error when radius is zero, got: %s", err)
}
_, _, err = p.RandomFreePoint(float32(w + h)) // guaranteed to mark whole area dead zone
_, _, err = p.RandomFreePoint(float64(w + h)) // guaranteed to mark whole area dead zone
if err != nil {
t.Errorf("expect: no error getting random point, got: %s", err)
}