最小覆盖圆(smallest enclosing circle)算法 python 实现

百度一圈没有找到合适的博客,通过外网找到了python实现,所以整理记录一下。最小圆问题The smallest-circle problem (also known as minimum covering circle problem, bounding circle problem, smallest enclosing circle problem)有多种算法求解实现,具体参见维基百科。最容易理解,使用最多的是一些计算几何算法,大家可以参考这篇博文最小圆覆盖(经典算法【三点定圆)进行理论学习
摘要由CSDN通过智能技术生成

百度一圈没有找到合适的博客,通过外网找到了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
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值