-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomCubeGen.pde
217 lines (188 loc) · 5.18 KB
/
RandomCubeGen.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
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
import org.qscript.*;
import org.qscript.editor.*;
import org.qscript.errors.*;
import org.qscript.events.*;
import org.qscript.eventsonfire.*;
import org.qscript.operator.*;
import javax.swing.JOptionPane;
import java.util.Random;
PImage[] pix = new PImage[6];
float rotx = 0;
float roty = 0;
static final int WIDTH = 100;
enum Face {FRONT,BACK,BOTTOM,TOP,RIGHT,LEFT};
void setup() {
for(int complexity = 1; complexity < 10; complexity++) {
Function[] rgbFunc = new Function[3];
println("Complexity of " + complexity);
rgbFunc[0] = new Function(complexity);
rgbFunc[1] = new Function(complexity);
rgbFunc[2] = new Function(complexity);
println(rgbFunc[0].getFunc());
println();
println(rgbFunc[1].getFunc());
println();
println(rgbFunc[2].getFunc());
for(Face f : Face.values()) {
genFace(f, rgbFunc).save("" + complexity + "/side" + f.ordinal() + ".jpg");
System.out.printf("%.2f%% Done with complex. %d%n", (f.ordinal() + 1) * 100 / (double) Face.values().length, complexity);
}
}
}
void keyPressed() {
save();
}
void save() {
for(Face f : Face.values()) {
pix[f.ordinal()].save("/" + f.ordinal() + "/side" + f.ordinal() + ".jpg");
}
}
PImage genFace(Face f, Function[] rgbFunc) {
PImage img = createImage(WIDTH, WIDTH, RGB);
for(int i = 0; i < WIDTH; i++) {
for(int j = 0; j < WIDTH; j++) {
float[] rgb;
switch(f) {
case FRONT:
rgb = getRGBValues(rgbFunc, new int[] {i,j,0});
break;
case BACK:
rgb = getRGBValues(rgbFunc, new int[] {i,j,WIDTH});
break;
case TOP:
rgb = getRGBValues(rgbFunc, new int[] {j,0,i});
break;
case BOTTOM:
rgb = getRGBValues(rgbFunc, new int[] {j,WIDTH,i});
break;
case LEFT:
rgb = getRGBValues(rgbFunc, new int[] {0,j,i});
break;
default:
rgb = getRGBValues(rgbFunc, new int[] {WIDTH,j,i});
}
img.set(i, j, color(rgb[0],rgb[1],rgb[2]));
//println(x + ", " + y + "\tr:" + r + " g:" + g + " b:" + b + "\tfoo:" + foo + " bar:" + bar + " baz:" + baz);
}
//println("row " + i);
}
return img;
}
float[] getRGBValues(Function[] rgbFunc, int[] xyz) {
float d = map(xyz[0], 0, WIDTH, -1, 1);
float e = map(xyz[1], 0, WIDTH, -1, 1);
float f = map(xyz[2], 0, WIDTH, -1, 1);
float r = map(rgbFunc[0].eval(d,e,f),-1,1,0,255);
float g = map(rgbFunc[1].eval(d,e,f),-1,1,0,255);
float b = map(rgbFunc[2].eval(d,e,f),-1,1,0,255);
return new float[] {r,g,b};
}
void draw()
{
background(0);
noStroke();
translate(width/2.0, height/2.0, -100);
rotateX(rotx);
rotateY(roty);
scale(150);
//TexturedCube(pix);
}
void TexturedCube(PImage[] pix) {
beginShape(QUADS);
texture(pix[0]);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex(1, -1, 1, 1, 0);
vertex(1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
texture(pix[1]);
// -Z "back" face
vertex(-1, -1, -1, 0, 0);
vertex(1, -1, -1, 1, 0);
vertex(1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
texture(pix[2]);
// +Y "bottom" face
vertex(-1, 1, 1, 0, 0);
vertex(-1, 1, -1, 1, 0);
vertex(1, 1, -1, 1, 1);
vertex(1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
texture(pix[3]);
// -Y "top" face
vertex(-1, -1, 1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(1, -1, -1, 1, 1);
vertex(1, -1, 1, 0, 1);
endShape();
beginShape(QUADS);
texture(pix[4]);
// +X "right" face
vertex(1, -1, 1, 0, 0);
vertex(1, -1, -1, 1, 0);
vertex(1, 1, -1, 1, 1);
vertex(1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
texture(pix[5]);
// -X "left" face
vertex(-1, -1, 1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex(-1, 1, 1,0, 1);
endShape();
}
void mouseDragged() {
float rate = 0.01;
rotx += (pmouseY-mouseY) * rate;
roty += (mouseX-pmouseX) * rate;
}
class Function {
String str;
Random random = new Random();
Function() {
str = genVar();
}
Function(int num) {
this();
iterate(num);
}
void iterate(int num) {
for(int i = 0; i < num; i++) {
str = str.replaceAll("[xyz]","q");
while(str.contains("q")) {
int rand = random.nextInt(4);
if(rand == 0) {
str = str.replaceFirst("q","sin(PI * " + genVar() + ")");
} else if(rand == 1) {
str = str.replaceFirst("q","cos(PI * " + genVar() + ")");
} else if(rand == 2) {
str = str.replaceFirst("q", "(" + genVar() + " + " + genVar() + ") / 2.0");
} else {
str = str.replaceFirst("q", genVar() + " * " + genVar());
}
}
}
}
String getFunc() {
return str;
}
float eval(float x, float y, float z) {
return Float.valueOf(Solver.evaluate("x = " + x + ";y = " + y + ";z = " + z + ";" + str).toString());
}
String genVar() {
int rand = random.nextInt(3);
if(rand == 0) {
return "x";
} else if(rand == 1) {
return "y";
} else {
return "z";
}
}
}