Parth Soni
Parth Soni

Reputation: 11648

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

Upvotes: 3

Views: 6432

Answers (3)

Louis Wasserman
Louis Wasserman

Reputation: 198103

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.

Upvotes: 10

Pete Kirkham
Pete Kirkham

Reputation: 49311

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.

Upvotes: 2

thatidiotguy
thatidiotguy

Reputation: 8991

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.

Upvotes: 1

Related Questions