ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Detect if Lines are Intersecting

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Valenin Febeldorfer
Detect if Lines are Intersecting
on Jan 3, 2019 at 10:41:18 am

I have two lines (The lines are imaginary, i have two Nulls as endpoints for each line) I want a fifth Null two appear where the lines intersect. I have used this code so far:


p1 = thisComp.layer("red_end").transform.position;
p2 = thisComp.layer("red_start").transform.position;
p3 = thisComp.layer("blue_end").transform.position;
p4 = thisComp.layer("blue_start").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]

The fifth Null does appear in the intersections when the lines intersect, but also miserably stumble around on screen when the lines don't intersect. I want the Null to go to a specific position whenever the lines don't intersect. So how would I do that? Any help is appreciated.


Return to posts index

Kalleheikki Kannisto
Re: Detect if Lines are Intersecting
on Jan 3, 2019 at 6:34:48 pm

Add a section that checks whether the intersection point is within both rectangles defined by the start and end point of blue and red lines respectively. If the point is within both those rectangles, it gives you the intersection point and should be shown there, if not, set it to your fixed position.
It should be easy to check that the x coordinate falls between the x coordinates of the end points and y coordinate falls between the y coordinates of the end points, one line at a time. You will have to check which point has the smaller coordinate value in each pair in both x and y directions to know in which order to check it.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]