Hi there!
I'm kind of new to processing. I took L-system example from Learning\Examples. I've tried to add some slider to control iterations and angle. When i have a lot of string in RuleA, the program is not respond.
So, I have tried to check in the function. I guess the problem happened in simulate function. The second problem, when I try to change the number of iterations on the slider (let's say 4 iterations), the program is take a long time to calculate(very slow). So, anybody knows how to fix this.
import controlP5.*; LSystem ps; ControlP5 controlP5; int iterations = 2; float angle = 360; void setup() { size(700, 700); frameRate(24); controlP5 = new ControlP5(this); controlP5.addSlider("iterations",0,9,iterations,20,420,20,100); controlP5.addSlider("Angle",1,360,angle,20,550,20,100); ps = new LSystem(); ps.simulate(iterations,2); } public void iterations(int newIteration) { ps.reset(); ps.simulate(newIteration,newIteration); } public void Angle(float newAngle) { //ps.reset(); angle = newAngle; }
void draw() { stroke(255); background(0); ps.render(); smooth(); } class LSystem { int steps = 0; int step_inc = 1; String axiom; String ruleA; String ruleB; String production; float startLength; float drawLength; float theta; int generations; LSystem() { axiom = "F+F"; ruleA = "+F+F-F-F+F+F+F+F+F+F+F-F-F-F-F "; //Let's say I have a lot of string here!(may be 100)
ruleB = "F-F-F+FF"; startLength = 130.0f; theta = radians(1); reset(); } void reset() { production = axiom; drawLength = startLength; generations = 0; steps = 0; } int getAge() { return generations; } void render() { pushMatrix(); translate(width/2, height/2); steps += step_inc; if (steps > production.length()) { steps = production.length(); } for (int i = 0; i < steps; i++) { char step = production.charAt(i); if ((step == 'F') || (step == 'B')) { fill(255,20); line(0, 0, drawLength, 0); translate(drawLength, 0); } else if (step == '+') { rotate(-theta*angle); } else if (step == '-') { rotate(+theta*angle); } } popMatrix(); } void simulate(int gen, int step_inc_) { while (getAge() < gen) { production = iterate(production, ruleA, ruleB); } step_inc = step_inc_; } String iterate(String prod_, String ruleA_, String ruleB_) { drawLength = drawLength * 0.6; generations++; String newProduction = ""; for (int i = 0; i < prod_.length(); i++) { if (prod_.charAt(i) == 'F') { println("******"+generations+" "+prod_.length()); newProduction += ruleA_; // } else if (prod_.charAt(i) == 'B') { // newProduction += ruleB_; } else { newProduction += prod_.charAt(i); } } return newProduction; } } |
This is what I have tried but it's doesn't work well..
******************************************************
String iterate(String prod_, String ruleA_, String ruleB_) {
drawLength = drawLength * 0.6;
generations++;
// char[] c=new char[prod_.length()*ruleA_.length()];
// char[] ruleAChar=ruleA_.toCharArray();
// String newProduction=new String(c);
// c=null;
// int index=0;
String newProduction = "";
for (int i = 0; i < prod_.length(); i++) {
if (prod_.charAt(i) == 'F') {
//
//
// for(int j=0;j<ruleAChar.length;j++){
// c[j+index]=ruleAChar[j];
// }
// index=index+ruleAChar.length;
// println("******"+generations+" "+index+" "+prod_.length());
newProduction += ruleA_;
} else {
// c[index] = prod_.charAt(i);
// index++;
newProduction += prod_.charAt(i);
}
}
return newProduction;
//char c2[]=new char[index];
//println(index+" "+c.length);
//for(int i=0;i<c2.length;i++)
// c2[i]=c[i];
//
// c=null;
//
//return (new String(c2));
}
}
thanks
patty