mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 23:52:47 +08:00
spatial/r{2,3}: add Norm and Norm2
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
package r2
|
||||
|
||||
import "math"
|
||||
|
||||
// Vec is a 2D vector.
|
||||
type Vec struct {
|
||||
X, Y float64
|
||||
@@ -40,6 +42,18 @@ func (p Vec) Cross(q Vec) float64 {
|
||||
return p.X*q.Y - p.Y*q.X
|
||||
}
|
||||
|
||||
// Norm returns the Euclidean norm of p
|
||||
// |p| = sqrt(p_x^2 + p_y^2).
|
||||
func Norm(p Vec) float64 {
|
||||
return math.Hypot(p.X, p.Y)
|
||||
}
|
||||
|
||||
// Norm returns the Euclidean squared norm of p
|
||||
// |p|^2 = p_x^2 + p_y^2.
|
||||
func Norm2(p Vec) float64 {
|
||||
return p.X*p.X + p.Y*p.Y
|
||||
}
|
||||
|
||||
// Box is a 2D bounding box.
|
||||
type Box struct {
|
||||
Min, Max Vec
|
||||
|
Reference in New Issue
Block a user