## How to test if to lines intersect

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Purple Fedora
Prole
Posts: 23
Joined: Fri Oct 26, 2018 8:41 pm

### How to test if to lines intersect

I have 2 points and an line in between them.
I now the X and Y coordinates of the Points.
How can i test if the lines? Is there an Formular?

TIA

4vZEROv
Citizen
Posts: 93
Joined: Wed Jan 02, 2019 8:44 pm

### Re: How to test if to lines intersect

Learn how to use google mate.

Everything you need for 2d collision detection is here :
http://www.jeffreythompson.org/collisio ... ntents.php

And since i'm such a good person :

Code: Select all

function line_line(x1, y1, x2, y2, x3, y3, x4, y4)
local uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
local uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
return uA >= 0 and uA <= 1 and uB >= 0 and uB <= 1
end


ivan
Party member
Posts: 1599
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: How to test if to lines intersect

The code above could be optimized using "early bailout" as described in my tutorial:
https://2dengine.com/?p=intersections#S ... vs_segment
Furthermore it shows how to find the point of intersection.

### Who is online

Users browsing this forum: No registered users and 6 guests