Check if two polygons overlap: Difference between revisions

(→‎{{header|jq}}: 2D convex polygons)
Line 242:
 
=={{header|jq}}==
'''Adapted from [[#Wren|WrebWren]]''' (2D convex polygons)
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
Line 251:
* a projection is represented by a JSON object {min, max}
<syntaxhighlight lang="jq">
# Input: [$A, $B] where $A and $B are points
# Output: the vector $B - $A
def AB:
. as [$A, $B]
| [ $B[0] - $A[0], $B[1] - $A[1]];
 
# Input: a vector
# Output: perpendicular
Line 265 ⟶ 271:
$poly[$i] as $vertex1
| $poly[if $i+1 == ($poly|length) then 0 else $i+1 end] as $vertex2
| . + [ [$vertex1[0] -, $vertex2[0], $vertex1[1]| -AB $vertex2[1]| perp] as $edge);
| . + [$edge | perp]);
 
# emit {min, max}
2,502

edits