Smallest enclosing circle in Python, error in the code

前端 未结 2 2024
谎友^
谎友^ 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.

    0 讨论(0)
  • 2021-02-13 03:19

    Without understanding anything about your algorithm, I noticed one thing: the ratio between your coordinates and your radius is very large, about 2e5. Maybe, your algorithm is ill conditioned when trying to find a circle around points which are so far away from the origin. Especially in your _make_circumcircle function, this leads to the subtraction of large numbers, which is usually a bad thing for numerical errors.

    Since fitting the radius and the center of the circle with respect to the points should be independent of a translation, you could simply subtract the mean of all points (the center of mass of your cloud of points), do the fitting, and then add the mean back to obtain the final result:

    def numerical_stable_circle(points):
        pts = np.array(points)
        mean_pts = np.mean(pts, 0)
        print 'mean of points:', mean_pts
        pts -= mean_pts  # translate towards origin
        result = make_circle(pts)
        print 'result without mean:', result
        print 'result with mean:', (result[0] + mean_pts[0], 
        result[1] + mean_pts[1], result[2])
    

    Result:

    mean of points: [  421645.83745955  4596388.99204294]
    result without mean: (0.9080813432488977, 0.8327111343034483, 24.323287017466253)
    result with mean: (421646.74554089626, 4596389.8247540779, 24.323287017466253)
    

    These numbers do not change a single digit from one run to the next one, and differ from your 'correct result' by only a tiny amount (probably different numerical errors due to a different implementation).

    0 讨论(0)
提交回复
热议问题