Jump to content
  • Advertisement
Sign in to follow this  
fguihen

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

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

If you intended to correct an error in the post then please contact us.

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 ad,bc;
			double c;
			double xIntersect=0, yIntersect=0;
			Vector intersectionPoint;
						
			ad = p1.Yval - p2.Yval;
			bc = p2.Xval - p1.Xval;

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

			//line2
			double ad2,bc2;
			double c2;
			
			ad2 = p3.Yval - p4.Yval;
			bc2 = p4.Xval - p3.Xval;

			c2 = ad* p3.Xval + bc*p3.Yval;
			//where two lines intersect
			double det = ad * bc2 - ad2 * bc;
			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;
				yIntersect = ((ad*c2) - (ad2*c))/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 this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!