cmd: send group
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package util
|
||||
|
||||
import "math"
|
||||
|
||||
func ShortDistance(w, h uint32, x1, y1, x2, y2 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 math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package util_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/iliadenisov/galaxy/internal/number"
|
||||
"github.com/iliadenisov/galaxy/internal/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestShortDistance(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
w, h uint32
|
||||
x1, y1, x2, y2, d float64
|
||||
}{
|
||||
{10, 10, 0, 0, 5, 5, 7.071},
|
||||
{10, 10, 0, 0, 5.01, 5.01, 7.057},
|
||||
{10, 10, 2, 2, 8, 2, 4.},
|
||||
{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)
|
||||
assert.Equal(t, tc.d, number.Fixed3(d))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user