-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFS.pde
85 lines (75 loc) · 1.46 KB
/
IFS.pde
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
class IFS {
private int w,h; // width, height
private int sx,sy;
private int sw,sh;
private float[][] v;
private float[][] b;
private float[][] a;
private boolean stable;
IFS(int w_, int h_) {
w = w_;
h = h_;
v = new float[h][w];
}
void setParameters(float[][] a_, float[][] b_) {
b = b_;
a = a_;
sx = a.length;
sy = b[0].length;
sw = w / sx;
sh = h / sy;
stable = false;
}
void iterate() {
int x,y;
boolean s = true;
float tmp;
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
x = j / sw;
y = i / sh;
tmp = v[(i%sh)*sy][(j%sw)*sx]*a[x][y] +b[x][y];
if(tmp != v[i][j]) {
v[i][j] = tmp;
s = false;
}
}
}
stable = s;
}
boolean isStable() {
return stable;
}
void evolve() {
stable = false;
}
PImage getImage() {
float max = max(1,maxVal());
float min = minVal();
PImage img = createImage(w,h,ALPHA);
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
img.pixels[i*w+j] = (int)(map(v[i][j],min,max,0,255));
}
}
return img;
}
float minVal() {
float t = MAX_FLOAT;
for(float[] v_ : v) {
for(float a : v_) {
t = min(a,t);
}
}
return t;
}
float maxVal() {
float t = -MAX_FLOAT;
for(float[] v_ : v) {
for(float a : v_) {
t = max(a,t);
}
}
return t;
}
}