在 Java Swing 中检查点是否在线

发布于 2025-01-01 13:18:46 字数 1146 浏览 2 评论 0原文

我画了一条线,然后画了一个点,然后我想检查该点是否在线上。我在数组中获取了一条线坐标(因为有多条线)。我想检查最后一行的当前点吗?

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 的值变化很大。所以,检测一个点是否在线上存在一个问题。我该如何检查这种情况?

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. So, there's a problem for detecting if a point on line or not. How can I check for this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳哥哥 2025-01-08 13:18:46

Line2D.ptSegDist(x1, y1, x2, y2, xP, yP) 如果该点返回 0.0 (xP, yP) 位于从 (x1, y1) 到 (x2, y2) 的线段上。 Line2D.ptLineDist 对无限线执行相同的操作。

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). Line2D.ptLineDist does the same thing for the infinite line.

來不及說愛妳 2025-01-08 13:18:46

使用从点到线的距离的矢量形式,其中线为x = a + t n

如果使用非单位向量 N 而不是单位向量 n,则 d = ||(a > - p) - ((a - p) · N) N / ( N · N) ||,消除一个正方形 根。

假设用于描述线条的浮点数组被解释为 { x1, y1, x2, y2 },则 a = ( x1, y1 ) 和 N = (x2 - x1,y2 - y1)。

如果计算出的距离与测量或算术误差相当,则该点在线上。同样,您不需要计算模数的平方根,但可以比较平方值。

Use the vector form of the distance from a point to a line where the line is 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.

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 ).

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.

自此以后,行同陌路 2025-01-08 13:18:46

就算法而言,一条线(除了具有 x = 常数等方程的垂直线之外)具有 y = mx + b 的形式。如果你的点满足这个方程,那么它就在线上。因此,您需要做的就是找到直线的斜率值及其 y 截距,并检查该点的 x 和 y 值是否满足每条直线的方程。

编辑:

正如上面的评论所指出的,您可以使用点斜率形式(斜率为 (y2-y1)/(x2/x1))而不是斜率截距形式。这将为您提供一个仅取决于 y,x 以及线的起点和终点的方程,这将更容易编码(因为您通过起点和终点定义线,至少在摆动中)。我建议使用斜率截距形式的唯一原因是因为您已经尝试在算法中使用它。

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. 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.

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. 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). The only reason I suggested the slope-intercept form was because you were already attempting to use it in your algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文