方法:
public static int[] FindIntersectionPoint(int[] p0, int[] p1, int[] p2, int[] p3) {
//line 1 equation
double m1 = (double) (Math.abs(p0[1] - p1[1])) / (double) (Math.abs(p0[0] - p1[0])); //slope
double b1 = p0[1] - (m1 * p0[0]); //y axis intercept
DC.drawLine(p0[0], p0[1], p1[0], p1[1]);
//line 2 equation
double m2 = (double) (Math.abs(p2[1] - p3[1])) / (double) (Math.abs(p2[0] - p3[0])); //slope
double b2 = p2[1] - (m2 * p2[0]); //y axis intercept
DC.drawLine(p2[0], p2[1], p3[0], p3[1]);
//Intersection points
double intersectX = (double)(b2 - b1) / (double)(m1 - m2);
double intersectY = 0;
System.out.println(intersectX); //is -18.181818181818205 which is way off
return new int[] {(int)intersectX, (int)intersectY};
}
方法调用:
int point[] = FindIntersectionPoint(new int[]{200, 100}, new int[]{300, 400}, new int[]{500, 50}, new int[]{200, 400});
我已经检查了m1,m2,b1,b2变量都是正确计算的,并且xIntercept的公式:(b2 - b1)/(m1 - m2)在纸上工作,但程序计算出来了-18.181818181818205这绝对不是答案。
答案 0 :(得分:0)
可能的错误可能在这一行:
double b1 = p0[1] - (m1 * p0[0]); //y axis intercept
您正在保存int
到double
的操作,其中小数位将始终为0.您确定此计算是否正常?
在分割之前,至少投放ints
至double
之一。