alias MslInPoly {
;=== setup variables and complete polygon
var %cn = 0 , %x = $1 , %y = $2 , %size = $0 , %poly = $3- $3-4
if (%size > 2) {
var %i = 0 , %j = %size - 2
while (%i < %j) {
var %ax = $gettok(%poly,$calc(%i + 1),32) , %ay = $gettok(%poly,$calc(%i + 2),32) , %bx = $gettok(%poly,$calc(%i + 3),32) , %by = $gettok(%poly,$calc(%i + 4),32)
;=== Condition1 = Upward Crossing, Condition2 = downward crossing
if (%ay <= %y && %by > %y) || (%ay > %y && %by <= %y) {
;=== Compute the actual edge-ray intersect x-coordinage
var %vt = $calc((%y - %ay) / (%by - %ay))
;=== Test for valid crossing
if (%x < $calc(%ax + %vt * (%bx - %ax))) { inc %cn }
}
inc %i 2
}
}
var %cn = $and(%cn,1)
if (%cn > 0) { return $true }
}
alias MslOnPoly {
;=== Setup variables
var %s1 = $1 , %s2 = $2 , %p1e = $1 * 2 , %p1 = $gettok($3-,$+(1-,%p1e),32) , %p2 = $gettok($3-,$+($calc(%p1e + 1),-),32)
;=== Early escape test, if either polygon is fully contained in the other, any and every point will be inside the other. Knowing this, we don't need to test them all, just one.
var %p1x = $gettok(%p1,1,32) , %p1y = $gettok(%p1,2,32) , %p2x = $gettok(%p2,1,32) , %p2y = $gettok(%p2,2,32)
if ($mslInPoly(%p1x,%p1y, [ $regsubex(%p2,/ /g,$chr(44)) ] ) || $mSLInPoly(%p2x,%p2y, [ $regsubex(%p1,/ /g,$chr(44)) ] )) { return $true }
;=== Complete the polygons
var %p1 = %p1 $gettok(%p1,1-2,32) , %p2 = %p2 $gettok(%p2,1-2,32)
;=== Else: Iterate line segments and check for intersection
var %i = 0
while ($calc(%i / 2) < %s1) {
var %p1ax = $gettok(%p1,$calc(%i + 1),32) , %p1ay = $gettok(%p1,$calc(%i + 2),32)
var %p1bx = $gettok(%p1,$calc(%i + 3),32) , %p1by = $gettok(%p1,$calc(%i + 4),32)
var %j = 0
while ($calc(%j / 2) < %s2) {
var %p2ax = $gettok(%p2,$calc(%j + 1),32) , %p2ay = $gettok(%p2,$calc(%j + 2),32)
var %p2bx = $gettok(%p2,$calc(%j + 3),32) , %p2by = $gettok(%p2,$calc(%j + 4),32)
;=== LineSeg to LineSeg intersection test
if ($intersect(%p1ax,%p1ay,%p1bx,%p1by,%p2ax,%p2ay,%p2bx,%p2by,ll)) { return $true }
inc %j 2
}
inc %i 2
}
}