forked from mathworks/awesome-matlab-students
-
Notifications
You must be signed in to change notification settings - Fork 1
/
A_Pythagorean_Treehouse.m
90 lines (67 loc) · 2 KB
/
A_Pythagorean_Treehouse.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
animateFrames();
function animateFrames()
animFilename = 'A_Pythagorean_Treehouse.gif'; % Output file name
firstFrame = true;
framesPerSecond = 24;
delayTime = 1/framesPerSecond;
% Create the gif
for frame = 1:48
drawframe(frame);
fig = gcf();
fig.Units = 'pixels';
fig.Position(3:4) = [300,300];
im = getframe(fig);
[A,map] = rgb2ind(im.cdata,256);
if firstFrame
firstFrame = false;
imwrite(A,map,animFilename, 'LoopCount', Inf, 'DelayTime', delayTime);
else
imwrite(A,map,animFilename, 'WriteMode', 'append', 'DelayTime', delayTime);
end
end
end
function drawframe(f)
theta = interp1([0 48],[0 pi/2],f);
clf
hgt = hgtransform(Parent=gca);
c = 1;
maxdepth = 6;
pythag_seg(hgt,c,theta,maxdepth);
x = c*[0 0 1 1];
y = c*[0 1 1 0];
cmap = parula(maxdepth+1);
p = patch(x,y,cmap(1,:));
axis equal
axis([-4 4.5 0 7.5])
axis off
set(gcf,Color='white')
end
function pythag_seg(hgtp,c,theta,maxdepth,depth)
if nargin < 5
depth = 1;
end
depth = depth + 1;
if depth > maxdepth
return
end
hgtc1 = hgtransform(Parent=hgtp);
hgtc2 = hgtransform(Parent=hgtp);
x = c*[0 0 1 1];
y = c*[0 1 1 0];
cmap = parula(maxdepth+1);
color = cmap(depth+1,:);
patch(x,y,color,EdgeColor='black',Parent=hgtc1);
patch(x,y,color,EdgeColor='black',Parent=hgtc2);
a = c*cos(theta);
b = c*sin(theta);
Txy = makehgtform('translate',0,c,0);
Rz = makehgtform('zrotate',theta);
S = makehgtform('scale',a/c);
set(hgtc1,Matrix=Txy*Rz*S);
Txy = makehgtform('translate',a*cos(theta),c+a*sin(theta),0);
Rz = makehgtform('zrotate',theta-pi/2);
S = makehgtform('scale',b/c);
set(hgtc2,Matrix=Txy*Rz*S);
pythag_seg(hgtc1,c,theta,maxdepth,depth)
pythag_seg(hgtc2,c,theta,maxdepth,depth)
end