From 88ed955ce46d81ce715c8c642c4b80860adabafb Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 9 Aug 2021 01:35:30 +0200 Subject: [PATCH] Added Atan2, Len, Norm for PointF{,32}. Added Dot for Point{,F,F32} --- point.go | 5 +++++ pointf.go | 22 +++++++++++++++++++++- pointf32.go | 22 +++++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/point.go b/point.go index bcbf314..cc975ba 100644 --- a/point.go +++ b/point.go @@ -64,6 +64,11 @@ func (p Point) DistInt(q Point) int { return ints.SubAbs(p.X, q.X) + ints.SubAbs(p.Y, q.Y) } +// Dot returns the dot product of p and q. +func (p Point) Dot(q Point) int { + return p.X*p.X + p.Y*p.Y +} + // In tests if the point p is inside the rectangle r. func (p Point) In(r Rectangle) bool { if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y { diff --git a/pointf.go b/pointf.go index 86ef9b8..58fef16 100644 --- a/pointf.go +++ b/pointf.go @@ -39,9 +39,14 @@ func (p PointF) AngleTo(q PointF) float64 { return a } +// Atan2 returns the arc tangent of y/x. +func (p PointF) Atan2() float64 { + return Atan2(p.Y, p.X) +} + // Distance calculates the distance between points p and q. func (p PointF) Distance(q PointF) float64 { - return math.Sqrt(p.Distance2(q)) + return Sqrt(p.Distance2(q)) } // Distance2 calculates the squared distance between points p and q. @@ -101,6 +106,11 @@ func (p PointF) Div(t float64) PointF { return PtF(p.X/t, p.Y/t) } +// Dot returns the dot product of p and q. +func (p PointF) Dot(q PointF) float64 { + return p.X*q.X + p.Y*q.Y +} + // In tests if the point p is inside the rectangle r. func (p PointF) In(r RectangleF) bool { if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y { @@ -132,6 +142,11 @@ func (p PointF) Invert() PointF { return PointF{-p.X, -p.Y} } +// Len returns the length of the vector. +func (p PointF) Len() float64 { + return Sqrt(p.X*p.X + p.Y*p.Y) +} + // Mul multiplies the X and Y values of point p with t and returns the result. func (p PointF) Mul(t float64) PointF { return PtF(p.X*t, p.Y*t) @@ -142,6 +157,11 @@ func (p PointF) Mul2D(x, y float64) PointF { return PtF(p.X*x, p.Y*y) } +// Norm returns the normalized vector of x and y. +func (p PointF) Norm() PointF { + return p.Mul(1 / p.Len()) +} + // Rect returns a rectangle starting from point p to given point q func (p PointF) Rect(q PointF) RectangleF { return RectangleF{Min: p, Max: q} diff --git a/pointf32.go b/pointf32.go index 8b5b1ae..4afc182 100644 --- a/pointf32.go +++ b/pointf32.go @@ -39,9 +39,14 @@ func (p PointF32) AngleTo(q PointF32) float32 { return a } +// Atan2 returns the arc tangent of y/x. +func (p PointF32) Atan2() float32 { + return Atan232(p.Y, p.X) +} + // Distance calculates the distance between points p and q. func (p PointF32) Distance(q PointF32) float32 { - return float32(math.Sqrt(float64(p.Distance2(q)))) + return Sqrt32(p.Distance2(q)) } // Distance2 calculates the squared distance between points p and q. @@ -101,6 +106,11 @@ func (p PointF32) Div(t float32) PointF32 { return PtF32(p.X/t, p.Y/t) } +// Dot returns the dot product of p and q. +func (p PointF32) Dot(q PointF32) float32 { + return p.X*q.X + p.Y*q.Y +} + // In tests if the point p is inside the rectangle r. func (p PointF32) In(r RectangleF32) bool { if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y { @@ -132,6 +142,11 @@ func (p PointF32) Invert() PointF32 { return PointF32{-p.X, -p.Y} } +// Len returns the length of the vector. +func (p PointF32) Len() float32 { + return Sqrt32(p.X*p.X + p.Y*p.Y) +} + // Mul multiplies the X and Y values of point p with t and returns the result. func (p PointF32) Mul(t float32) PointF32 { return PtF32(p.X*t, p.Y*t) @@ -142,6 +157,11 @@ func (p PointF32) Mul2D(x, y float32) PointF32 { return PtF32(p.X*x, p.Y*y) } +// Norm returns the normalized vector of x and y. +func (p PointF32) Norm() PointF32 { + return p.Mul(1 / p.Len()) +} + // Rect returns a rectangle starting from point p to given point q func (p PointF32) Rect(q PointF32) RectangleF32 { return RectangleF32{Min: p, Max: q}