Determining if a coordinate is on a line

Posted by TGCBraun on Stack Overflow See other posts from Stack Overflow or by TGCBraun
Published on 2013-11-01T15:50:52Z Indexed on 2013/11/01 15:53 UTC
Read the original article Hit count: 211

Filed under:
|
|
|
|

I´m coding a little app that allows the user to draw multiple shapes and then remove or resize them. It´s working perfectly on rectangles and ovals, but I´m having issues with lines.

Here´s a method that I wrote to find if the clicked spot on the screen is part of a specific line:

    public boolean containsLocation(int x, int y) {
    int m = (getY2() - getY()) / (getX2() - getX());
    int b = getY() - (m * getX());
    if (y == (m * x) +  b) {
        return true;
    }
    return false;

I´m using the famous y = mx + b formula and replacing y and x to find if the clicked spot is part of the line. The problem is when I click on the screen to remove the line, it only works if I click on the very fist coordinate (x,y) where the line starts.

Nothing happens when I click anywhere else along the line.

Can anyone shed a light on what I´m doing wrong?

Thanks a lot.

© Stack Overflow or respective owner

Related posts about java

Related posts about geometry