mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 07:37:03 +08:00
spatial/r{2,3}: add Norm and Norm2
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user