Search ProofOfProgress Blog

Saturday, April 23, 2011

Intersection of two vectors using point normal form

I am pretty proud of this. Here is the intersection point of 2 vectors (2D).
It would be pretty easy to convert this to 3D, which I am going to do next.
But I originally needed it for 2D intersections in actionscript.

[EDIT: Lines rarely intersect in 3D. Formula does not translate.]
[I feel kinda stupid now.]

Yes... MaxScripting code to prototype actionScript code.
This being because it is very easy to visually debug geometric formulas
in 3dsmax. So its actually quicker for me to write maxscript and actionscript
than just actionscript.

Here is the formula, as a maxscript function:


function pointNormalIntersect2D
oA --Origin A
oB --Origin B
vA --Vector #1.
vB --Vector #2.
&P --position output
=(

nA = normalize(vA);
nB = normalize(vB);

--oA --origin A
--nA --normal A
--sA --scalar A
--oB --origin B
--nB --normal B
--sB --scalar B
--oA+(nA*sA)==oB+(nB*sB);
--oA.x+(nA.x*sA)==oB.x+(nB.x*sB); //MULTIPLY BY nB.y
--oA.y+(nA.y*sA)==oB.y+(nB.y*sB); //MULTIPLY BY nB.x
--
--(nB.y*oA.x)+(nB.y*nA.x*sA)==(nB.y*oB.x)+(nB.y*nB.x*sB);
--(nB.x*oA.y)+(nB.x*nA.y*sA)==(nB.x*oB.y)+(nB.x*nB.y*sB);
--[ c1 ] [ c2 ] [ c3 ] [ c4 ]
--
--MINUS THEM: c4 cancels out.
c1 = (nB.y*oA.x)- (nB.x*oA.y);
--c2 = (nB.y*nA.x*sA)-(nB.x*nA.y*sA);
c3 = (nB.y*oB.x)- (nB.x*oB.y);

--New Equation is:
--c2 = c3 - c1;
c3c1 = c3 - c1; --c3 minus c1;

--Figure out how many sA's you have by combining like terms;
sAcount = (nB.y*nA.x)-(nB.x*nA.y);

--New Equation is:
--sAcount*sA = c3c1;

--calculate sA:
sA = c3c1/sAcount;

--once scalar sA is found, plug it into point normal form to find intersection.
P=[0,0,0];
P.x = oA.x+(nA.x*sA);
P.y = oA.y+(nA.y*sA);

)--[FN:pointNormalIntersect]

oA = $v1A.pos;
oB = $v2A.pos;
vA = $v1A.pos - $v1B.pos;
vB = $v2A.pos - $v2B.pos;
P = [0,0,0];
pointNormalIntersect2D oA oB vA vB &P;
$intPos.pos = P;

No comments:

Post a Comment