-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeter_de_jonge_attractor.js
225 lines (203 loc) · 6.78 KB
/
peter_de_jonge_attractor.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
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
// Attractor分形绘制 + Flame算法 + Density Esitimation + Tone mapping进行图像渲染
var attrac;
var offsetWidth; // tanslate的偏移量
var offsetHeight;
function setup() {
createCanvas(300, 300);
offsetHeight = height / 2; // 移动到中心
offsetWidth = width / 2;
attrac = new myAttractor(width, height);
attrac.makePoints();
attrac.toSymmetric();
}
function draw() {
background(255);
translate(offsetWidth, offsetHeight); //平移至中心
attrac.display();
}
function myAttractor(w, h){
this.a = random(-3, 3); // 迭代函数的参数
this.b = random(-3, 3);
this.c = random(-3, 3);
this.d = random(-3, 3);
this.e = random(-3, 3);
this.f = random(-3, 3);
console.log(this.a, this.b, this.c, this.d, this.e, this.f);
this.maxIter = 2000000; // 最多迭代次数
this.scale = floor(min(w, h) * 0.4); // 放大的倍数
this.width = w; // 图像大小
this.height = h;
this.freqMax = 0; // 最高像素点频率
this.gamma = 2.0; // gamma渲染
this.itst = new iterStruc(); // 迭代过程的结果保存,xn yn color等等
this.points = []; // 像素点
this.redParam1 = 0.0; // 颜色前面的系数
this.greenParam1 = 0.0;
this.blueParam1 = 0.0;
this.redParam2 = 0.0;
this.greenParam2 = 0.0;
this.blueParam2 = 0.0;
// 构造像素点
this.makePoints = function(){
// 初始化
for(var j = 0; j < this.height; j++){
var rowPoints = [];
for(var i = 0; i < this.width; i++){
rowPoints.push(new colorPoint(j, i));
}
this.points.push(rowPoints);
}
// 迭代
for(var k = 0; k < this.maxIter; k++){
this.iterateFunction();
}
// 求最高频率,然后利用频率进行对数归一化得到alpha
for(var j = 0; j < this.height; j++){
for(var i = 0; i < this.width; i++){
this.freqMax = max(this.freqMax, this.points[j][i].freq);
}
}
var toDivide = log(this.freqMax);
if(toDivide != 0.0){
for(var j = 0; j < this.height; j++){
for(var i = 0; i < this.width; i++){
if(this.points[j][i].freq != 0.0){
this.points[j][i].alpha = log(this.points[j][i].freq) / toDivide;
}
}
}
}
// gamma渲染,增强图像
for(var j = 0; j < this.height; j++){
for(var i = 0; i < this.width; i++){
this.points[j][i].color = this.points[j][i].color * pow(this.points[j][i].alpha, 1 / this.gamma);
if(j % 50 == 0 && i % 50 == 0){
console.log(this.points[j][i].color);
}
}
}
}
// 将像素点进行对称
this.toSymmetric = function(){
var rn = floor(random(0, 3));
console.log(rn);
if(rn == 0){
// 上下对称
for(var j = floor(this.height)/2; j < this.height; j++){
for(var i = 0; i < this.width; i++){
var p1 = this.points[j][i];
var p2 = this.points[this.height - 1 - j][i];
var freqSum = floor(p1.freq + p2.freq);
var colorMean = (p1.color + p2.color)/2;
this.points[j][i].freq = freqSum;
this.points[j][i].color = colorMean;
this.points[this.height - j - 1][i].freq = freqSum;
this.points[this.height - j - 1][i].color = colorMean;
}
}
}else if(rn == 1){
// 左右
for(var j = 0; j < this.height; j++){
for(var i = floor(this.width/2); i < this.width; i++){
var p1 = this.points[j][i];
var p2 = this.points[j][this.width - i];
var freqSum = floor(p1.freq + p2.freq);
var colorMean = (p1.color + p2.color)/2;
this.points[j][i].freq = freqSum;
this.points[j][i].color = colorMean;
this.points[j][this.width - i - 1].freq = freqSum;
this.points[j][this.width - i - 1].color = colorMean;
}
}
}else if(rn == 2){
// 中心对称
for(var j = 0; j < this.height; j++){
for(var i = floor(this.width/2); i < this.width; i++){
var p1 = this.points[j][i];
var p2 = this.points[this.height - 1 - j][this.width - 1 - i];
var freqSum = floor(p1.freq + p2.freq);
var colorMean = (p1.color + p2.color)/2;
this.points[j][i].freq = freqSum;
this.points[j][i].color = colorMean;
this.points[this.height - 1 - j][this.width - 1 - i].freq = freqSum;
this.points[this.height - 1 - j][this.width - 1 - i].color = colorMean;
}
}
}
}
// 逐个像素点显示
this.display = function(){
this.randomSetColorParams();
beginShape(POINTS);
for(var j = 0; j < this.height; j++){
for(var i = 0; i < this.width; i++){
var r = this.toRed(this.points[j][i].color);
var g = this.toGreen(this.points[j][i].color);
var b = this.toBlue(this.points[j][i].color);
stroke(r, g, b);
for(var k = 0; k < this.points[j][i].freq; k++){
point(j - offsetHeight, i - offsetWidth);
}
}
}
endShape();
}
// 对点进行迭代,包括坐标和颜色
this.iterateFunction = function(){
var xn1 = sin(this.a * this.itst.yn) - cos(this.b * this.itst.xn);
var yn1 = sin(this.c * this.itst.xn) - cos(this.d * this.itst.yn);
var color = sin(this.e * this.itst.xn) - cos(this.f * this.itst.color);
var j = this.toRowIndex(yn1);
var i = this.toColIndex(xn1);
if(j >= 0 && i >= 0 && j < this.height && i < this.width){
this.points[j][i].freq += 1;
this.points[j][i].color = (this.points[j][i].color + color) / 2;
}
this.itst.xn = xn1;
this.itst.yn = yn1;
this.itst.color = (color + this.itst.color) / 2;
}
// 将值转换为行index
this.toRowIndex = function(yn){
return floor(yn * this.scale + offsetHeight);
}
this.toColIndex = function(xn){
return floor(xn * this.scale + offsetWidth);
}
// 生成r颜色
this.toRed = function(color){
var c = abs(color)/1.732;
var r = floor(255 * (this.redParam1 + this.redParam1 * c));
return r;
}
this.toGreen = function(color){
var c = abs(color)/1.732;
var g = floor(255 * (this.greenParam1 + this.greenParam2 * c));
return g;
}
this.toBlue = function(color){
var c = abs(color)/1.732;
var b = floor(255 * (this.blueParam1 + this.blueParam2 * c));
return b;
}
this.randomSetColorParams = function(){
this.redParam1 = random(0, 1); // 颜色前面的系数
this.greenParam1 = random(0, 1);
this.blueParam1 = random(0, 1);
this.redParam2 = random(-this.redParam1, 1 - this.redParam1);
this.greenParam2 = random(-this.greenParam1, 1 - this.greenParam1);
this.blueParam2 = random(-this.blueParam1, 1 - this.blueParam1);
}
}
function iterStruc(){
this.xn = 0.0;
this.yn = 0.5;
this.color = 0;
}
function colorPoint(j, i){
this.j = j; // 在图像上的坐标
this.i = i;
this.color = 0; // 颜色
this.freq = 0; // 计数
this.alpha = 1.0; // 颜色透明度
}