mirror of
https://github.com/gonum/gonum.git
synced 2025-10-13 10:54:03 +08:00
spatial/r2: implement Vec.Dot
This commit is contained in:
@@ -30,6 +30,11 @@ func (p Vec) Scale(f float64) Vec {
|
||||
return p
|
||||
}
|
||||
|
||||
// Dot returns the dot product p·q.
|
||||
func (p Vec) Dot(q Vec) float64 {
|
||||
return p.X*q.X + p.Y*q.Y
|
||||
}
|
||||
|
||||
// Box is a 2D bounding box.
|
||||
type Box struct {
|
||||
Min, Max Vec
|
||||
|
@@ -77,3 +77,38 @@ func TestScale(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDot(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
u, v Vec
|
||||
want float64
|
||||
}{
|
||||
{Vec{1, 2}, Vec{1, 2}, 5},
|
||||
{Vec{1, 0}, Vec{1, 0}, 1},
|
||||
{Vec{1, 0}, Vec{0, 1}, 0},
|
||||
{Vec{1, 0}, Vec{0, 1}, 0},
|
||||
{Vec{1, 1}, Vec{-1, -1}, -2},
|
||||
{Vec{1, 2}, Vec{-0.3, 0.4}, 0.5},
|
||||
} {
|
||||
t.Run("", func(t *testing.T) {
|
||||
{
|
||||
got := test.u.Dot(test.v)
|
||||
if got != test.want {
|
||||
t.Fatalf(
|
||||
"error: %v · %v: got=%v, want=%v",
|
||||
test.u, test.v, got, test.want,
|
||||
)
|
||||
}
|
||||
}
|
||||
{
|
||||
got := test.v.Dot(test.u)
|
||||
if got != test.want {
|
||||
t.Fatalf(
|
||||
"error: %v · %v: got=%v, want=%v",
|
||||
test.v, test.u, got, test.want,
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user