Par*_*rth 2 java math swing line
我画了一条线然后是一个点,然后我想检查点是否在线上.我在数组中采用了一个线坐标(因为有多个线).我想检查最后一行的当前点吗?
if (positionX1 == positionX2 && positionY1 == positionY2) {
float m = line.getSlope(
drawLines[currentLines - 1][2], drawLines[currentLines - 1][3],
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
m = Float.parseFloat(df.format(m));
float c = line.getIntercept(
drawLines[currentLines - 1][2], drawLines[currentLines - 1][3],
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
c = Math.round(c);
m1 = line.getSlope(positionX2, positionY2,
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
m1 = Float.parseFloat(df.format(m1));
System.out.println(m + " " + m1);
c1 = line.getIntercept(positionX2, positionY2,
drawLines[currentLines - 1][0], drawLines[currentLines - 1][1]);
c1 = Math.round(c1);
if (m == m1 && ((c == c1) || (c == c1 - 1) || (c == c1 + 1))) {
System.out.println("Point is on Line");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当一个点靠近线的起点或一条线约为m1的垂直值时,c1的变化差别很大.因此,检测线上是否有点存在问题.我该如何检查这种情况?
Line2D.ptSegDist(x1, y1, x2, y2, xP, yP)如果点(xP,yP)位于从(x1,y1)到(x2,y2)的线段上,则返回0.0. Line2D.ptLineDist为无限线做同样的事情.