Smallest enclosing circle in Python, error in the code

前端 未结 2 2028
谎友^
谎友^ 2021-02-13 02:38

I have a set of points that represent the vertices (x, y) of a polygon.

points= [(421640.3639270504, 4596366.353552659), (421635.79361391126, 4596369.054192241),         


        
2条回答
  •  一生所求
    2021-02-13 02:55

    I am the author of the smallest enclosing circle implementation that you used. Please accept my apologies for the faulty code and the 2-years-late response.

    The current version of the library fixes the issue that you experienced. Please update your copy to the latest version, and I assure you that the algorithm generates stable and sane results for the numerical case that you gave.


    Another user came to me with a similar problem to you, where the algorithm outputs wildly different circles depending on how the list of points is randomized. His input data has a similar characteristic - the variation in the numbers is smaller than the magnitude of the numbers; for example 10.000001 versus 10.000002. I managed to thoroughly debug his test case because it contained only 5 points whereas yours has 32.

    The root cause is that _make_circle() and _make_circumcircle() blindly calculate a radius that is mathematically correct, but fail to account for the distortion when coordinates of the center point is rounded. The correct way is to calculate the center point of the proposed circle, and then calculate the radius based on the maximum of how far each circumferential point is from the center.

    For example, suppose that we want to find a circle that encloses (1,0) and (6,0), but every point is rounded to an integer. The true circle is of course (3.5, 0, 2.5). We calculate the x center as (1+6)÷2 = 3.5 → 4 (round to half even). If we calculate the radius separately and blindly, then it is the distance between 1 and 6, divided by 2, which is (6−1)÷2 = 2.5 → 2 (round to half even). But if we calculate the distance from the distorted center of (4,0) to (1,0) and (6,0), then we can see that we actually need a radius of 3. When the circle's radius is too small, it won't contain points that it was required to contain by design, and so the algorithm gets confused and tries to calculate new circles based on dubious data in dubious ways.

提交回复
热议问题