Does a line contain a point

后端 未结 3 1913
野趣味
野趣味 2020-12-21 17:00

I want the user to be able to drag the edges of a square around the canvas. With my current solution it works but has glitches, sometimes an edge cannot be selected. Is ther

3条回答
  •  难免孤独
    2020-12-21 17:25

    use the line equation y = mx + b to find out if the point is on a line

    float EPSILON = 0.001f;
    
    public boolean isPointOnLine(Point linePointA, Point linePointB, Point point) {
        float m = (linePointB.y - linePointA.y) / (linePointB.x - linePointA.x);
        float b = linePointA.y - m * linePointA.x;
        return Math.abs(point.y - (m*point.x+b)) < EPSILON);
    }
    

提交回复
热议问题