geom/pointf.go

159 lines
3.6 KiB
Go

package geom
import (
"math"
)
// PointF is an X, Y coordinate pair (floating point).
type PointF struct {
X, Y float64
}
// ZeroPtF is initialized on (0, 0).
var ZeroPtF = PointF{X: 0, Y: 0}
// PtF is a shorthand function to create a point.
func PtF(x, y float64) PointF {
return PointF{X: x, Y: y}
}
// Add adds q as a vector to p.
func (p PointF) Add(q PointF) PointF {
return PointF{p.X + q.X, p.Y + q.Y}
}
// Add2D adds x and y to X and Y of point p and returns the sum.
func (p PointF) Add2D(x, y float64) PointF {
return PtF(p.X+x, p.Y+y)
}
// AngleTo calculates the angle [0..2*Pi) from point p to point q.
func (p PointF) AngleTo(q PointF) float64 {
a := math.Atan((p.Y - q.Y) / (p.X - q.X))
if q.X < p.X {
return a + math.Pi
}
if a < 0 {
a += 2 * math.Pi
}
return a
}
// Distance calculates the distance between points p and q.
func (p PointF) Distance(q PointF) float64 {
return math.Sqrt(p.Distance2(q))
}
// Distance2 calculates the squared distance between points p and q.
func (p PointF) Distance2(q PointF) float64 {
dx := q.X - p.X
dy := q.Y - p.Y
return dx*dx + dy*dy
}
// DistanceToLine calculates the distance to the line segment a, b.
func (p PointF) DistanceToLine(a, b PointF) float64 {
dx1, dy1 := (a.X - p.X), (a.Y - p.Y)
dx2, dy2 := (b.X - a.X), (b.Y - a.Y)
t := -((dx1*dx2 + dy1*dy2) / (dx2*dx2 + dy2*dy2))
if 0 <= t && t <= 1 {
return Abs(dx2*dy1-dy2*dx1) / Sqrt(dx2*dx2+dy2*dy2)
}
d1, d2 := Sqrt(Sq(b.X-p.X)+Sq(b.Y-p.Y)), Sqrt(dx1*dx1+dy1*dy1)
if d1 < d2 {
return d1
}
return d2
}
// DistanceToLines calculates the smallest distance to the line segments of q.
func (p PointF) DistanceToLines(q PointsF) float64 {
n := len(q)
if n == 0 {
return NaN()
}
if n == 1 {
return p.Distance(q[0])
}
min := p.DistanceToLine(q[0], q[1])
for i := range q {
if i < 2 {
continue
}
d := p.DistanceToLine(q[i-1], q[i])
if d < min {
min = d
}
}
return min
}
// DistanceToPolygon calculates the smallest distance to the polygon q.
func (p PointF) DistanceToPolygon(q PolygonF) float64 {
if len(q.Points) == 0 {
return NaN()
}
return p.DistanceToLines(append(q.Points, q.Points[0]))
}
// Div divides the X and Y values of point p with t and returns the result.
func (p PointF) Div(t float64) PointF {
return PtF(p.X/t, p.Y/t)
}
// 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 {
return false
}
return true
}
// InPolygon tests is the point p is inside the polygon q.
func (p PointF) InPolygon(q PolygonF) bool {
var n = len(q.Points)
var c = false
var i = 0
var j = n - 1
for i < n {
if ((q.Points[i].Y >= p.Y) != (q.Points[j].Y >= p.Y)) &&
(p.X <= (q.Points[j].X-q.Points[i].X)*(p.Y-q.Points[i].Y)/(q.Points[j].Y-q.Points[i].Y)+q.Points[i].X) {
c = !c
}
j = i
i++
}
return c
}
// Invert changes the sign of the components.
func (p PointF) Invert() PointF {
return PointF{-p.X, -p.Y}
}
// Mul multiplier 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)
}
// Sub subtracts q as a vector from p.
func (p PointF) Sub(q PointF) PointF {
return PointF{p.X - q.X, p.Y - q.Y}
}
// To32 transforms the point p into a PointF32.
func (p PointF) To32() PointF32 {
return PointF32{float32(p.X), float32(p.Y)}
}
// MaxPtF returns the point that is at the largest X & Y position of a and b.
func MaxPtF(a, b PointF) PointF {
return PtF(Max(a.X, b.X), Max(a.Y, b.Y))
}
// MinPtF returns the point that is at the smallest X & Y position of a and b.
func MinPtF(a, b PointF) PointF {
return PtF(Min(a.X, b.X), Min(a.Y, b.Y))
}