百度一圈没有找到合适的博客,通过外网找到了python实现,所以整理记录一下。
最小圆问题
The smallest-circle problem (also known as minimum covering circle problem, bounding circle problem, smallest enclosing circle problem)
有多种算法求解实现,具体参见维基百科。
最容易理解,使用最多的是一些计算几何算法,大家可以参考这篇博文最小圆覆盖(经典算法【三点定圆)进行理论学习
代码实现
Project Nayuki
三十组符合高斯分布的随机点,求最小覆盖圆
import math, random
# Data conventions: A point is a pair of floats (x, y). A circle is a triple of floats (center x, center y, radius).
# Returns the smallest circle that encloses all the given points. Runs in expected O(n) time, randomized.
# Input: A sequence of pairs of floats or ints, e.g. [(0,5), (3.1,-2.7)].
# Output: A triple of floats representing a circle.
# Note: If 0 points are given, None is returned. If 1 point is given, a circle of radius 0 is returned.
#
# Initially: No boundary points known
def make_circle(points):
# Convert to float and randomize order
shuffled = [(float(x), float(y)) for (x, y) in points]
random.shuffle(shuffled)
# Progressively add points to circle or recompute circle
c = None
for (i, p) in enumerate(shuffled):
if c is None or not is_in_circle(c, p):
c = _make_circle_one_point(shuffled[ : i + 1], p)
return c
# One boundary point known
def _make_circle_one_point(points, p):
c = (p[0], p[1], 0.0)
for (i, q) in enumerate(points):
if not is_in_circle(c, q):
if c[2] == 0.0:
c = make_diameter(p, q)
else:
c = _make_circle_two_points(points[ : i + 1], p, q)
return c
# Two boundary points known
def _make_circle_two_points