Schreiben Sie ein Programm
snowflake(k),
das die k-te Iteration der Kochsche Schneeflocke plottet.
function p = snow_flake(steps)
% function q = snow_flake(steps)
% generates the polygon q of the Koch snow flake
if nargin < 1;
steps = 3;
end;
% initial polygon and mask for subdivision
p = [0 1 1/2; 0 0 sqrt(3)/2];
mask = [0 0; 0 0; 1/3 0; 0 1/3; 1/2 sqrt(3)/6; ...
-sqrt(3)/6 1/2; 2/3 0; 0 2/3];
n = size(mask,1)/2;
% subdivision of edges
% p(:,k), p(:,k+1) = p(:,k) + d(:,k)
% mask(c:2:end,:)*d(:,k) yields
% x- (c=1) and y-coordinates (c=2)
for k=1:steps;
d = diff([p p(:,1)]')';
p = repmat(p,n,1);
p = mask*d+p;
p = reshape(p,2,length(p(:))/2);
end;
% plot polygon
fill(p(1,:),p(2,:),[1 1 1]/2);
axis square;
|
automatisch erstellt
am 10. 5. 2007 |