Possibly I am asking the wrong question but…

Does anyone have a lua function for line intersection?

I can neither find nor easily build one. I keep finding too many special cases.

Preferably it would return nil, the point, or the line of intersection.

This http://paulbourke.net/geometry/lineline2d/ didn’t seem to handle everything (if my version was correct).

Assuming you know the slope/intercept of the 2 lines I think this should do it…

`function intersects(m1, c1, m2, c2) if m1 == m2 then if c1 == c2 then return {m1,c1,"line"} end return nil end x = (c2 - c1) / (m1 - m2) y = m1 * x + c1 return {x,y,"point"} `

end

I’m having issues since there line segments. |- would intersect but - isn’t long enough.

This seems to work… True or false only though, it doesn’t return the point.

Thx, that’s working much better than what I had. I’ll post something when I get a chance.

The formula doesn’t handle when the lines are in the same plane, or the same line etc. This is the everything is 0 case in the original function.

I added those special cases ( possibly there is a shorter solution possibly woth math alone but there are many cases )

It really helped starting with the formula then handling the special cases.

I was doing it the other way around originally.

Yea, I just discovered that, unfortuately all the tutorials and code samples I found ignored that and just returned false as soon as the lines tested as parallel.

I started the min-max thing that you are doing in your code but decided there must be a better way to do it, I’m wandering whether bumping one of the lines out by a pixel and recalculating might be a ‘good enough’ approach, then you only have 2 cases to worry about - exactly vertical (bump out an x coordinate) or not (bump out a y coordinate).

I haven’t had a chance to write any code yet though and I’m no math expert so there might be reasons why thats a terrible idea…

Ok, I think I have a solution. I used the voiceoftreason code and I modified a bit to get the point of intersection of a pair of lines. Here’s the code: http://pastebin.com/aF2qxWTP