Search code examples
javaalgorithmconvex-hull

Find point farthest from line


I have an array of points, as well as two more points (A and B). The last two points form a line, and I'm trying to find which of the points in my array are furthest from the line. How would I do this in Java?

I'm wondering if it's something along the lines of finding the distance from A and B, but that doesn't sit well in my head.

Additional info: I think it's a line segment. Given this is QuickHull, I don't know if it makes a difference. I've never been the greatest when it comes to math and formulas, so more explanation is better. Thanks!


Solution

  • Note that each 3 points [a,b,p] for each p in the array form a trianle, whose area is denoted by: (ab) * h /2 [where h is the distance from p to ab]

    You can compute the area these trianles create, and select the minimal. Since ab is constant for all - it guarantees that the trianle with the minimal area will also have the minimal h.

    You can find it [the area of each triangle] using

    T=(1/2)* abs((x_a - x_p) * (y_b-y_a) - (x_a - x_b)* (y_p - y_a))
    

    [where x_a,x_b,x_p and y_a,y_b,y_p are the x,y coordinates of a,b,p respectively].

    • Though I find this method very elegant, I believe there are better ways to do it.