mirror of
https://github.com/gonum/gonum.git
synced 2025-10-16 04:00:48 +08:00
spatial/r2: implement Vec.Cross
This commit is contained in:
@@ -35,6 +35,11 @@ func (p Vec) Dot(q Vec) float64 {
|
|||||||
return p.X*q.X + p.Y*q.Y
|
return p.X*q.X + p.Y*q.Y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cross returns the cross product p×q.
|
||||||
|
func (p Vec) Cross(q Vec) float64 {
|
||||||
|
return p.X*q.Y - p.Y*q.X
|
||||||
|
}
|
||||||
|
|
||||||
// Box is a 2D bounding box.
|
// Box is a 2D bounding box.
|
||||||
type Box struct {
|
type Box struct {
|
||||||
Min, Max Vec
|
Min, Max Vec
|
||||||
|
@@ -112,3 +112,26 @@ func TestDot(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCross(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
v1, v2 Vec
|
||||||
|
want float64
|
||||||
|
}{
|
||||||
|
{Vec{1, 0}, Vec{1, 0}, 0},
|
||||||
|
{Vec{1, 0}, Vec{0, 1}, 1},
|
||||||
|
{Vec{0, 1}, Vec{1, 0}, -1},
|
||||||
|
{Vec{1, 2}, Vec{-4, 5}, 13},
|
||||||
|
{Vec{1, 2}, Vec{2, 3}, -1},
|
||||||
|
} {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
got := test.v1.Cross(test.v2)
|
||||||
|
if got != test.want {
|
||||||
|
t.Fatalf(
|
||||||
|
"error: %v × %v = %v, want %v",
|
||||||
|
test.v1, test.v2, got, test.want,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user