Check if a polygon overlaps with a rectangle: Difference between revisions
Check if a polygon overlaps with a rectangle (view source)
Revision as of 17:43, 23 October 2023
, 7 months ago→{{header|Julia}}: cleanup
m (→{{header|Julia}}: cleanup) |
|||
Line 479:
function axes(poly::Polygon)
result = [(0.0, 0.0)
▲ for (i, vertex1) in enumerate(poly)
▲ vertex2 = i == plen ? first(poly) : poly[i+1] # wraps around
▲ edge = (vertex1[1] - vertex2[1], vertex1[2] - vertex2[2])
end▼
▲ result[i] = (-edge[2], edge[1])
▲ return result
end
function projectiononaxis(poly::Polygon, axis::Vector2)
resultmin = Inf▼
for vertex in poly
▲ resultmax = -Inf
p =
end▼
return
▲ resultmax = p
end
projectionoverlaps(p1::Projection, p2::Projection) = p1[2] <= p2[1] && p2[2] >= p1[1]
Polygon(r::Rectangle)
▲ return [(r.x, r.y), (r.x, r.y + r.h), (r.x + r.w, r.y + r.h), (r.x + r.w, r.y)]
▲end
function polygonoverlapsrect(
return
▲ if projectionoverlaps(projectiononaxis(poly1, axis), projectiononaxis(poly2, axis))
end
const poly = [(0.0, 0.0), (0.0, 2.0), (1.0, 4.0), (2.0, 2.0), (2.0, 0.0)]
const
const
println("poly = a polygon with vertices: ", poly)
▲end
</syntaxhighlight>{{out}}
<pre>
Line 540 ⟶ 524:
poly and rect1 overlap? false
poly and rect2 overlap? true
</pre>
|