All of vectorpoint coverage
This commit is contained in:
parent
d8e3dd04a4
commit
30aee4f09f
115
govector_test.go
115
govector_test.go
@ -125,24 +125,47 @@ func TestIntersection(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAngle(t *testing.T) {
|
||||
v1 := Vector2d{10, 0}
|
||||
v2 := Vector2d{0, 10}
|
||||
|
||||
a := Angle(v2, v1)
|
||||
|
||||
if math.Abs(float64(a))-math.Pi/2 > Epsilon {
|
||||
t.Errorf("Angle Error")
|
||||
cases := []struct {
|
||||
v1 Vector2d
|
||||
v2 Vector2d
|
||||
expected float32
|
||||
}{
|
||||
{
|
||||
v1: Vector2d{10, 0},
|
||||
v2: Vector2d{0, 10},
|
||||
expected: -math.Pi / 2,
|
||||
},
|
||||
{
|
||||
v1: Vector2d{5, 0},
|
||||
v2: Vector2d{10, 10},
|
||||
expected: -math.Pi / 4,
|
||||
},
|
||||
{
|
||||
v1: Vector2d{0, 10},
|
||||
v2: Vector2d{0, -10},
|
||||
expected: math.Pi,
|
||||
},
|
||||
// nan angles:
|
||||
{
|
||||
v1: Vector2d{0, 0},
|
||||
v2: Vector2d{0, 0},
|
||||
expected: 0,
|
||||
},
|
||||
// nan angles
|
||||
{
|
||||
v1: Vector2d{0.0001, 0},
|
||||
v2: Vector2d{0, 0},
|
||||
expected: math.Pi * Rad2deg,
|
||||
},
|
||||
}
|
||||
var a float32
|
||||
for _, c := range cases {
|
||||
a = Angle(c.v2, c.v1)
|
||||
|
||||
v1 = Vector2d{5, 0}
|
||||
v2 = Vector2d{10, 10}
|
||||
|
||||
a = Angle(v1, v2)
|
||||
|
||||
if math.Abs(float64(a))-math.Pi/4 > Epsilon {
|
||||
t.Errorf("Angle Error")
|
||||
if math.Abs(float64(a-c.expected)) > Epsilon {
|
||||
t.Errorf("Angle Error; expected: %f, actual: %f", c.expected, a)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRotate(t *testing.T) {
|
||||
@ -354,3 +377,65 @@ func TestOrientedSquare(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPopPop(t *testing.T) {
|
||||
v1 := Vector2d{10, 10}
|
||||
if v1.PopPop() != float32(math.Sqrt(10*10+10*10)) {
|
||||
t.Errorf("Incorrect Magnitude!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAASquareAtPoint(t *testing.T) {
|
||||
p1 := Point2d{10, 10}
|
||||
expected := AABB2d{
|
||||
A: Point2d{5, 5},
|
||||
B: Point2d{15, 15},
|
||||
}
|
||||
aabb := AASquareAtPoint(p1, 10)
|
||||
if aabb != expected {
|
||||
t.Errorf("incorrect AABB2d; expected: %+v, calculated: %+v ", expected, aabb)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDistance(t *testing.T) {
|
||||
if Distance(Point2d{0, 0}, Point2d{0, 0}) != 0.0 {
|
||||
t.Error("incorectly calculating distance between two points at origin")
|
||||
}
|
||||
if Distance(Point2d{0, 0}, Point2d{10, 0}) != 10.0 {
|
||||
t.Error("incorectly calculating distance between points on y axis")
|
||||
}
|
||||
if Distance(Point2d{0, 0}, Point2d{3, 4}) != 5.0 {
|
||||
t.Error("incorectly calculating distance for pythagorean triple")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAABBToRect(t *testing.T) {
|
||||
r := AABB2d{
|
||||
A: Point2d{5, 5},
|
||||
B: Point2d{15, 15},
|
||||
}
|
||||
p := r.ToPolygon()
|
||||
expected := Polygon2d{
|
||||
Points: []Vector2d{
|
||||
Vector2d{X: 0, Y: 0},
|
||||
Vector2d{X: 0, Y: 10},
|
||||
Vector2d{X: 10, Y: 10},
|
||||
Vector2d{X: 10, Y: 0},
|
||||
},
|
||||
Origin: Point2d{X: 5, Y: 5},
|
||||
}
|
||||
if len(p.Points) != 4 {
|
||||
t.Error("incorrect number of points")
|
||||
}
|
||||
// alright, this just feels cumbersome ... unfortunate that structs with
|
||||
// slices can't be compared this way ...
|
||||
if p.Origin != expected.Origin {
|
||||
t.Error("just wrong")
|
||||
}
|
||||
for i, pp := range expected.Points {
|
||||
if p.Points[i] != pp {
|
||||
t.Error("just wrong")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,6 +140,7 @@ func Distance(p1, p2 Point2d) float32 {
|
||||
return p1.Sub(p2).Mag()
|
||||
}
|
||||
|
||||
// returns radians
|
||||
func Angle(v1, v2 Vector2d) float32 {
|
||||
x := (v1.Dot(v2) / (v1.Mag() * v2.Mag()))
|
||||
angle := math.Acos(float64(x))
|
||||
|
Loading…
Reference in New Issue
Block a user