Help regarding line intersection from "General Math"

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
NoCom
Prole
Posts: 2
Joined: Sun Jun 04, 2017 5:14 pm

Help regarding line intersection from "General Math"

Post by NoCom »

I'm doing something that requires checking line intersections. Being a lazy sob, I'm using this code from the General math wiki page:

Code: Select all

-- Checks if two lines intersect (or line segments if seg is true)
-- Lines are given as four numbers (two coordinates)
function findIntersect(l1p1x,l1p1y, l1p2x,l1p2y, l2p1x,l2p1y, l2p2x,l2p2y, seg1, seg2)
	local a1,b1,a2,b2 = l1p2y-l1p1y, l1p1x-l1p2x, l2p2y-l2p1y, l2p1x-l2p2x
	local c1,c2 = a1*l1p1x+b1*l1p1y, a2*l2p1x+b2*l2p1y
	local det,x,y = a1*b2 - a2*b1
	if det==0 then return false, "The lines are parallel." end
	x,y = (b2*c1-b1*c2)/det, (a1*c2-a2*c1)/det
	if seg1 or seg2 then
		local min,max = math.min, math.max
		if seg1 and not (min(l1p1x,l1p2x) <= x and x <= max(l1p1x,l1p2x) and min(l1p1y,l1p2y) <= y and y <= max(l1p1y,l1p2y)) or
		   seg2 and not (min(l2p1x,l2p2x) <= x and x <= max(l2p1x,l2p2x) and min(l2p1y,l2p2y) <= y and y <= max(l2p1y,l2p2y)) then
			return false, "The lines don't intersect."
		end
	end
	return x,y
end
This is MOSTLY working, but I've ran into some troubles. I've got cases where I absolutely KNOW two line segments intersect, but the algorithm above returns false with "The lines don't intersect.".

Trying to debug the thing, I'm printing out the coordinates of lines i'm testing with. Take this case where I'm calling the function with:

Code: Select all

l1p1x = 255.9
l1p1y = 600
l1p2x = 105.9
l1p2y = 0
l2p1x = 267.8
l2p1y = 400
l2p2x = 100
l2p2y = 400
seg1 = true
seg2 = true
In which line 1 is a slightly diagonal line running from the bottom of the screen to the top of the screen and line 2 is a horizontal line which does seem to lie on the path of the first one. Still... I'm getting "not intersecting".

Does anyone have a clue into what's happening? Is the algorithm somehow expecting points or lines in some certain direction/order perhaps?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Help regarding line intersection from "General Math"

Post by davisdude »

This function is working for me, so I'm not sure what's going on for you...
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
NoCom
Prole
Posts: 2
Joined: Sun Jun 04, 2017 5:14 pm

Re: Help regarding line intersection from "General Math"

Post by NoCom »

Phew. This had me really stumped but I managed to solve the issue by using the slightly altered function in this thread.

The problem seemed to be in the fact that one of the crossing lines was perfectly horizontal. For some scientific reason this introduces possibility for error due to floating point precision issues. (Or that's what I gathered from the linked thread).
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 221 guests