ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Finding the intersection of two lines in polar coordinates

FAQ   •   VIEW ALL Finding the intersection of two lines in polar coordinates on Jul 11, 2012 at 10:28:26 am

I have a few lines connecting points in a comp. These lines are just solids, scaled/positioned/rotated to connect two other layers. I now want to "fill" the space bounded by sets of lines, so I'm using solids which are corner pinned to warp around as the points, and therefore the lines associated with them, move. So each "fill" layer has 8 layer controls associated with it, two per corner, which define which pair of lines detemine the corner's position at their crossing point.

The problem is, to find out what those positions are, I now have a load of (I think) parallel equations, like this. (This would be for the rightmost point on the green diamond shape - where lines 3 and 5 cross.)

``` L1 = thisComp.layer("line3"); L2 = thisComp.layer("line4"); p1 = L1.position; p2 = L2.position; r1 = degreesToRadians(L1.rotation); r2 = degreesToRadians(L2.rotation); // if the crossing point is [x, y]... pseudocode from here on! x = p1 + d1 * Math.cos(r1) = p2 + d2 * Math.cos(r2); y = p1 + d1 * Math.sin(r1) = p2 + d2 * Math.sin(r2); // where d1 and d2 are the distances between p1 and [x,y], and p2 and [x,y], respectively. ```

I know there's probably a neat way of solving these two (basically all I need is to find d1 or d2) but it's years since I had to do this at school (and I was never very good at it anyway) so I'm struggling! Re: Finding the intersection of two lines in polar coordinateson Jul 11, 2012 at 1:07:02 pm

Sorry, the first two lines of that code should've read....

``` L1 = thisComp.layer("line3"); L2 = thisComp.layer("line5"); ``` Re: Finding the intersection of two lines in polar coordinateson Jul 12, 2012 at 1:53:34 pm

This isn't exactly what you're asking for, but it should help. This finds the intersection of two lines where one line is defined by end point layers P1 and P2, and the other line is defined by the end point layers P3 and P4:

``` p1 = thisComp.layer("P1").transform.position; p2 = thisComp.layer("P2").transform.position; p3 = thisComp.layer("P3").transform.position; p4 = thisComp.layer("P4").transform.position; m1vert = Math.abs(p1-p2) < .0001; m2vert = Math.abs(p3-p4) < .0001; if (! m1vert){ m1 = (p2-p1)/(p2-p1); b1 = p1-m1*p1; } if (! m2vert){ m2 = (p4-p3)/(p4-p3); b2 = p3-m2*p3; } if (m1vert && m2vert){ x = value; y = value; }else if(m1vert){ x = p1; y = m2*x + b2; }else if (m2vert){ x = p3; y = m1*x + b1; }else if (m1 == m2){ x = value; y = value; }else{ x = (b1-b2)/(m2-m1); y = m1*x +b1; } [x,y] ```

I haven't tested it thoroughly, but it should be close.

Dan Re: Finding the intersection of two lines in polar coordinateson Jul 12, 2012 at 7:49:37 pm

Sir, as usual, I stand humbled by your comprehensive mathematical awesomeness. Thank you! Re: Finding the intersection of two lines in polar coordinateson Mar 15, 2019 at 5:20:29 pm

You'd saved me man! : )
Thansk a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Re: Finding the intersection of two lines in polar coordinateson Mar 15, 2019 at 5:21:03 pm

You'd saved me man! : )
Thansk a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Re: Finding the intersection of two lines in polar coordinateson Mar 18, 2019 at 12:56:28 pm

We have god os the Sun, God of the Earth, and God of the After Effects Expressions : ) Re: Finding the intersection of two lines in polar coordinateson Aug 2, 2019 at 3:29:01 pm 