最小覆盖圆(附完整代码)

一、问题描述

  给定平面 n n n个点,计算最小覆盖圆,使得所有给定点均在圆周以内(包括在圆周上)。博主commonc详细给出了最小覆盖圆算法步骤、算法时间复杂度及其证明、空间复杂度。博主Howe_Young书写的代码简洁、形式优雅,本文在其代码上做了小修改:(1)通过判断距离的平方而非距离,来判断点是否在圆内,从而避免平方根运算,有效提升计算速度 (2)尽量采用乘法代替除法,提升计算速度。下面是改进后的最小覆盖圆算法的MATLAB、LUA、C++完整代码。
  2个点示例:

  3个点示例:

  10个点示例:

  100个点示例:

二.MATLAB代码

%{
   
   
Function: get_sign
Description: 求实数x的符号
Input: 实数x
Output: 实数x的符号y
Author: Marc Pony(marc_pony@163.com)
%}
function y = get_sign(x)
if abs(x) < 1.0e-8
    y = 0;
else
    if x < 0.0
        y = -1;
    else
        y = 1;
    end
end
end
%{
   
   
Function: get_distance_square
Description: 求平面两点之间距离的平方
Input: 平面两点a,b
Output: 平面两点之间距离的平方
Author: Marc Pony(marc_pony@163.com)
%}
function distanceSquare = get_distance_square(a, b)
distanceSquare = (a(1) - b(1))^2 + (a(2) - b(2))^2;
end
%{
   
   
Function: get_circle_center
Description: 求三角形外接圆的圆心
Input: 平面三个点a,b,c
Output: 三角形外接圆的圆心center
Author: Marc Pony(marc_pony@163.com)
%}
function center = get_circle_center(a, b, c)
center = zeros(2, 1);
a1 = b(1) - a(1);
b1 = b(2) - a(2);
c1 = 0.5 * (a1 * a1 + b1 * b1);
a2 = c(1) - a(1);
b2 = c(2) - a(2);
c2 = 0.5 * (a2 * a2 + b2 * b2);
d = a1 * b2 - a2 * b1;
center(1) = a(1) + (c1 * b2 - c2 * b1) / d;
center(2) = a(2) + (a1 * c2 - a2 * c1) / d;
end
%{
   
   
Function: min_cover_circle
Description: 求平面pointCount个点的最小覆盖圆
Input: 平面pointCount个点的坐标(x,y),点个数pointCount
Output: 平面pointCount个点的最小覆盖圆圆心center,半径radius
Author: Marc Pony(marc_pony@163.com)
%}
function [center, radius] = min_cover_circle(x, y, pointCount)
p = [x(:)'; y(:)'];
p = p(:, randperm(pointCount)); %随机打乱数据
center = p(:, 1);
radiusSquare = 0.0;
for i = 2 : pointCount
    if get_sign(get_distance_square(p(:, i), center) - radiusSquare) > 0
        center = p(:, i);
        radiusSquare = 0.0;
        for j = 1 : i - 1
            if get_sign(get_distance_square(p(:, j), center) - radiusSquare) 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值