-
Notifications
You must be signed in to change notification settings - Fork 3
/
penrose.html
245 lines (204 loc) · 9.3 KB
/
penrose.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<html>
<head>
<meta charset="utf-8"/>
<style type="text/css">
html,body {
height:100%;
}
#wrapper {
width: 100%;
height: 90%;
display:flex;
}
#menu {
width: 20%;
height: 100%;
float: left;
}
#canvasWrapper {
width: 80%;
height: 100%;
float: left;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="menu">
<fieldset>
<p>Initial shape</p>
<div>
<input type="radio" name="init_shape" value="rhombus" checked="checked">
<label>Rhombus</label>
<input type="radio" name="init_shape" value="circle">
<label>Circle</label>
</div>
</fieldset>
<fieldset>
<p>Depth level</p>
<input type="range" id="level" name="Level" min="0" max="8" oninput="levelOutput.value = level.value" value="4" />
<output id="levelOutput">4</output>
</fieldset>
<input type="button" onclick="drawPenroseTiling();" value="Draw" />
</div>
<div id="canvasWrapper">
<canvas id="main" width="700px" height="600px">
</canvas>
</div>
</div>
<script type="text/javascript">
var GOLDEN_RATIO = 0.6180339887498948482;
// Used to represent both points and vectors for simplicity
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
static fromPoints(start, end) {
return new Vector(end.x - start.x, end.y - start.y);
}
multiply(multiplier) {
return new Vector(this.x * multiplier, this.y * multiplier);
}
add(anotherVector) {
return new Vector(this.x + anotherVector.x, this.y + anotherVector.y);
}
}
class Triangle {
constructor(v1, v2, v3, fillColor) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.fillColor = fillColor;
}
draw(ctx) {
// Store fill style in a temp variable, to set it back later
var tempFillStyle = ctx.fillStyle;
ctx.fillStyle = this.fillColor;
ctx.beginPath();
ctx.moveTo(this.v1.x, this.v1.y);
ctx.lineTo(this.v2.x, this.v2.y);
ctx.lineTo(this.v3.x, this.v3.y);
ctx.lineTo(this.v1.x, this.v1.y);
ctx.fill();
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(this.v1.x, this.v1.y);
ctx.lineTo(this.v2.x, this.v2.y);
ctx.stroke();
ctx.moveTo(this.v1.x, this.v1.y);
ctx.lineTo(this.v3.x, this.v3.y);
ctx.stroke();
ctx.fillStyle = tempFillStyle;
}
}
class ThinLeftTriangle extends Triangle {
constructor(v1, v2, v3) {
super(v1, v2, v3, 'blue');
}
split() {
var vector_13 = Vector.fromPoints(this.v1, this.v3).multiply(GOLDEN_RATIO);
var split_point_13 = this.v1.add(vector_13);
var new_triangles = []
new_triangles.push(new ThinLeftTriangle(this.v2, this.v3, split_point_13));
new_triangles.push(new ThickLeftTriangle(split_point_13, this.v1, this.v2));
return new_triangles;
}
}
class ThinRightTriangle extends Triangle {
constructor(v1, v2, v3) {
super(v1, v2, v3, 'blue');
}
split() {
var vector_12 = Vector.fromPoints(this.v1, this.v2).multiply(GOLDEN_RATIO);
var split_point_12 = this.v1.add(vector_12);
var new_triangles = []
new_triangles.push(new ThinRightTriangle(this.v3, split_point_12, this.v2));
new_triangles.push(new ThickRightTriangle(split_point_12, this.v3, this.v1));
return new_triangles;
}
}
class ThickLeftTriangle extends Triangle {
constructor(v1, v2, v3) {
super(v1, v2, v3, 'red');
}
split() {
var vector_32 = Vector.fromPoints(this.v3, this.v2).multiply(GOLDEN_RATIO);
var split_point_32 = this.v3.add(vector_32);
var vector_31 = Vector.fromPoints(this.v3, this.v1).multiply(GOLDEN_RATIO);
var split_point_31 = this.v3.add(vector_31);
var new_triangles = [];
new_triangles.push(new ThickRightTriangle(split_point_31, split_point_32, this.v3));
new_triangles.push(new ThinRightTriangle(split_point_32, split_point_31, this.v1));
new_triangles.push(new ThickLeftTriangle(split_point_32, this.v1, this.v2));
return new_triangles;
}
}
class ThickRightTriangle extends Triangle {
constructor(v1, v2, v3) {
super(v1, v2, v3, 'red');
}
split() {
var vector_21 = Vector.fromPoints(this.v2, this.v1).multiply(GOLDEN_RATIO);
var split_point_21 = this.v2.add(vector_21);
var vector_23 = Vector.fromPoints(this.v2, this.v3).multiply(GOLDEN_RATIO);
var split_point_23 = this.v2.add(vector_23);
var new_triangles = [];
new_triangles.push(new ThickRightTriangle(split_point_23, this.v3, this.v1));
new_triangles.push(new ThinLeftTriangle(split_point_23, this.v1, split_point_21));
new_triangles.push(new ThickLeftTriangle(split_point_21, this.v2, split_point_23));
return new_triangles;
}
}
function drawPenroseTiling() {
var rounds = document.getElementById("level").value;
var init_shape = document.querySelector('input[name="init_shape"]:checked').value;
var canvas = document.getElementById("main");
var ctx = canvas.getContext('2d');
var triangles = [];
if (init_shape === 'rhombus') {
var side = Math.min(canvas.width, canvas.height);
var ratio = Math.sin(36 * (Math.PI / 180)) / Math.sin(54 * (Math.PI / 180));
var t1 = new ThickRightTriangle(new Vector(side / 2.0, 0), new Vector(side, side / 2.0 * ratio), new Vector(0, side / 2.0 * ratio));
var t2 = new ThickLeftTriangle(new Vector(side / 2.0, side * ratio), new Vector(0, side / 2.0 * ratio), new Vector(side, side / 2.0 * ratio));
triangles.push(t1);
triangles.push(t2);
}
if (init_shape === 'circle') {
var side = Math.min(canvas.width, canvas.height);
var r = side / 2.0;
var grad_increment = 36 * (Math.PI / 180);
var center = new Vector(side / 2.0, side / 2.0);
for (var i = 0; i < 10; i++) {
var v1 = center.add(new Vector(Math.cos(grad_increment * i), Math.sin(grad_increment * i)).multiply(r));
var v2 = center.add(new Vector(Math.cos(grad_increment * (i+1)), Math.sin(grad_increment * (i+1))).multiply(r));
var trig_class;
if (i % 2 == 0) {
trig_class = ThinRightTriangle;
} else {
trig_class = ThinLeftTriangle;
}
var trig = new trig_class(center, v2, v1);
triangles.push(trig);
}
}
triangles.forEach(function(t){
t.draw(ctx);
})
for (var round = 0; round < rounds; round++) {
var new_triangles = [];
for (var i = 0; i < triangles.length; i++) {
var trig = triangles[i];
new_triangles = new_triangles.concat(trig.split());
}
triangles = new_triangles;
ctx.clearRect(0, 0, canvas.width, canvas.height);
triangles.forEach(function(t){
t.draw(ctx);
})
}
}
</script>
</body>
</html>