// FindIntersection(coords for 2 lines) : This function returns the point where the two specified lines intersect. 2D return
POINT FindIntersection(int l1x1, int l1y1, int l1x2, int l1y2, int l2x1, int l2y1, int l2x2, int l2y2, DWORD *pIntersectionType)
{
POINT Line11; // Line 1 Point 1
POINT Line12; // Line 1 Point 2
long double Line1m; // Line 1 slope
long double Line1b; // Line 1 y-intercept
POINT Line21; // Line 2 Point 1
POINT Line22; // Line 2 Point 2
long double Line2m; // Line 2 slope
long double Line2b; // Line 2 y-intercept
POINT Inter; // The point where the lines intercept
Inter.x = 0;
Inter.y = 0;
// Put the lines in the proper algebraeic format
if(l1x1 < l1x2)
{
Line11.x = l1x1;
Line11.y = l1y1;
Line12.x = l1x2;
Line12.y = l1y2;
}
else
{
Line11.x = l1x2;
Line11.y = l1y2;
Line12.x = l1x1;
Line12.y = l1y1;
}
if(l2x1 < l2x2)
{
Line21.x = l2x1;
Line21.y = l2y1;
Line22.x = l2x2;
Line22.y = l2y2;
}
else
{
Line21.x = l2x2;
Line21.y = l2y2;
Line22.x = l2x1;
Line22.y = l2y1;
}
// When both lines are neither vertical or horizontal
if(Line12.y != Line11.y && Line12.x != Line11.x && Line22.y != Line21.y && Line22.x != Line21.x)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_NEITHER_H_V;
Line1m = (Line12.y - Line11.y) / (Line12.x - Line11.x);
Line2m = (Line22.y - Line21.y) / (Line22.x - Line21.x);
Line1b = Line11.y - (Line1m * Line11.x);
Line2b = Line21.y - (Line2m * Line21.x);
Inter.x = (Line1b - Line2b) / (Line2m - Line1m);
Inter.y = Line1m * Inter.x + Line1b;
}
// When one line is vertical and one is horizontal
else if(Line12.y == Line11.y && Line22.x == Line21.x)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_BOTH_H_V;
Inter.x = Line21.x;
Inter.y = Line11.y;
}
else if(Line22.y == Line21.y && Line12.x == Line11.x)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_BOTH_H_V;
Inter.x = Line11.x;
Inter.y = Line21.y;
}
// When one line is horizontal and one is not
else if(Line12.y == Line11.y && Line22.y != Line21.y)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_ONE_H_V;
Line1m = 0;
Line2m = (Line22.y - Line21.y) / (Line22.x - Line21.x);
Line1b = Line11.y;
Line2b = Line21.y - (Line2m * Line21.x);
Inter.x = (Line1b - Line2b) / (Line2m - Line1m);
Inter.y = Line12.y;
}
else if(Line22.y == Line21.y && Line12.y != Line11.y)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_ONE_H_V;
Line1m = (Line12.y - Line11.y) / (Line12.x - Line11.x);
Line2m = 0;
Line1b = Line11.y - (Line1m * Line11.x);
Line2b = Line21.y;
Inter.x = (Line1b - Line2b) / (Line2m - Line1m);
Inter.y = Line22.y;
}
// When one line is vertical and one is not
else if(Line12.x == Line11.x && Line22.x != Line21.x)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_ONE_H_V;
Line2m = (Line22.y - Line21.y) / (Line22.x - Line21.x);
Line2b = Line21.y - (Line2m * Line21.x);
Inter.x = Line12.x;
Inter.y = Line2m * Line12.x + Line2b;
}
else if(Line22.x == Line21.x && Line12.x != Line11.x)
{
if(pIntersectionType != NULL)
*pIntersectionType = GD_ONE_H_V;
Line1m = (Line12.y - Line11.y) / (Line12.x - Line11.x);
Line1b = Line11.y - (Line1m * Line11.x);
Inter.x = Line22.x;
Inter.y = Line1m * Line22.x + Line1b;
}
else
if(pIntersectionType != NULL)
*pIntersectionType = GD_DONT_INTERSECT;
return Inter;
}
There it is! I am hoping all you code/math sharks out there will be able to tell me my predicament. A problem with trying to find the intercept between a horizontal line, and a line that is mostly horizontal(the line has very litle change along the y axis). If anyone could help me, I would be very grateful.
BTW: Sorry about the new post
Brent Robinson
"Ich bin deinem Vatter!"
Problem with that Intersection Computation Algorithm
Hey, I have a problem with those Algorithms from my other post, and I have tried all I can to figure out what is not working, so I am just gonna post the code here. Sorry, but I don''t know how to do that neat, text parsing stuff:
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
For an another look at intersection of two lines check out Paul Bourke''s website or visit mine for the link. I''ve done intersection with parametric equation so look at my csg code for that also. It''s rather longish so I won''t post it here. The equation doesn''t use slopes. My code uses Paul''s equation.
my homepage
E-Mail: BlueOrbSoftware@mailcity.com
my homepage
E-Mail: BlueOrbSoftware@mailcity.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement