spatial/r{2,3}: add Norm and Norm2

This commit is contained in:
Sebastien Binet
2020-04-01 11:26:45 +02:00
committed by GitHub
parent def9e57adc
commit 962425fcd9
4 changed files with 116 additions and 1 deletions

View File

@@ -4,6 +4,8 @@
package r3
import "math"
// Vec is a 3D vector.
type Vec struct {
X, Y, Z float64
@@ -47,6 +49,18 @@ func (p Vec) Cross(q Vec) Vec {
}
}
// Norm returns the Euclidean norm of p
// |p| = sqrt(p_x^2 + p_y^2 + p_z^2).
func Norm(p Vec) float64 {
return math.Hypot(p.X, math.Hypot(p.Y, p.Z))
}
// Norm returns the Euclidean squared norm of p
// |p|^2 = p_x^2 + p_y^2 + p_z^2.
func Norm2(p Vec) float64 {
return p.X*p.X + p.Y*p.Y + p.Z*p.Z
}
// Box is a 3D bounding box.
type Box struct {
Min, Max Vec