Sign in to follow this  

determinants + solving linear equations

This topic is 4688 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

im finding the intersection point of two lines using determinants. the algorithm seems to work ok most of the time, but not always. im wondering if there has to be any exceptions made when dealing with horizontal and/or vertical lines? ( the original way i was trying to do this was with y = mx + c, but i learned here that way is very alkward and i was told to avoid it) [Edited by - fguihen on February 11, 2005 2:42:01 PM]

Share this post


Link to post
Share on other sites
No, no exceptions for horizontal or vertical lines. However, parallel and/or colinear lines may need to be special-cased.

Anyway, what sort of problems are you having? You could also post your line intersection code - it should be pretty easy to figure out what's going wrong.

Share this post


Link to post
Share on other sites
your prob right. it seems to work for sloped lines, but in my app, i have a bot and his "sight" is a line out in front of him. when this intersects any lines, a collision has occoured, but there are many lines that are paralell with his sight line



___________________________

OO____________ |
OO |
|
|

___________________________


as you can see here, my bot ( the four 0,s) is going through a corridor. his sight line is in front of him and will detect when he will colide with the vertical wall in front of him, but tests against the paralell walls fail.

im not sure enough of determinants to know how to solve this.

the function is here:

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(det == 0)
{
//lines are paralell. nothing to do.
intersectionPoint = new Vector(0,0);
velocity =velocity;
}
else
{
xIntersect = ((bc2*c) - (bc*c2))/det;
yIntersect = ((ad*c2) - (ad2*c))/det;
velocity.Xval = 0;
velocity.Yval = 4;
}
intersectionPoint = new Vector((float)xIntersect,(float)yIntersect);

return intersectionPoint;

Share this post


Link to post
Share on other sites
That isn't c++ is it? (C#?) In any case there are a couple of things I'm not sure about. What is velocity = velocity supposed to do? Also, why do you set velocity to 0, 4 in the other case?

It looks like your function is responding to the parallel case by returning a zero vector. I'm not sure what your intention is with this, but in any case it's not a very good way of informing the caller of the parallel case. (For one thing, what if the intersection point is actually 0, 0? Highly unlikely, but not impossible.)

Even if you just return 'false' for the parallel case, you'll be ignoring colinear but intersecting lines. For your purposes though this is probably ok.

Also, it doesn't look like you're checking to make sure that the intersection point is actually contained in both line segments. Although again perhaps in your particular application that's ok.

I would start by having this function return a boolean result telling whether or not the segments intersect, and have the intersection point be a pointer or reference argument.

Lastly, there are (IMO) better methods for segment intersection than the one you're using. I'd try to get your current method working first, though.

Share this post


Link to post
Share on other sites
i have added a few if statements to check if the point is on both lines. in the suituation im using this, there is absolutely zero chance of intersection being on 0,0. there is a chance though of colinear. il bear in mind what you said. thanks

Share this post


Link to post
Share on other sites

This topic is 4688 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this