Jump to content

Check if a polygon overlaps with a rectangle: Difference between revisions

Added FreeBASIC
m (Added Easylang)
(Added FreeBASIC)
Line 5:
* [[Check_if_two_polygons_overlap|Check if two polygons overlap]]
<br><br>
 
 
 
 
 
Line 531 ⟶ 528:
polyOverlap poly1[][] poly3[][] r ; print r
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|C}}
<syntaxhighlight lang="vbnet">Type Vector2
x As Double
y As Double
End Type
 
Type Projection
min As Double
max As Double
End Type
 
Type Rectangle
x As Double
y As Double
w As Double
h As Double
End Type
 
Function dot(v1 As Vector2, v2 As Vector2) As Double
Return v1.x * v2.x + v1.y * v2.y
End Function
 
Sub getAxes(poly() As Vector2, axes() As Vector2)
Dim As Integer i, j
Dim As Vector2 vector1, vector2, edge
For i = 0 To Ubound(poly)
vector1 = poly(i)
j = Iif((i + 1 = Ubound(poly)+1), 0, i + 1)
vector2 = poly(j)
edge.x = vector1.x - vector2.x
edge.y = vector1.y - vector2.y
axes(i).x = -edge.y
axes(i).y = edge.x
Next i
End Sub
 
Function projectOntoAxis(poly() As Vector2, axis As Vector2) As Projection
Dim As Vector2 vector
Dim As Double min, max, p
vector = poly(0)
min = dot(axis, vector)
max = min
For i As Integer = 1 To Ubound(poly)
vector = poly(i)
p = dot(axis, vector)
If p < min Then
min = p
Elseif p > max Then
max = p
End If
Next i
Return Type<Projection>(min, max)
End Function
 
Function projectionsOverlap(proj1 As Projection, proj2 As Projection) As Boolean
If proj1.max < proj2.min Then Return False
If proj2.max < proj1.min Then Return False
Return True
End Function
 
Sub rectToPolygon(r As Rectangle, poly() As Vector2)
poly(0).x = r.x: poly(0).y = r.y
poly(1).x = r.x: poly(1).y = r.y + r.h
poly(2).x = r.x + r.w: poly(2).y = r.y + r.h
poly(3).x = r.x + r.w: poly(3).y = r.y
End Sub
 
Function polygonOverlapsRect(poly1() As Vector2, rect As Rectangle) As Boolean
Dim As Integer i
Dim As Vector2 axis
Dim As Projection proj1, proj2
Dim As Vector2 poly2(3)
rectToPolygon(rect, poly2())
Dim As Vector2 axes1(Ubound(poly1)), axes2(3)
getAxes(poly1(), axes1())
getAxes(poly2(), axes2())
For i = 0 To Ubound(poly1)
axis = axes1(i)
proj1 = projectOntoAxis(poly1(), axis)
proj2 = projectOntoAxis(poly2(), axis)
If projectionsOverlap(proj1, proj2) = 0 Then Return False
Next i
For i = 0 To Ubound(poly2)
axis = axes2(i)
proj1 = projectOntoAxis(poly1(), axis)
proj2 = projectOntoAxis(poly2(), axis)
If projectionsOverlap(proj1, proj2) = 0 Then Return False
Next i
Return True
End Function
 
Sub printPoly(poly() As Vector2)
Print "{";
For i As Integer = 0 To Ubound(poly)
Print "{" & poly(i).x & ", " & poly(i).y & "}";
If i < Ubound(poly) Then Print ", ";
Next i
Print "}"
End Sub
 
Dim As Vector2 poly(4) = {Type<Vector2>(0, 0), Type<Vector2>(0, 2), Type<Vector2>(1, 4), Type<Vector2>(2, 2), Type<Vector2>(2, 0)}
Dim As Vector2 poly2(3)
Dim As Rectangle rect1 = Type<Rectangle>(4, 0, 2, 2)
Dim As Rectangle rect2 = Type<Rectangle>(1, 0, 8, 2)
 
Print "poly = ";
printPoly(poly())
Print "rect1 = {" & rect1.x & ", " & rect1.y & ", " & rect1.w & ", " & rect1.h & "} => ";
rectToPolygon(rect1, poly2())
printPoly(poly2())
Print "rect2 = {" & rect2.x & ", " & rect2.y & ", " & rect2.w & ", " & rect2.h & "} => ";
rectToPolygon(rect2, poly2())
printPoly(poly2())
Print
Print "poly and rect1 overlap? "; Iif(polygonOverlapsRect(poly(), rect1), "true", "false")
Print "poly and rect2 overlap? "; Iif(polygonOverlapsRect(poly(), rect2), "true", "false")
 
Sleep</syntaxhighlight>
{{out}}
<pre>poly = {{0, 0}, {0, 2}, {1, 4}, {2, 2}, {2, 0}}
rect1 = {4, 0, 2, 2} => {{4, 0}, {4, 2}, {6, 2}, {6, 0}}
rect2 = {1, 0, 8, 2} => {{1, 0}, {1, 2}, {9, 2}, {9, 0}}
 
poly and rect1 overlap? false
poly and rect2 overlap? true</pre>
 
=={{header|Go}}==
2,169

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.