Check if two polygons overlap: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: 2D convex polygons)
Line 242: Line 242:


=={{header|jq}}==
=={{header|jq}}==
'''Adapted from [[#Wren|Wreb]]''' (2D convex polygons)
'''Adapted from [[#Wren|Wren]]''' (2D convex polygons)
{{works with|jq}}
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
'''Also works with gojq, the Go implementation of jq'''
Line 251: Line 251:
* a projection is represented by a JSON object {min, max}
* a projection is represented by a JSON object {min, max}
<syntaxhighlight lang="jq">
<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
# Input: a vector
# Output: perpendicular
# Output: perpendicular
Line 265: Line 271:
$poly[$i] as $vertex1
$poly[$i] as $vertex1
| $poly[if $i+1 == ($poly|length) then 0 else $i+1 end] as $vertex2
| $poly[if $i+1 == ($poly|length) then 0 else $i+1 end] as $vertex2
| [$vertex1[0] - $vertex2[0], $vertex1[1] - $vertex2[1]] as $edge
| . + [ [$vertex1, $vertex2] | AB | perp] );
| . + [$edge | perp]);


# emit {min, max}
# emit {min, max}