270
Appendix B: Software
add the position of the additional point—the triangular detour—to the list of points
that define the next generation. The position of the additional point is calculated in
the function nextpt(), discussed below. Once the loop over m completes and all
points in the next generation are known, they are plotted. Only in the first generation
the initial red triangle is added. Finally the array containing the previous points PP
is updated, such that it can be used when calculating the following generation.
% koch.m
PP=[-0.5,1/sqrt(3)-0.5*sqrt(3);
0,1/sqrt(3);
0.5,1/sqrt(3)-0.5*sqrt(3);
-0.5,1/sqrt(3)-0.5*sqrt(3)]; % same as first
for generation=1:4
subplot(2,2,generation)
NN=size(PP,1); % number of points , first=last
PN=zeros(4*NN-3,2);
for m=1:NN-1
mm=4*m-3;
PN(mm,:)=PP(m,:);
PN(mm+1:mm+3,:)=nextpt(PP(m:m+1,:));
PN(mm+4,:)=PP(m+1,:);
end
plot(PN(:,1),PN(:,2))
axis equal
axis([-0.6 0.6 -0.6 0.6])
title([’Generation:’ num2str(generation) ])
hold on
if generation ==1
plot(PP(:,1),PP(:,2),’r’)
end
PP=PN;
pause(0.5)
end
The following function calculates the coordinates of the additional points for the
“triangular detour.” It receives the coordinates of two points in PP as input and
returns the coordinates of the three points for the detour. Two of the points lie on the
line connecting the start points at 1/3 and 2/3 of the way. The third point is located
perpendicular to the connecting line, which is found with the help of DPP.
% nextpt.m
function PPP=nextpt(PP)
PPP=zeros(3,2);
PPP(1,:)=(2*PP(1,:)+PP(2,:))/3;
PPP(3,:)=(PP(1,:)+2*PP(2,:))/3;
Précédent

- 276/292

Suivant