-
-
Save pdstuff/0b6c764f32996803ea51595c6c7e156c to your computer and use it in GitHub Desktop.
fast SAT lua poly:poly for playdate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function polygonsIntersect(p1, p2) -- tables of x1, y1, x2, y2, ... poly vertices | |
local huge = math.huge | |
for i = 1, #p1 - 2, 2 do | |
local dx, dy = p1[i+2] - p1[i], p1[i+3] - p1[i+1] | |
local minA, maxA, minB, maxB = huge, -huge, huge, -huge | |
for j = 1, #p1 - 2, 2 do | |
local proj = p1[j] * -dy + p1[j+1] * dx | |
if proj < minA then minA = proj else if proj > maxA then maxA = proj end end | |
end | |
for j = 1, #p2 - 2, 2 do | |
local proj = p2[j] * -dy + p2[j+1] * dx | |
if proj < minB then minB = proj else if proj > maxB then maxB = proj end end | |
end | |
if maxA < minB or maxB < minA then return false end | |
end | |
for i = 1, #p2 - 2, 2 do | |
local dx,dy = p2[i+2] - p2[i], p1[i+3] - p1[i+1] | |
local minA, maxA, minB, maxB = huge, -huge, huge, -huge | |
for j = 1, #p1 - 2, 2 do | |
local proj = p1[j] * -dy + p1[j+1] * dx | |
if proj < minA then minA = proj else if proj > maxA then maxA = proj end end | |
end | |
for j = 1, #p2 - 2, 2 do | |
local proj = p2[j] * -dy + p2[j+1] * dx | |
if proj < minB then minB = proj else if proj > maxB then maxB = proj end end | |
end | |
if maxA < minB or maxB < minA then return false end | |
end | |
return true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment