我先画了一条线,然后画了一个点,然后我要检查该点是否在线上。我已经在数组中取得了一条线坐标(因为有多条线)。我想在最后一行检查当前点吗?
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"); } }
问题是当一点在直线的起点附近或直线大约在m1和c1的垂直值相差很大时。因此,检测点是否在线上存在一个问题。我如何检查这种情况?
Line2D.ptSegDist(x1, y1, x2, y2, xP, yP)如果点(xP,yP)在从(x1,y1)到(x2,y2)的线段上,则返回0.0。 Line2D.ptLineDist对无限行执行相同的操作。
Line2D.ptSegDist(x1, y1, x2, y2, xP, yP)
Line2D.ptLineDist