cleanup and optimization for Point.OnSegment

This commit is contained in:
Gavin Nishizawa 2023-03-15 13:29:23 -07:00
parent ad4d483bf8
commit 0fe8a6b5f0
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -199,9 +199,23 @@ func (p *Point) FormattedCoordinates() string {
return fmt.Sprintf("%d,%d", int(p.X), int(p.Y)) return fmt.Sprintf("%d,%d", int(p.X), int(p.Y))
} }
func (q *Point) OnSegment(p, r *Point) bool { // returns true if point p is on orthogonal segment between points a and b
return (q.X <= math.Max(p.X, r.X)) && (q.X >= math.Min(p.X, r.X)) && func (p *Point) OnOrthogonalSegment(a, b *Point) bool {
(q.Y <= math.Max(p.Y, r.Y)) && (q.Y >= math.Min(p.Y, r.Y)) if a.X < b.X {
if p.X < a.X || b.X < p.X {
return false
}
} else if p.X < b.X || a.X < p.X {
return false
}
if a.Y < b.Y {
if p.Y < a.Y || b.Y < p.Y {
return false
}
} else if p.Y < b.Y || a.Y < p.Y {
return false
}
return true
} }
// Creates a Vector pointing to point // Creates a Vector pointing to point