# line on line intersection function( experts please see if you can spot the errors)

This topic is 5053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

sorry for posting again today, but ive been racking my brain and scanning through this for the last few hours, and changing it but i cant fix it. it takes 4 points, the first two are the points of one line, and the second two are the points of the second line. the x and y points at which it thinks the intersection works at are always off, and x is always in a minus figure, which cant be true as its on a pc screen, where the lowest numbers in use are 00. the alkward if statement is just to check the intersection point is within the range of both line segments. i know there are many little stupid flaws, but what im looking for is actual algorithm errors. im hoping fresh eyes might make a diffenenc , thanks.
	private Vector IntersectionTest(Vector p1,Vector p2, Vector p3, Vector p4)//unsure of whats wrong with this function
{
double c;
double xIntersect=0, yIntersect=0;
Vector intersectionPoint;

bc = p2.Xval - p1.Xval;

c = ad* p1.Xval + bc*p1.Yval;

//line2
double c2;

bc2 = p4.Xval - p3.Xval;

c2 = ad* p3.Xval + bc*p3.Yval;
//where two lines intersect
if( p2.Xval >500)
{
int i = 0;
}

if(det == 0)// i know this is inefficient, but it should work
{
//lines are paralell. nothing to do.
intersectionPoint = new Vector(0,0);
}
else
{
xIntersect = ((bc2*c) - (bc*c2))/det;

#region //just makes sure intersection is on line segments, and not along the infinite line
if (xIntersect >= p1.Xval && xIntersect <= p2.Xval && xIntersect >=p3.Xval && xIntersect <= p4.Xval)
{
if(yIntersect >= p1.Yval && yIntersect <= p2.Yval && yIntersect >=p3.Yval && yIntersect <= p4.Yval)
{
velocity.Xval = 0;
velocity.Yval = 4;
}

}
else{velocity = velocity;}
#endregion

}
intersectionPoint = new Vector((float)xIntersect,(float)yIntersect);

return intersectionPoint;
}


##### Share on other sites
im sorry for posting again and wasting your time. i had one more look at my code and found the error.

the line c2 = ad* p3.Xval + bc*p3.Yval;

should be ce = ad2 * p3.Xval + bc2*p3.Yval;

just staring at it so long i couldnt see the errors. thanks and again, sorry

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633711
• Total Posts
3013488
×