# Get coordinates where two lines intersect

Hello,

I have two lines that are drawn with sops. I want to get the coordinates from the point where the two lines cross each other. So I can draw a new line from where the lines intersect. I’ve been trying to figure out how to achieve this, but I can’t seem to work it out.
Does anyone know how I can achieve this?

Hello @Okke,
long time ago I have implemented this using VEX in Houdini. I am not sure if it might help you somehow, but maybe it could serve as a good starting point for implementation in TouchDesigner.

``````// ! function writes values to pos, state, mag1 and mag2
// calculate intersection point of two lines and write its position to pos (if there is no intersection the function returns the closest point on vec1 to vec2)
// state -1 means lines are parallel
// state 0 means no intersection
// state 1 means there is intersection on both lines
// state 2 means there is intersection on infinite extension of one (or both) lines
// mag1 is magnitude of vec1 which represents intersection point on first line
// mag2 is magnitude of vec2 which represents intersection point on second line
void line_intersect(vector pos1; vector pos2; vector vec1; vector vec2; vector pos; int state; float mag1; float mag2)
{
state = 0;
vector vec = cross(vec1, vec2);

if ( vec == 0 ){
// lines are parallel to each other
// magnutides and intersection positions are not calculated in this case (!division by zero)
state = -1;
}
else{
vector line = pos1 - pos2;
float val1a = dot(vec, cross(vec2, line));
float val1b = dot(vec, cross(vec1, line));
// following dot product basically stands for ||vec|| ^ 2 (since cos of two parallel vectors is 1)
float val2 = dot(vec, vec);

// calculate magnitude for line vectors that represent intersection point on each line
mag1 = val1a / val2;
mag2 = val1b / val2;
pos = pos1 + mag1 * vec1;

if ( dot(normalize(vec), normalize(cross(vec2, line))) >= 1 - 1e-6 )
{
// intersection point lies either on lines or their infinite extensions
state = 2;
if (mag1 <= 1 && mag1 >= 0 && mag2 <= 1 && mag2 >= 0)
{
// intersection point lies on *both* lines
state = 1;
}
}
}
}
``````
1 Like

hi there,

This is a bit more complex than you might initial think, at least that was for me when I first encountered it :).
I’m not sure if you’re talking about 2d lines intersection or trying to intersect 3d lines. 3d lines only intersect if they both share a plane and so you would rather need to use calculus to find the closest distance of the lines even if they dont intersect (which is with random 3d lines almost always the case)

For 2d lines it’s possible to calculate the intersection lines but involves some linear algebra.
If you have a line1 with equation line1(t) = o1 + d1 t, o1 being the origin/source of the line1, d1 being the normalized direction of the line1 (vector with length 1) and ‘t’ being some parameter describing where you are on the line (t = 0, is the origin, t = length(line1) will be the end of the line)

Now the goal is the find ‘t’ where line2 is intersecting line1. You can’t really use line2(t) = o2+d2t, and solve for ‘t’, since this ‘t’ would be different from the line1(t). So what you can do is use a different equation to see if a point is on a line by measuring the distance from that point to the line.
This can be done using dot products: -dot(o1 - o2, n2)/dot(d1,n2). where ‘n2’ is the normal of line2 (the normalized vector perpendicular to the direction of the line). This might seem hard to understand if you never worked with dot products. It’s a way to project a vector onto another vector and find its length. Quite powerful mathematical tool :). (check out 3blue1brown linear algebra course on youtube)
So the dot product equation gives the ‘t’, then the intersection point is o1 + d1 t.

Might not be the best explanation so I made an example in chops & glsl to show the implementation of this math stuff.

Hope it helps!
cheers,
tim

LineIntersect.tox (3.7 KB)

3 Likes

There’s an old SOP I think that deals with lines crossings if you don’t need too many intersections in real-time, I forgot the name. Not on TD at the moment, Cheers! P