# Line Line Intersection

2 replies to this topic

### #1 zachman

zachman

MagicMan Productions

• New Member
• 212 posts

Posted 03 November 2010 - 04:31 AM

Here's a little script I wrote for a project I was working on. Someone has probably posted this somewhere already, but you never know... and I'm to lazy to search to see if someone already has

Here's how it works. The script checks for a collision between two lines through the line segments of x1,y1 to x2,y2 and x3,y3 to x4,y4. The script then returns the x and y chords of the intersection between those two lines.

Remember that a line continues infinitely in either direction, and this script checks lines not line segments. This means that it can return a intersection beyond the lengths of the line segments given in the arguments.

There are two scripts, one returning the x choord, the other returning the y. They both take the exact same arguments. The script also assumes there is a collision. So this will not work for parallel or coinciding lines. Well, at least they're untested. The scripts are not meant to check if there is a collision, but rather where the collision happens.

linelineintersection_x()
```/* LineLineIntersection_X(x1,y1,x2,y2,x3,y3,x4,y4)

Calculates the x coordinate of the intersection point of two lines from x1,y1 to x2,y2 and x3,y3 to x4,y4
This script assumes that there is an intersection, and the lines are not parallel or coinciding.

Remember that a line extends infinetly in either direction, unlike a line segment. So this script will treat
the segments given in the arguments as lines, so even if there would be no intersection between the segments,
this script will still return an intersection between the lines.
*/

return ((argument0*argument3-argument1*argument2)*(argument4-argument6)-(argument0-argument2)*(argument4*argument7-argument5*argument6))/((argument0-argument2)*(argument5-argument7)-(argument1-argument3)*(argument4-argument6))```
linelineintersection_y()
```/* LineLineIntersection_Y(x1,y1,x2,y2,x3,y3,x4,y4)

Calculates the y coordinate of the intersection point of two lines from x1,y1 to x2,y2 and x3,y3 to x4,y4
This script assumes that there is an intersection, and the lines are not parallel or coinciding.

Remember that a line extends infinetly in either direction, unlike a line segment. So this script will treat
the segments given in the arguments as lines, so even if there would be no intersection between the segments,
this script will still return an intersection between the lines.
*/

return ((argument0*argument3-argument1*argument2)*(argument5-argument7)-(argument1-argument3)*(argument4*argument7-argument5*argument6))/((argument0-argument2)*(argument5-argument7)-(argument1-argument3)*(argument4-argument6))
```

Hope someone finds a use for them
~Zach
• 1

### #2 Maerron

Maerron

GMC Member

• GMC Member
• 90 posts
• Version:GM:Studio

Posted 23 March 2012 - 05:30 AM

Good script, used to solve my critical problem . Though I just combined the script into one and modified them to check the denominator first to prevent 'divide by zero' error.

Edited by Maerron, 23 March 2012 - 05:32 AM.

• 0

### #3 brac37

brac37

GMC Member

• GMC Member
• 808 posts
• Version:GM7

Posted 23 March 2012 - 08:47 PM

A though problem, but solved many times already. I would suggest to select the absolutely largest denominator instead of only checking for division by zero, for stability with respect to rounding errors.
• 0