How to test if to lines intersect

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

How to test if to lines intersect

Post by Purple Fedora » Sat Sep 14, 2019 7:04 pm

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

User avatar
4vZEROv
Prole
Posts: 46
Joined: Wed Jan 02, 2019 8:44 pm

Re: How to test if to lines intersect

Post by 4vZEROv » Sun Sep 15, 2019 2:52 pm

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

User avatar
ivan
Party member
Posts: 1511
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to test if to lines intersect

Post by ivan » Mon Sep 16, 2019 5:22 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests