bunch of probably irrelevant fenceposting

This commit is contained in:
Josh Deprez 2021-09-08 17:37:10 +10:00
parent 1af3e141fb
commit a1abd8ac3c

View file

@ -92,20 +92,20 @@ func polygonRectOverlap(polygon []image.Point, rect image.Rectangle) bool {
}
}
// Reduce Max to the inclusive bound.
rect.Max = rect.Max.Sub(image.Pt(1, 1))
// Reduced Max (to the inclusive bound).
rmax := rect.Max.Sub(image.Pt(1, 1))
// Check if any vertex of the rect is inside the polygon.
if polygonContains(polygon, rect.Min) {
return true
}
if polygonContains(polygon, rect.Max) {
if polygonContains(polygon, rmax) {
return true
}
if polygonContains(polygon, image.Pt(rect.Min.X, rect.Max.Y)) {
if polygonContains(polygon, image.Pt(rect.Min.X, rmax.Y)) {
return true
}
if polygonContains(polygon, image.Pt(rect.Max.X, rect.Min.Y)) {
if polygonContains(polygon, image.Pt(rmax.X, rect.Min.Y)) {
return true
}
@ -116,6 +116,11 @@ func polygonRectOverlap(polygon []image.Point, rect image.Rectangle) bool {
// Walk each edge of polygon.
for i, p := range polygon {
q := polygon[(i+1)%len(polygon)]
// Exclude edges that don't overlap the rectangle in any way.
if !rect.Overlaps(image.Rectangle{p, q}.Canon()) {
continue
}
d := q.Sub(p)
// If the polygon edge is not vertical, test left and right sides
if d.X != 0 {
@ -125,17 +130,17 @@ func polygonRectOverlap(polygon []image.Point, rect image.Rectangle) bool {
min := (rect.Min.Y - p.Y) * d.X
max := (rect.Max.Y - p.Y) * d.X
// Test left side of rect
if (rect.Min.X >= p.X || rect.Min.X >= q.X) && (rect.Min.X <= p.X || rect.Min.X <= q.X) {
if t := (rect.Min.X - p.X) * d.Y; min <= t && t <= max {
return true
}
//if (rect.Min.X >= p.X || rect.Min.X >= q.X) && (rect.Min.X <= p.X || rect.Min.X <= q.X) {
if t := (rect.Min.X - p.X) * d.Y; min <= t && t < max {
return true
}
//}
// Test right side of rect
if (rect.Max.X >= p.X || rect.Max.X >= q.X) && (rect.Max.X <= p.X || rect.Max.X <= q.X) {
if t := (rect.Max.X - p.X) * d.Y; min <= t && t <= max {
return true
}
//if (rect.Max.X > p.X || rect.Max.X > q.X) && (rect.Max.X <= p.X || rect.Max.X <= q.X) {
if t := (rmax.X - p.X) * d.Y; min <= t && t < max {
return true
}
//}
}
// If the polygon edge is not horizontal, test the top and bottom sides
if d.Y != 0 {
@ -145,18 +150,18 @@ func polygonRectOverlap(polygon []image.Point, rect image.Rectangle) bool {
min := (rect.Min.X - p.X) * d.Y
max := (rect.Max.X - p.X) * d.Y
// Test top side of rect
if (rect.Min.Y >= p.Y && rect.Min.Y >= q.Y) && (rect.Min.Y <= p.Y && rect.Min.Y <= q.Y) {
if t := (rect.Min.Y - p.Y) * d.X; min <= t && t <= max {
return true
}
//if (rect.Min.Y >= p.Y || rect.Min.Y >= q.Y) && (rect.Min.Y <= p.Y || rect.Min.Y <= q.Y) {
if t := (rect.Min.Y - p.Y) * d.X; min <= t && t < max {
return true
}
//}
// Test bottom side of rect
if (rect.Max.Y >= p.Y && rect.Max.Y >= q.Y) && (rect.Max.Y <= p.Y && rect.Max.Y <= q.Y) {
if t := (rect.Max.Y - p.Y) * d.X; min <= t && t <= max {
return true
}
//if (rect.Max.Y > p.Y || rect.Max.Y > q.Y) && (rect.Max.Y <= p.Y || rect.Max.Y <= q.Y) {
if t := (rmax.Y - p.Y) * d.X; min <= t && t < max {
return true
}
//}
}
}
return false