feat: move func to calc package
This commit is contained in:
+5
-18
@@ -1,6 +1,9 @@
|
||||
package util
|
||||
|
||||
import "math"
|
||||
import (
|
||||
"galaxy/calc"
|
||||
"math"
|
||||
)
|
||||
|
||||
// WrapF maps value into the half-open interval [0, float64(size)).
|
||||
// It supports negative input values and is used for torus coordinates
|
||||
@@ -28,12 +31,8 @@ func WrapF(value float64, size int) float64 {
|
||||
return r
|
||||
}
|
||||
|
||||
func ShortDistance(w, h uint32, x1, y1, x2, y2 float64) float64 {
|
||||
return math.Hypot(deltas(w, h, x1, y1, x2, y2))
|
||||
}
|
||||
|
||||
func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float64, bool) {
|
||||
deltaX, deltaY := deltas(w, h, x1, y1, x2, y2)
|
||||
deltaX, deltaY := calc.Deltas(w, h, x1, y1, x2, y2)
|
||||
distance := math.Hypot(deltaX, deltaY)
|
||||
if distance <= delta {
|
||||
return x2, y2, true
|
||||
@@ -93,15 +92,3 @@ func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float
|
||||
|
||||
return tx, ty, false
|
||||
}
|
||||
|
||||
func deltas(w, h uint32, x1, y1, x2, y2 float64) (float64, float64) {
|
||||
dx := math.Abs(x2 - x1)
|
||||
dy := math.Abs(y2 - y1)
|
||||
if dx > float64(w/2) {
|
||||
dx = float64(h) - dx
|
||||
}
|
||||
if dy > float64(h/2) {
|
||||
dy = float64(h) - dy
|
||||
}
|
||||
return dx, dy
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"galaxy/calc"
|
||||
"galaxy/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -22,7 +23,7 @@ func TestShortDistance(t *testing.T) {
|
||||
{10, 10, 8, 7, 1, 7, 3.},
|
||||
} {
|
||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||
d := util.ShortDistance(tc.w, tc.h, tc.x1, tc.y1, tc.x2, tc.y2)
|
||||
d := calc.ShortDistance(tc.w, tc.h, tc.x1, tc.y1, tc.x2, tc.y2)
|
||||
assert.Equal(t, tc.d, util.Fixed3(d))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user