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 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