spatial/{r2,r3}: add Cos, Unit

This commit is contained in:
Sebastien Binet
2020-04-08 12:28:23 +02:00
committed by GitHub
parent 962425fcd9
commit 448e2b6d0e
4 changed files with 179 additions and 1 deletions

View File

@@ -61,6 +61,20 @@ func Norm2(p Vec) float64 {
return p.X*p.X + p.Y*p.Y + p.Z*p.Z
}
// Unit returns the unit vector colinear to p.
// Unit returns {NaN,NaN,NaN} for the zero vector.
func Unit(p Vec) Vec {
if p.X == 0 && p.Y == 0 && p.Z == 0 {
return Vec{X: math.NaN(), Y: math.NaN(), Z: math.NaN()}
}
return p.Scale(1 / Norm(p))
}
// Cos returns the cosine of the opening angle between p and q.
func Cos(p, q Vec) float64 {
return p.Dot(q) / (Norm(p) * Norm(q))
}
// Box is a 3D bounding box.
type Box struct {
Min, Max Vec