Hi,
I've been trying to make a function that finds the distance between two lines (stored as a start point and an end point) in a 2-dimensional space, but I can't seem to be able to find a way of doing it. I managed to figure out a method of getting the distance between a point and a line but when I turn that point into a line I just can't figure it out.
I tried searching both on google and here but the closest thing I could find was the distance between two skew lines in 3d space.
I'll include the code I ended up with for my point-line function (mostly to "prove" that I actually tried before running in here asking for help), it's probably not the best way of doing it but at least it works. I tried modifying this function to get it to support 2 lines instead and also tried starting over, to no avail.
I don't necessarily need the solution, just a nudge in the right direction.
Code:float linePointDistance( Vector3d l1, Vector3d l2, Vector3d p ) { // Get the distances between the start and end point on the line float dx = l2.x - l1.x; float dy = l2.y - l1.y; // Get t, which represents how far along the line we have to move to // get to the closest point. l1 + t[dx,dy] = closest. // (found this by setting the derivative of the distance equal to 0) float t = -((l1.x-p.x)*dx + (l1.y-p.y)*dy) / (dx*dx + dy*dy); // If t isn't >= 0 and <= 1 then the closest point is outside of the // line, we therefore use the end or start point instead. Vector3d usevector; if( t > 1 ) usevector = l2; else if( t < 0 ) usevector = l1; else usevector = l1+((l2-l1)*t); return (usevector-p).magnitude(); }