-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfractal.js
More file actions
51 lines (51 loc) · 1.42 KB
/
fractal.js
File metadata and controls
51 lines (51 loc) · 1.42 KB
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
(function (window, document, undefined) {
var recorder = new Turtle.Recorder();
function drawLine (pixels, depth) {
if (depth > 0) {
drawLine(pixels/3, depth-1);
recorder.turn(60);
drawLine(pixels/3, depth-1);
recorder.turn(-120);
drawLine(pixels/3,depth-1);
recorder.turn(60);
drawLine(pixels/3,depth-1);
} else {
recorder.move(pixels);
}
return;
}
function fractalLine(pixels, depth, turns) {
if (depth > 0) {
turns.map(function(degrees) {
recorder.turn(degrees);
fractalLine(pixels/3, depth-1, turns);
});
} else {
recorder.move(pixels);
}
return;
}
function von_koch (pixels, depth) {
for (var i=3; i > 0; i--) {
fractalLine(pixels, depth, [0, 60, -120, 60]);
recorder.turn(-120);
}
return;
}
function peano (pixels, depth) {
fractalLine(pixels, depth, [0, 90, -90, -90, -90, 90, 90, 90, -90]);
return;
}
function drawFractal(canvas) {
recorder.position({x:5, y:80});
recorder.pendown();
von_koch(90, 3);
recorder.penup();
recorder.position({x:105, y:55});
recorder.pendown();
peano(90, 3);
recorder.play(new Turtle.Line(new Turtle(), canvas.getContext('2d')), 5, 1);
return;
}
Turtle.drawFractal = drawFractal;
})(this, this.document);