-
Notifications
You must be signed in to change notification settings - Fork 0
/
sierpinski.html
132 lines (109 loc) · 2.7 KB
/
sierpinski.html
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
<html>
<head><title>Sierpinski Curves</title></head>
<script type="text/javascript">
function Ctxt(canvas, x, y, unit) {
this.x = x;
this.y = y;
this.c = canvas;
this.c.moveTo(x, y);
this.u = unit;
this.lineUp = function() {
this.y = this.y - 2 * this.u;
this.draw();
}
this.lineUpLeft = function() {
this.x = this.x - this.u;
this.y = this.y - this.u;
this.draw();
}
this.lineUpRight = function() {
this.x = this.x + this.u;
this.y = this.y - this.u;
this.draw();
}
this.lineRight = function() {
this.x = this.x + 2 * this.u;
this.draw();
}
this.lineDown = function() {
this.y = this.y + 2 * this.u;
this.draw();
}
this.lineDownLeft = function() {
this.x = this.x - this.u;
this.y = this.y + this.u;
this.draw();
}
this.lineDownRight = function() {
this.x = this.x + this.u;
this.y = this.y + this.u;
this.draw();
}
this.lineLeft = function() {
this.x = this.x - 2 * this.u;
this.draw();
}
this.draw = function() {
this.c.lineTo(this.x, this.y);
this.c.stroke();
}
}
function A(ctxt, i) {
if (i == 0)
return;
A(ctxt, i - 1); ctxt.lineDownRight();
B(ctxt, i - 1); ctxt.lineRight();
D(ctxt, i - 1); ctxt.lineUpRight();
A(ctxt, i - 1);
}
function B(ctxt, i) {
if (i == 0)
return;
B(ctxt, i - 1); ctxt.lineDownLeft();
C(ctxt, i - 1); ctxt.lineDown();
A(ctxt, i - 1); ctxt.lineDownRight();
B(ctxt, i - 1);
}
function C(ctxt, i) {
if (i == 0)
return;
C(ctxt, i - 1); ctxt.lineUpLeft();
D(ctxt, i - 1); ctxt.lineLeft();
B(ctxt, i - 1); ctxt.lineDownLeft();
C(ctxt, i - 1);
}
function D(ctxt, i) {
if (i == 0)
return;
D(ctxt, i - 1); ctxt.lineUpRight();
A(ctxt, i - 1); ctxt.lineUp();
C(ctxt, i - 1); ctxt.lineUpLeft();
D(ctxt, i - 1);
}
function draw() {
var e = document.getElementById("S");
var c = e.getContext("2d");
c.strokeStyle = "darkGrey";
c.strokeRect(0, 0, e.width, e.height);
c.strokeStyle = "black";
var u = e.height / 4;
var x = e.width / 2;
var y = e.height / 2 - u;
for (var i = 1; i < 6; ++i) {
x = x - u;
u = u / 2;
y = y - u;
c.beginPath();
var ctxt = new Ctxt(c, x, y, u);
A(ctxt, i); ctxt.lineDownRight();
B(ctxt, i); ctxt.lineDownLeft();
C(ctxt, i); ctxt.lineUpLeft();
D(ctxt, i); ctxt.lineUpRight();
c.closePath();
}
}
</script>
<body onload="draw()">
<canvas id="S" width="1024" height="1024"></canvas>
</body>
</html>