Skip to content

Commit

Permalink
spatial/r3: implement Vec.Dot
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Mar 30, 2020
1 parent c5a3c90 commit 2321f50
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spatial/r3/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 + p.Z*q.Z
}

// Box is a 3D bounding box.
type Box struct {
Min, Max Vec
Expand Down
35 changes: 35 additions & 0 deletions spatial/r3/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, 3}, Vec{1, 2, 3}, 14},
{Vec{1, 0, 0}, Vec{1, 0, 0}, 1},
{Vec{1, 0, 0}, Vec{0, 1, 0}, 0},
{Vec{1, 0, 0}, Vec{0, 1, 1}, 0},
{Vec{1, 1, 1}, Vec{-1, -1, -1}, -3},
{Vec{1, 2, 2}, Vec{-0.3, 0.4, -1.2}, -1.9},
} {
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,
)
}
}
})
}
}

0 comments on commit 2321f50

Please sign in to comment.