Original Post
To make a short story even shorter, this code doesn''t work the way I''d like it to. It''s supposed to tell me if two lines intersect (and if they intersect immediatly)
I see two possible problems. One is that I simply have a bad algorithim. The other is that the code that checks to see if the collision point is on one of the lines. Either way, I don''t know how to efficiently fix them.
Here''s the code:
COORDVAR Line_Sect(COORDVAR c1, COORDVAR c2, COORDVAR c3, COORDVAR c4)
{
//function designed to give the intersection point of two lines.
//Lines that eventually would intersect if infinite cause a to equal 0, otherwise its 1.
float b1, b2; //The slope of each line
float a1, a2; //The adjustment each line needs
COORDVAR ReturnInfo; //The information that will be returned by the function
//This whole algorith runs off sytems of equations and the fact that any straight lines can
//be written as y = a + (b * x)
if(c1.x - c2.x == 0)
c2.x += .001;
if(c3.x - c4.x == 0)
c4.x += .001;
//Find the slope of each line
//Slope of first line
b1 = (c1.y - c2.y) / (c1.x - c2.x);
//Check to make sure slope is non-zero
if(b1 == 0)
b1 = .001;
//slope of second line
b2 = (c3.y - c4.y) / (c3.x - c4.x);
//Check to make sure slope is non-zero
if(b2 == 0)
b2 = .001;
//Make sure lines aren''t perpendicular
if(b1 == b2)
b1 += .001;
//add the height adjustments
a1 = c1.y - (b1 * c1.x);
a2 = c3.y - (b2 * c3.x);
//Finally, find the intersection points
ReturnInfo.x = -(a1 - a2) / (b1 - b2);
ReturnInfo.y = a1 + (b1 * ReturnInfo.x);
//Check to make sure the lines intersect immediatly
if((c3.x - ReturnInfo.x) * (ReturnInfo.x - c4.x) < 0 && (c3.y - ReturnInfo.y) * (ReturnInfo.y - c4.y) < 0)
{
//"Intersect" should change to 0,0 and a should be set to 0
ReturnInfo.x = 0;
ReturnInfo.y = 0;
ReturnInfo.a = 0;
}
else
ReturnInfo.a = 1;
return(ReturnInfo);
}
Sorry if its a bit messy/amatuer, but I''m still learning.