Smallest enclosing circle in Python, error in the code

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

提交回复
热议问题