续 https://www.toutiao.com/i6766960319995576843/
- 设定三角形A顶点的坐标为 (x1,y1);(x2,y2);(x3,y3);随机初始化;计算得知垂心到三个顶点距离为:
R=(((x1^2 - 2*x1*x2 + x2^2 + y1^2 - 2*y1*y2 + y2^2)*(x1^2 - 2*x1*x3 + x3^2 + y1^2 - 2*y1*y3 + y3^2)*(x2^2 - 2*x2*x3 + x3^2 + y2^2 - 2*y2*y3 + y3^2))/(4*(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y3 - x3*y2)^2))^(1/2);
- 固定 R, 利用遍历x,y坐标轴 绘制,误差z(三个顶点到坐标(x,y)的距离和R 的差的绝对值),其3d 图为
- 初始化n个 粒子群 points ,计算每个粒子的误差,并且从小到大排序。
- 从群体中任取另外两个粒子,并获取到从适应度低的到高的两个粒子的空间向量p。以适应度最好的粒子为基准,加上每个向量p; 得到一个·新的粒子群;排序,筛选前n个;并且反复迭代第4步,直到所有粒子位置叠合。得到结果图:
x1=rand()*30;x2=rand()*30;x3=rand()*30;y1=rand()*30;y2=rand()*30;y3=rand()*30;p3=[x1,x2,x3;y1,y2,y3];triangle_x=[x1,x2,x3,x1];triangle_y=[y1,y2,y3,y1];% figurefill(triangle_x,triangle_y,'b');% mx0=rand()*60;% my0=rand()*60;points = ceil(rand(20,4)*60);for i= 1:size(points) x=points(i,1); y=points(i,2); R=points(i,3); hold on plot(x,y,'r.') points(i,4)= countDev(p3,[x,y],R);endF=getframe(gcf);I=frame2im(F);[I,map]=rgb2ind(I,256);imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.6);% pointspoints = sortrows(points,4);min00 = points(1,4);pz = size(points,1);tp = zeros((pz-1)*(pz-2)/2,4);for time=1:20 tpp=1; cla hold on fill(triangle_x,triangle_y,'b'); for rn=2:pz-1 for rn2=rn+1:pz % tp(tpp,:) = points(1,:)+ (points(1,:)- points(rn,:))+(points(1,:)- points(rn2,:)); tp(tpp,:) = points(1,:) + (points(rn,:)- points(rn2,:)); % + (points(1,:)-) x=tp(tpp,1); y=tp(tpp,2); tp(tpp,3)=abs(tp(tpp,3)); R=tp(tpp,3); % [rn,rn2] %hold on plot(x,y,'r.'); tp(tpp,4)= countDev(p3,[x,y],R); % if tpp==1 % plot(points(rn,1),points(rn,2),'r.'); % plot(points(rn2,1),points(rn2,2),'r.'); % plot(points(1,1),points(1,2),'b.'); % plot(tp(tpp,1),tp(tpp,2),'g.') % end tpp=tpp+1; end end % plot3(tp(1,1),tp(1,2),tp(1,4),'r.'); tp=sortrows([tp],4);%;points % for ii=1:size(tp,1) % if tp(ii,4) > min00 % [time,ii,size(tp,1)] % break % end % end min00 =tp(1,4); points=tp(1:pz,:); % 00 F=getframe(gcf); I=frame2im(F); [I,map]=rgb2ind(I,256); imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.6); pause(0.9);end