forked from FlorianWendelborn/sierpinski
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
71 lines (49 loc) · 1008 Bytes
/
main.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
var canvas, ctx;
var steps;
var width, height;
var points = [];
function polygon (n) {
var m = {
x:width/2,
y:height/2
}
var r = (m.x < m.y)?m.x:m.y;
var a = 2 * Math.PI / n;
for (var i = 0; i < n; i++) {
points.push({
x: m.x + r * Math.cos(i * a+Math.PI/2),
y: m.y - r * Math.sin(i * a+Math.PI/2)
});
}
}
var x,y;
function step (s,n) {
x = x || points[0].x;
y = y || points[0].y;
for (var i = s; i < n; i++) {
var a = Math.floor(Math.random()*points.length);
ctx.fillRect(x,y,1,1);
x = (x+points[a].x)/2;
y = (y+points[a].y)/2;
}
}
window.onload = function () {
canvas = document.getElementsByTagName('canvas')[0];
width = window.innerWidth;
height = window.innerHeight;
canvas.height = height;
canvas.width = width;
ctx = canvas.getContext('2d');
polygon(prompt('polygon count'));
steps = 10000000;
magic();
}
var i = 0;
function magic () {
step(i*10000,i*10000+10000);
if(i*10000<steps) {
setTimeout(function() {
magic();
},1);
}
}