ArrayList<Gshape> island;
int maxLevel = 1;
float time = 0;
int added = 0;
boolean BandWmode;
void setup() {
size(800, 800);
colorMode(HSB, 255);
BandWmode = false;
}
void draw() {
time = map(mouseX, 0, width, 0, 500);
maxLevel = int(map(time, 0, 500, 0, 6));
//if (time % 100 == 0) {
//maxLevel ++;
//}
added = 0;
if (! BandWmode) {
background(255);
} else {
background(255/2.0);
}
island = new ArrayList<Gshape>();
//seed shape
Gshape seed;
seed = new Gshape();
for (float T = 0; T < TWO_PI-0.0001; T += TWO_PI/6) {
seed.addVertex(cos(T), sin(T));
}
island.add(seed);
float scale = map(time, 0, 500, 125, 5)/800*width;
//steps through the levels
for (int j = 0; j < maxLevel; j++) {
//recursively (not really) steps through each object and makes 6 more
for (int i = island.size()-1; i >= 0; i--) {
Gshape g = island.get(i);
float offset = 0;
if (j % 2 == 1) {
// need to rotate every other level
offset = asin(sqrt(3)/(2*sqrt(7)));
}
for (float T = PI/6 + offset; T < offset + PI/6 + TWO_PI - 0.0001; T += TWO_PI/6) {
Gshape h = new Gshape();
h = g.deepCopy();
float a = 0;
if (j == 0) {
a = 1.40375;
} else if (j == 1) {
a = 1.40375;
} else if (j == 2) {
a = 1.77125;
} else if (j == 3) {
a = 2.1056;
} else if (j == 4) {
a = 2.4206;
} else if (j == 5) {
a = 2.7205;
} else {
a = 0.3281 * j + 1.09948;
}
////a = 2;
float mult = pow(j+1, a);
h.translatePoints(mult*sqrt(3)*cos(T), mult*sqrt(3)*sin(T));
island.add(h);
}
}
scale *= 0.826;//map(mouseY, height,0, 1, 0.40);
}
for (Gshape part : island) {
part.changeScale(scale);
part.display(width/2, height/2);
part.changeScale(1.0/scale);
}
}
void mouseClicked() {
if (BandWmode) {
BandWmode = false;
} else {
BandWmode = true;
}
}
class Gshape {
ArrayList<Float> x;
ArrayList<Float> y;
int level;
color c;
Gshape() {
x = new ArrayList<Float>();
y = new ArrayList<Float>();
level = 0;
}
Gshape deepCopy() {
Gshape clone = new Gshape();
for (int i = 0; i < x.size(); i++) {
float xt = x.get(i);
float yt = y.get(i);
clone.x.add(xt);
clone.y.add(yt);
}
clone.level = level + 1;
clone.c = c;
return clone;
}
void addVertex(float xt, float yt) {
x.add(xt);
y.add(yt);
}
void changeScale(float s) {
for (int i = 0; i < x.size(); i++) {
float xt = x.get(i);
xt *= s;
x.set(i, xt);
float yt = y.get(i);
yt *= s;
y.set(i, yt);
}
}
void translatePoints(float xtranslate, float ytranslate) {
for (int i = 0; i < x.size(); i++) {
float xt = x.get(i);
xt += xtranslate;
x.set(i, xt);
float yt = y.get(i);
yt += ytranslate;
y.set(i, yt);
}
}
void display(float ox, float oy) {
c = color(map(level, 0, 8, 0, 255)%255, 255, 255);
//offset of (ox,oy)
beginShape();
stroke(0, 100);
strokeWeight(1);
if (BandWmode) {
if (level % 2 == 0) {
fill(0);
} else {
fill(255);
}
} else {
fill(c);
}
for (int i = 0; i < x.size(); i++) {
float xt = x.get(i);
float yt = y.get(i);
vertex(xt + ox, yt + oy);
}
endShape(CLOSE);
}
}