Hi guys....I'm trying to have the numer of links changing accordingly to the X mouse movement....basically I want mouseX to assume the value of _maxLevels....any ideia on how to do that? I know the code is not ready for that but I'm stuck here....thanks!
[code]
//import processing.serial.*;
//Serial port;
int _numChildren = 1;
int _maxLevels = 20;
Branch _trunk;
void setup() {
size(750, 500);
smooth();
newTree();
//port = new Serial(this, Serial.list()[1], 9600); // MOUSE
}
void newTree() {
_trunk = new Branch(2, 0, width/2, height/2);
_trunk.drawMe();
}
void draw() {
background(255);
_trunk.updateMe(width/2, height/2);
_trunk.drawMe();
//port.write(mouseX); // MOUSE
print("X = ");
println(mouseX);
print("Y = ");
println(mouseY);
}
class Branch {
float level, index;
float x, y;
float endx, endy;
float alph;
float len, lenChange;
float rot, rotChange;
Branch[] children = new Branch[0];
Branch(float lev, float ind, float ex, float why) {
level = lev;
alph = 300 / level; // transparency
lenChange = random(10) - 5; // amplitude
rotChange = random(10) - 5; // rotation
if (level < _maxLevels) {
children = new Branch[_numChildren];
for (int x=0; x < _numChildren; x++) {
children[x] = new Branch(level+1, x, endx, endy); }
}
}
void updateMe(float ex, float why) {
x = ex;
y = why;
rot += rotChange; // random movements
// circular movement
if (rot > 360) { rot = 0; }
else if(rot < 0) { rot = 360; }
len -= lenChange;
// amplification
if (len < 0) { lenChange *= -1; }
else if (len > 100) { lenChange *= -1; }
// direction
float radian = radians(rot);
endx = x + (len * cos(radian));
endy = y + (len * sin(radian));
for (int i=0; i<children.length; i++) {
children[i].updateMe(endx, endy);
}
}
void drawMe() {
strokeWeight(15); // thickness
stroke(0, alph); // transparency
line(x, y, endx, endy); // conections
ellipse(x, y, 10, 10); // size
for (int i=0; i < children.length; i=10) {
children[i].drawMe();
}
}
}
[code]
thanks a lot!!
1