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

(Added C)
Line 264:
poly and rect1 overlap? false
poly and rect2 overlap? true
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
This entry uses the same strategy and the same functions as in [[Check_if_two_polygons_overlap#jq]],
and so the code is not repeated here.
 
The twist here is that we provide a function that allows rectangles to be specified by three vertices.
<syntaxhighlight lang="jq">
def addVector($B): [ .[0] + $B[0], .[1] + $B[1]];
 
def isPerpTo($other): dot($other) == 0;
 
# Input should be three points [A,B,C] where AB is perpendicular to BC
def rectangleToPolygon:
. as [$A, $B, $C]
# check perpendicularity
| if ([$A,$B] | AB | isPerpTo( [$B,$C] | AB)) then .
else "rectangleToPolygon: AB is not perpendicular to BC" | error
end
| . + [$A | addVector( [$B,$C]|AB ) ] ;
def poly1: [[0, 0], [0, 2], [1, 4], [2, 2], [2, 0]];
 
def rect1: [ [4, 0], [4, 2], [6, 2] ];
def rect2: [ [1, 0], [1, 2], [9, 2] ];
 
def task:
([rect1, rect2] | map(rectangleToPolygon)) as [$r1, $r2]
| "poly1 = \(poly1)",
"r1 = \($r1)",
"r2 = \($r2)",
"",
"poly1 and r1 overlap? \(polygonsOverlap(poly1; $r1))",
"poly1 and r2 overlap? \(polygonsOverlap(poly1; $r2))"
;
 
task
</syntaxhighlight>
{{output}}
<pre>
poly1 = [[0,0],[0,2],[1,4],[2,2],[2,0]]
r1 = [[4,0],[4,2],[6,2],[6,0]]
r2 = [[1,0],[1,2],[9,2],[9,0]]
 
poly1 and r1 overlap? false
poly1 and r2 overlap? true
</pre>
 
2,495

edits