|
|
Back to UserFriendly Strip Comments Index
| I have a java question |
by benmiller |
2004-04-05 17:43:31 |
Trying to find intersection of two line segments in point-point form, and it's not working very well. Here is the function.
//Uses the matrix form to check to see if two lines intersect.
//Returns a b2dPoint with the location of the point if they do
//Can handle both vertical and horizontal lines, but it uses a rough approximation for infinity
//Should be sufficient for the small viewing area i'm using
public b2dPoint lineIntersection(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) {
//Initialize some variables
float m0;
float m1;
float c0;
float c1;
float determinateInverse;
if ((x1-x0) != 0) {
m0 = (y1-y0)/(x1-x0);
}
else {
m0 = 1000000000;//One billion may not be infinity, but it's close enough
}
if ((x3-x2) != 0) {
m1 = (y3-y2)/(x3-x2);
}
else {
m1 = 1000000000;//One billion may not be infinity, but it's close enough
}
//If the slopes are not equal, then the lines will intersect somewhere. Find that point.
if (m0 != m1) {
float xIntersection;
float yIntersection;
/*c0 = (y0-m0*x0);
c1 = (y2-m1*x2);
//Compute the inverse of the determinate
determinateInverse = 1/(m1-m0);
//Use Kramer's rule to get xIntersection and yIntersection
//xIntersection = ((c0-c1)*determinateInverse);
//yIntersection = ((m0-m1)*determinateInverse);*/
//xIntersection = ((-m0/(m1-m0))*x2+m0*(y2-y0) + x0);
//yIntersection = ((m0*y0 - m1*y2 + x2 - x0)/(m0 - m1));
//Use my OWN substitution to try and find the intersection. GAH!
yIntersection = ((y0*m1 - m1*m0*x0 - m0*y2 + m0*m1*x2)/(m1-m0));
xIntersection = ((yIntersection - y2 + m1*x2)/m1);
/*System.out.println("x0:" + x0);
System.out.println("y0:" + y0);
System.out.println("x1:" + x1);
System.out.println("y1:" + y1);
System.out.println("x2:" + x2);
System.out.println("y2:" + y2);
System.out.println("x3:" + x3);
System.out.println("y3:" + y3);
System.out.println("m0:" + m0);
System.out.println("m1:" + m1);
System.out.println("xint:" + xIntersection);
System.out.println("yint:" + yIntersection);*/
//If the intersection lies on BOTH lines, return it
if ((((x0 <= xIntersection && xIntersection <= x1) || (x1 <= xIntersection && xIntersection <= x0)) && ((x2 <= xIntersection && xIntersection <= x3) || (x3 <= xIntersection && xIntersection <= x2))) && (((y0 <= yIntersection && yIntersection <= y1) || (y1 <= yIntersection && yIntersection <= y0)) && ((y2 <= yIntersection && yIntersection <= y3) || (y3 <= yIntersection && yIntersection <= y2)))) {
System.out.println("x0:" + x0);
System.out.println("y0:" + y0);
System.out.println("x1:" + x1);
System.out.println("y1:" + y1);
System.out.println("x2:" + x2);
System.out.println("y2:" + y2);
System.out.println("x3:" + x3);
System.out.println("y3:" + y3);
System.out.println("m0:" + m0);
System.out.println("m1:" + m1);
System.out.println("xint:" + xIntersection);
System.out.println("yint:" + yIntersection);
return new b2dPoint(xIntersection,yIntersection);
}
//Otherwise, return the endpoints of the first line
else {
return new b2dPoint(x1,y1);
}
}
//If they are equal, return the endpoint of the first line
else {
return new b2dPoint(x1,y1);
}
} |
|
[ Reply ] |
|
Hmmm- trying to get UFies to do your CS homework?? | by inittab | 2004-04-05 18:34:21 |
|
Of course not | by benmiller | 2004-04-05 18:43:44 |
|
My solution: | by benmiller | 2004-04-05 18:59:42 |
|
Different solution | by BrainBug | 2004-04-05 20:33:27 |
|
Code convention? | by benmiller | 2004-04-05 20:44:06 |
|
Hehe... | by BrainBug | 2004-04-05 20:56:56 |
|
Sign errors | by BrainBug | 2004-04-05 21:32:05 |
|
|
[Todays Cartoon Discussion]
[News Index]
|
|