Skip to content

Instantly share code, notes, and snippets.

@CoronaSDK
Created March 29, 2013 04:39
Show Gist options
  • Save CoronaSDK/5268780 to your computer and use it in GitHub Desktop.
Save CoronaSDK/5268780 to your computer and use it in GitHub Desktop.
Intersection of 2 lines for a Corona SDK Application
function intersection(p11,p12,p21,p22)
local Z = (p12.y-p11.y)*(p21.x-p22.x)-(p21.y-p22.y)*(p12.x-p11.x);
local Ca = (p12.y-p11.y)*(p21.x-p11.x)-(p21.y-p11.y)*(p12.x-p11.x);
local Cb = (p21.y-p11.y)*(p21.x-p22.x)-(p21.y-p22.y)*(p21.x-p11.x);
if Z == 0 and Ca == 0 and Cb == 0 then return nil end
if Z == 0 then return nil end
local Ua = Ca/Z;
local Ub = Cb/Z;
local pt = {}
pt.x = p11.x + (p12.x - p11.x) * Ub;
pt.y = p11.y + (p12.y - p11.y) * Ub;
if 0 <= Ua and Ua <= 1 and 0 <= Ub and Ub <= 1 then
-- ( (Ua == 0)||(Ua == 1)||(Ub == 0)||(Ub == 1) ) ?
-- result.type = ctOnBounds :
-- result.type = ctInBounds;
else
return nil
end
return pt
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment