This is a failsafe code, I use it all the time
E_Vertex is a structure with x,y,z coords
E_Line is a structure with two E_Vertex structures (for two points that define the line)
EPS is an epsilon value for the lowest non-zero real value (just do something like #define EPS 0.000000001)
///////////////////////////////////////////
E_Vertex TwoLine(E_Line a, E_Line b)
{
E_Vertex p13,p43,p21;
double d1343,d4321,d1321,d4343,d2121;
double numer,denom, mua, mub;
E_Vertex pa, pb;
E_Vertex error;
error.x=error.y=error.z=-0.69f;
p13.x = a.a.x - b.a.x;
p13.y = a.a.y - b.a.y;
p13.z = a.a.z - b.a.z;
p43.x = b.b.x - b.a.x;
p43.y = b.b.y - b.a.y;
p43.z = b.b.z - b.a.z;
// if (abs((int)(p43.x)) < EPS && abs((int)(p43.y)) < EPS && abs((int)(p43.z)) < EPS)
// return error;
p21.x = a.b.x - a.a.x;
p21.y = a.b.y - a.a.y;
p21.z = a.b.z - a.a.z;
// if (abs((int)(p21.x)) < EPS && abs((int)(p21.y)) < EPS && abs((int)(p21.z)) < EPS)
// return error;
d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
denom = d2121 * d4343 - d4321 * d4321;
if (denom == 0)
return error;
numer = d1343 * d4321 - d1321 * d4343;
mua = numer / denom;
mub = (d1343 + d4321 * (mua)) / d4343;
pa.x = (GLfloat)(a.a.x + mua * p21.x);
pa.y = (GLfloat)(a.a.y + mua * p21.y);
pa.z = (GLfloat)(a.a.z + mua * p21.z);
pb.x = (GLfloat)(b.a.x + mub * p43.x);
pb.y = (GLfloat)(b.a.y + mub * p43.y);
pb.z = (GLfloat)(b.a.z + mub * p43.z);
if (pa.Dist(pb)<0.3f)
return(pa);
else
return error;
}
/////////////////////////////////////////
Returns the point of intersection E_Vertex structure if lines intersect and an error E_Vertex structure (-0.69,-0.69,-0.69) is they don’t… you can modify it to return a boolean.
pa.Dist(pb)<0.3f tests the closest distance between the two lines. In this case if the distance is less than 0.3f then the lines intersect, otherwise they don’t.
Hope this helps,
good luck