The original algorithm is sphere, which is simplified to circle here.
Ritter's Algorithm for Finding the smallest enclosed circle is a linear algorithm, which is widely used because it is very simple.
The circle obtained by this algorithm is about 5% to 20% larger than the optimal circle. You can use the bouncing bubble algorithm to find the optimal circle. You can try it later.
Ritter's algorithm is as follows:
1. Randomly select two points from the vertex set as the diameter to initialize the circle.
2. Determine whether the next vertex P is in the circle. If it is in the circle, continue this step. If not, proceed to step 3.
3. Use P as one boundary point of the new circle, and the other boundary point is the point on the circle farthest from P. Use these two points as the diameter to construct the new circle.
4. Continue Step 2 until all vertices are traversed.
The result is as follows:
The Matlab code is as follows:
clear all;close all;clc;n=100;p=rand(n,2);p1=p(1,:);p2=p(2,:);r=sqrt((p1(1)-p2(1))^2+(p1(2)-p2(2))^2)/2;cenp=(p1+p2)/2;for i=3:n newp=p(i,:); d=sqrt((cenp(1)-newp(1))^2+(cenp(2)-newp(2))^2); if d>r r=(r+d)/2; cenp=cenp+(d-r)/d*(newp-cenp); end endhold on;plot(p(:,1),p(:,2),‘o‘);x0=cenp(1);y0=cenp(2);theta=0:0.01:2*pi;x=x0+r*cos(theta);y=y0+r*sin(theta);plot(x,y,‘-‘,x0,y0,‘.‘);axis equal
Reference: http://en.wikipedia.org/wiki/Bounding_sphere
MATLAB exercise program (Ritter's smallest circle)