-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.js
204 lines (172 loc) · 5.3 KB
/
recursion.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const width = 1100;
const height = 400;
const treeDepth = 4;
const ellipsesHeight = 100;
const animationInterval = 1000;
let nodes = [];
let nodeIdx = 0;
let nodeTexts = ['n=10,W=10', 'n=9,W=10', 'n=8,W=10', 'n=7,W=10',
'n=7,W=8', 'n=8,W=7', 'n=7, W=7', 'n=7,W=5', 'n=9,W=7', 'n=8,W=7', 'n=7,W=7', 'n=7,W=5', 'n=8,W=4', 'n=7,W=4', 'n=7,W=2'];
let cnt = 0;
function drawRecursionTree() {
let canvas = document.getElementById("recursion-canvas");
canvas.height = height;
canvas.width = width;
let ctx = canvas.getContext("2d");
drawTree(ctx, {
'startx': 0,
'starty': 0,
'endx': width,
'endy': (height - ellipsesHeight) / treeDepth,
'levels': treeDepth
});
drawVerticalEllipses(ctx);
drawLeftDiagEllipses(ctx);
drawRightDiagEllipses(ctx);
setInterval(highlightNode, animationInterval, ctx, {});
}
function drawVerticalEllipses(ctx) {
let radius = 7;
for (let i = 0; i < 3; i++) {
drawCircle(ctx, {
'centerX': width / 2,
'centerY': height - ellipsesHeight + (i+1) * (ellipsesHeight - 10) / 3,
'radius': radius
});
}
}
function drawLeftDiagEllipses(ctx) {
let radius = 7;
for (let i = 0; i < 3; i++) {
drawCircle(ctx, {
'centerX': width / 9 - i * 30,
'centerY': height - ellipsesHeight + (i+1) * (ellipsesHeight - 10) / 3,
'radius': radius
});
}
}
function drawRightDiagEllipses(ctx) {
let radius = 7;
for (let i = 0; i < 3; i++) {
drawCircle(ctx, {
'centerX': 8 * width / 9 + i * 30,
'centerY': height - ellipsesHeight + (i+1) * (ellipsesHeight - 10) / 3,
'radius': radius
});
}
}
function drawCircle(ctx, circleProps) {
ctx.beginPath();
ctx.arc(
circleProps.centerX,
circleProps.centerY,
circleProps.radius,
0,
2 * Math.PI,
false
);
ctx.fillStyle = 'black';
ctx.fill();
ctx.line
}
function drawTree(ctx, treeProps) {
if (treeProps.levels === 0) {
return;
}
let nodeProps = {
'startx': treeProps.startx,
'starty': treeProps.starty,
'endx': treeProps.endx,
'endy': (treeProps.starty + treeProps.endy) / 2
};
drawNode(ctx, nodeProps);
let branchProps = {
'startx': treeProps.startx,
'starty': (treeProps.starty + treeProps.endy) / 2,
'endx': treeProps.endx,
'endy': treeProps.endy
};
drawLeftBranch(ctx, branchProps);
drawRightBranch(ctx, branchProps);
// draw left subtree
drawTree(ctx, {
'startx': treeProps.startx,
'starty': treeProps.endy,
'endx': (treeProps.startx + treeProps.endx) / 2,
'endy': treeProps.endy + (treeProps.endy - treeProps.starty),
'levels': treeProps.levels - 1,
});
// draw right subtree
drawTree(ctx, {
'startx': (treeProps.startx + treeProps.endx) / 2,
'starty': treeProps.endy,
'endx': treeProps.endx,
'endy': treeProps.endy + (treeProps.endy - treeProps.starty),
'levels': treeProps.levels - 1,
});
}
function drawNode(ctx, props) {
let rectProps = {
'topLeftX': props.startx + (props.endx - props.startx) / 4,
'topLeftY': props.starty,
'width': (props.endx - props.startx) / 2,
'height': (props.endy - props.starty),
'lineWidth': 1
};
nodes.push(rectProps);
drawRectCanvas(ctx, rectProps);
ctx.fillStyle = 'black';
ctx.font = "13px Arial";
ctx.fillText(nodeTexts[nodeIdx], (props.startx + props.endx) / 2 - 32, (props.starty + props.endy) / 2);
nodeIdx = (nodeIdx + 1) % nodeTexts.length;
}
function highlightNode(ctx, props) {
console.log('nodeIdx ' + nodeIdx);
if (nodeIdx === 0) {
resetCanvas(ctx);
}
let rectProps = nodes[nodeIdx];
rectProps['color'] = 'cadetblue';
rectProps['lineWidth'] = 5;
drawRectCanvas(ctx, rectProps);
nodeIdx = (nodeIdx + 1) % nodeTexts.length;
}
function resetCanvas(ctx) {
ctx.clearRect(0, 0, width, height);
ctx.strokeStyle = 'black';
drawTree(ctx, {
'startx': 0,
'starty': 0,
'endx': width,
'endy': (height - ellipsesHeight) / treeDepth,
'levels': treeDepth
});
drawVerticalEllipses(ctx);
drawLeftDiagEllipses(ctx);
drawRightDiagEllipses(ctx);
}
function drawLeftBranch(ctx, props) {
drawLineCanvas(ctx, (props.startx + props.endx) / 2, props.starty, props.startx + (props.endx - props.startx) / 4, props.endy);
}
function drawRightBranch(ctx, props) {
drawLineCanvas(ctx, (props.startx + props.endx) / 2, props.starty, props.startx + 3 * (props.endx - props.startx) / 4, props.endy);
}
function drawLineCanvas(ctx, startx, starty, endx, endy, width) {
ctx.lineWidth = width;
ctx.beginPath();
ctx.moveTo(startx, starty);
ctx.lineTo(endx, endy);
ctx.stroke();
}
function drawRectCanvas(ctx, props) {
ctx.beginPath();
ctx.rect(props.topLeftX, props.topLeftY, props.width, props.height);
ctx.strokeStyle = props.color;
ctx.lineWidth = props.lineWidth;
ctx.stroke();
}
function fillRectCanvas(ctx, props) {
ctx.beginPath();
ctx.fillStyle = props.color;
ctx.fillRect(props.topLeftX, props.topLeftY, props.width, props.height);
}