Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Finding the intersection of two lines in polar coordinates

  • Finding the intersection of two lines in polar coordinates

    Posted by Nick Hill on July 11, 2012 at 10:28 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[0] + d1 * Math.cos(r1) = p2[0] + d2 * Math.cos(r2);
    y = p1[1] + d1 * Math.sin(r1) = p2[1] + 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!

    Sveta Bogomolova replied 6 years ago 5 Members · 10 Replies
  • 10 Replies
  • Nick Hill

    July 11, 2012 at 1:07 pm

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


    L1 = thisComp.layer("line3");
    L2 = thisComp.layer("line5");

  • Dan Ebberts

    July 12, 2012 at 1:53 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[0]-p2[0]) < .0001;
    m2vert = Math.abs(p3[0]-p4[0]) < .0001;
    if (! m1vert){
    m1 = (p2[1]-p1[1])/(p2[0]-p1[0]);
    b1 = p1[1]-m1*p1[0];
    }
    if (! m2vert){
    m2 = (p4[1]-p3[1])/(p4[0]-p3[0]);
    b2 = p3[1]-m2*p3[0];
    }
    if (m1vert && m2vert){
    x = value[0];
    y = value[1];
    }else if(m1vert){
    x = p1[0];
    y = m2*x + b2;
    }else if (m2vert){
    x = p3[0];
    y = m1*x + b1;
    }else if (m1 == m2){
    x = value[0];
    y = value[1];
    }else{
    x = (b1-b2)/(m2-m1);
    y = m1*x +b1;
    }
    [x,y]

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

    Dan

  • Nick Hill

    July 12, 2012 at 7:49 pm

    Sir, as usual, I stand humbled by your comprehensive mathematical awesomeness. Thank you!

  • Mikhail Vasilev

    March 15, 2019 at 5:20 pm

    You’d saved me man! : )
    Thansk a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Mikhail Vasilev

    March 15, 2019 at 5:21 pm

    You’d saved me man! : )
    Thansk a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Mikhail Vasilev

    March 18, 2019 at 12:56 pm

    We have god os the Sun, God of the Earth, and God of the After Effects Expressions : )

  • Albert Kowalski

    August 2, 2019 at 3:29 pm

    Hi! Just refreshing this old post ☺

    My case is pretty similar, but I really can’t edit the code so it works. I have two shapes: SHAPE1 and SHAPE2. One is a straight line, second is bend line. Both of those line have two point, at the beginning and at the end. There is also a layer called OBJECT. What I want to achieve is to make the object follow the intersection of those two shapes. So whenever I move one of the shapes, the object follow cross point of those shapes. Here’s how it should look (now I just moved the object manually).

    Is there a way to edit this code so it fits my need? Thanks!

  • Sveta Bogomolova

    April 25, 2020 at 2:07 pm

    I wondered did you ever find an answer to your question? I have exactly the same problem. The above code will not work because it does not detect intersection, it is calculating when it is happening. But shape layer paths do not have start and end points. I have no idea how to solve this current situation though! I was thinking to make both shape layers say 50% and use sampleImage to detect if the result is 100%, but no, sampleImage look at a fixed point, not the whole comp. Any ideas?

  • Albert Kowalski

    April 25, 2020 at 4:24 pm

    Unfortunately I haven’t found a solution 🙁

  • Sveta Bogomolova

    April 26, 2020 at 3:03 am

    May be the new Cavalry app is good for this task:)

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy