Added Atan2, Len, Norm for PointF{,32}.

Added Dot for Point{,F,F32}
This commit is contained in:
Sander Schobers 2021-08-09 01:35:30 +02:00
parent 701ba39cdc
commit 88ed955ce4
3 changed files with 47 additions and 2 deletions

View File

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

View File

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

View File

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