forked from higham/matlab-guide-3ed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkoch.m
More file actions
23 lines (18 loc) · 734 Bytes
/
koch.m
File metadata and controls
23 lines (18 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function koch(pl,pr,level)
%KOCH Recursively generated Koch curve.
% KOCH(pl, pr, level) recursively generates a Koch curve,
% where pl and pr are the current left and right endpoints and
% level is the level of recursion.
if level == 0
plot([pl(1),pr(1)],[pl(2),pr(2)],'b'); % Join pl and pr.
hold on
else
A = (sqrt(3)/6)*[0 1; -1 0]; % Rotate/scale matrix.
pmidl = (2*pl + pr)/3;
koch(pl,pmidl,level-1) % Left branch.
ptop = (pl + pr)/2 + A*(pl-pr);
koch(pmidl,ptop,level-1) % Left mid branch.
pmidr = (pl + 2*pr)/3;
koch(ptop,pmidr,level-1) % Right mid branch.
koch(pmidr,pr,level-1) % Right branch.
end