I have a line segment made of two points p1 and p2, and a second line segment made up of points p3 and p4. I'm trying to figure out if they intersect, and so far, I have had no luck. This is my code so far:
public static double angle(Point p1, Point p2, Point p3) {
double AB = length(p2, p1);
double BC = length(p2, p3);
double AC = length(p3, p1);
return Math.acos((sqr(BC) + sqr(AB) - sqr(AC)) / (2 * BC * AB)) * (180 / Math.PI);
}
public static boolean doIntersect(Point p1, Point p2, Point p3, Point p4) {
double a = angle(p4, p3, p2);
double b = angle(p3, p2, p1);
double c = 180 - b - a;
System.out.println("a: " + a + ", b: " + b + ", c:" + c);
if((length(p3, p2) * Math.sin(a)) / Math.sin(c) > length(p2, p1)) return false;
if((length(p3, p2) * Math.sin(b)) / Math.sin(c) > length(p3, p4)) return false;
return true;
}
public static double length(Point point1, Point point2) {
return Math.sqrt(sqr(point1.x - point2.x) + sqr(point1.y - point2.y));
}
public static double sqr(double doub) {
return Math.pow(doub, 2);
}
But this isn't working. Sometimes, the angle "c" even comes out as negative numbers.
Also, Point is a custom class with two parameters: x and y. Should be fairly self-explanatory.