-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractal.js
139 lines (135 loc) · 4.96 KB
/
fractal.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
var fractals = [];
function NFlake(sides, config, canvas) {
sides = sides || 3;
canvas = canvas || document.getElementById('canvas');
config = config || {};
var scale = config.scale || 1,
side = config.side || 1024;
canvas.width = canvas.height = side * scale;
var radius = config.radius || side / 2,
ratio = config.ratio,
step = config.step || 5000,
points = [],
iterationsLeft = config.iterations || Infinity,
x = config.centerX || side / 2,
y = config.centerY || side / 2,
originalX = x,
originalY = y,
spectrum = config.spectrum;
if (!ratio)
ratio = 1/(2*(1+sum(function(_) { return cos(360 * _ / sides); }, floor(sides/4) + 1, 1)));
var context = canvas.getContext('2d'),
rest = 1 - ratio,
obj = { handle: 0, pause: function() { clearTimeout(this.handle); return this; }, delete: function() { this.pause(); delete chaosGames[this.Index]; return this; } };
for (var i = 0; i < sides; i++)
points.push([x+radius*sin(i*(360/sides)), y+radius*cos(i*(360/sides))]);
var draw = spectrum ?
function(n) {
for (var i = 0; i < (n || min(step, iterationsLeft)); i++) {
var pointNumber = Math.floor(Math.random() * sides);
x = round(points[pointNumber][0]*rest + x*ratio);
y = round(points[pointNumber][1]*rest + y*ratio);
var deltaX = x - originalX,
deltaY = y - originalY;
context.fillStyle = 'hsl(' + ((floor(atan(deltaY/deltaX))||0)+(deltaX<0?180:0)) + ',' + round((sqrt(deltaX*deltaX+deltaY*deltaY)/radius)*100) + '%,50%)';
point(x, y, context, scale);
}
iterationsLeft -= n || min(step, iterationsLeft);
if (iterationsLeft && !n)
obj.handle = setTimeout(draw, 0);
} :
function(n) {
for (var i = 0; i < (n || min(step, iterationsLeft)); i++) {
var pointNumber = Number.random(sides);
point(x = round(points[pointNumber][0]*rest + x*ratio), y = round(points[pointNumber][1]*rest + y*ratio), context, scale);
}
iterationsLeft -= n || min(step, iterationsLeft);
if (iterationsLeft && !n)
obj.handle = setTimeout(draw, 0);
};
context.fillStyle = config.color || 'rgba(0,0,0,0.1)';
if (config.start !== false)
draw();
obj.resume = function() { draw(); return this; };
obj.step = function(n) { draw(n); return this; };
obj.index = fractals.length;
fractals.push(obj);
return obj;
}
function Vicsek(config, canvas) {
canvas = canvas || document.getElementById('canvas');
config = config || {};
var scale = config.scale || 1,
side = config.side || 1024;
canvas.width = canvas.height = side * scale;
var step = config.step || 5000,
radius = config.radius || side/2,
context = canvas.getContext('2d'),
x = config.centerX || side / 2,
y = config.centerY || side / 2,
points = [[x, y], [0, 0], [0, 2*y], [2*x, 2*y], [2*x, 0]],
iterationsLeft = config.iterations || Infinity,
obj = { handle: 0, pause: function() { clearTimeout(this.handle); return this; }, delete: function() { this.pause(); delete chaosGames[this.Index]; return this; } },
originalX = x,
originalY = y,
spectrum = config.spectrum;
var draw = spectrum ? function(n) {
for (var i = 0; i < (n || min(step, iterationsLeft)); i++) {
var pointNumber = Math.floor(Math.random() * 5);
x = round(points[pointNumber][0]*2/3 + x*1/3);
y = round(points[pointNumber][1]*2/3 + y*1/3);
var deltaX = x - originalX;
var deltaY = y - originalY;
context.fillStyle = 'hsl(' + ((floor(atan(deltaY/deltaX))||0)+(deltaX<0?180:0)) + ',' + round((sqrt(deltaX*deltaX+deltaY*deltaY)/radius)*100) + '%,50%)';
point(x, y, context, scale);
}
iterationsLeft -= n || min(step, iterationsLeft);
if (iterationsLeft && !n)
obj.handle = setTimeout(draw, 0);
} :
function(n) {
for (var i = 0; i < (n || min(step, iterationsLeft)); i++) {
var pointNumber = Math.floor(Math.random() * 5);
point(x = round(points[pointNumber][0]*2/3 + x*1/3), y = round(points[pointNumber][1]*2/3 + y*1/3), context, scale);
}
iterationsLeft -= n || min(step, iterationsLeft);
if (iterationsLeft && !n)
obj.handle = setTimeout(draw, 0);
};
context.fillStyle = config.color || 'rgba(0,0,0,0.1)';
if (config.start !== false)
draw();
obj.resume = function() { draw(); return this; };
obj.step = function(n) { draw(n); return this; };
obj.index = fractals.length;
fractals.push(obj);
return obj;
}
function clearAllFractals() {
for (var i = 0; i < fractals.length; i++) {
if (fractals[i])
fractals[i].pause();
delete fractals[i];
}
clear2d();
}
function Lindenmayer(rules, initial, actions, iterations, setup, angle) {
var state = initial,
newState = [],
turtle = { angle: angle },
data = {};
for (var i = 0; i < iterations; i++) {
for (var j = 0; j < initial.length; j++)
newState = newState.concat(rules[initial[j]] || [initial[j]]); //TODO: is there a faster ES5 method?
state = newState;
newState = [];
}
setup(data);
for (var i = 0; i < state.length; i++) {
var action = actions[state[i]];
if (!action) //no-op/evolution control
continue;
//
}
}
//TODO: lindenmayer systems, local catenativity detection