简体   繁体   English

检查Java Swing中的点是否在线

[英]Check if point is on line in Java Swing

I have drawn a line and then a point, and then I want to check if the point is on the line or not. 我画了一条线然后是一个点,然后我想检查点是否在线上。 I have taken a line coordinate in array (as there was more than one line). 我在数组中采用了一个线坐标(因为有多个线)。 I want to check the current point in on the last line or not? 我想检查最后一行的当前点吗?

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");
    }
}

Problem is when a point is near the starting point of line or when a line is about vertical values of m1 and c1 changes with big difference. 问题是当一个点靠近线的起点或一条线约为m1的垂直值时,c1的变化差别很大。 So, there's a problem for detecting if a point on line or not. 因此,检测线上是否有点存在问题。 How can I check for this situation? 我该如何检查这种情况?

Line2D.ptSegDist(x1, y1, x2, y2, xP, yP) returns 0.0 if the point (xP, yP) is on the line segment from (x1, y1) to (x2, y2). 如果点(xP,yP)在从(x1,y1)到(x2,y2)的线段上,则Line2D.ptSegDist(x1, y1, x2, y2, xP, yP)返回0.0。 Line2D.ptLineDist does the same thing for the infinite line. Line2D.ptLineDist为无限线做同样的事情。

Use the vector form of the distance from a point to a line where the line is x = a + t n . 使用从点到线距离的向量形式,其中线是x = a + t n

If instead of a unit vector n you use a non-unit vector N , then d = ||( a - p ) - (( a - p ) · N ) N / ( N · N ) ||, which eliminates a square root. 如果使用非单位向量N代替单位向量n ,则d = ||( a - p ) - (( a - p )· NN /( N · N )||,这消除了一个正方形根。

Assuming that the arrays of floats you are using to describe lines are interpreted as { x1, y1, x2, y2 }, then a = ( x1, y1 ) and N = ( x2 - x1, y2 - y1 ). 假设您用于描述行的浮点数组被解释为{x1,y1,x2,y2},则a =(x1,y1)和N =(x2 - x1,y2 - y1)。

If the calculated distance is comparable to the measurement or arithmetic errors, the point is on the line. 如果计算的距离与测量或算术误差相当,则该点在线上。 Again, you don't need to calculate the square root in the modulus but can compare the squared value. 同样,您不需要计算模数中的平方根,但可以比较平方值。

In terms of an algorithm, a line (other than one which is vertical which has equation like x = constant) has a form y = mx + b. 就算法而言,一条线(除了具有x =常数之类的垂直的线之外)具有y = mx + b的形式。 If your point satisfies that equation, then it is on the line. 如果你的观点满足那个等式,那么它就在线上。 So all you need to is find the slope value of the line, and its y-intercept and check to see if the point's x and y values satisfy the equation for each line. 因此,您只需要找到线的斜率值及其y轴截距,并检查点的x和y值是否满足每条线的方程。

EDIT: 编辑:

As is pointed out in an above comment, you could use the point-slope form (with slope being (y2-y1)/(x2/x1)) instead of the slope-intercept form. 正如上面的注释所指出的,你可以使用点斜率形式(斜率为(y2-y1)/(x2 / x1))而不是斜率截距形式。 This would give you an equation that depends solely on y,x and the start and end points of the lines which would be much easier to code out (since you define a line by its start and end points, at least in swing). 这将为您提供一个方程式,该方程式仅取决于y,x以及更容易编码的线的起点和终点(因为您通过其起点和终点定义线,至少在摆动中)。 The only reason I suggested the slope-intercept form was because you were already attempting to use it in your algorithm. 我建议使用斜率截距形式的唯一原因是因为您已经尝试在算法中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM