We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › stuck at this point // growing problem
Page Index Toggle Pages: 1
stuck at this point // growing problem (Read 789 times)
stuck at this point // growing problem
Jul 17th, 2008, 9:25am
 
Hi everybody, its been a while since ive asked some questions for my program ive been working on... but now i am at a point where i seem to got stuck. Maybe because i didnt planned my programm from the beginning.

I was programming a plant which is created from ShortMessages I recieved on my cellphone, that works pretty well. The fist thing i did when I started was to create the different leafs step by step. After that i wanted do combine 40-50 leafs to a branch, and i did that by moving them up the screen randomly and change the algebraic sign to turn them left and right... i tried to control the beavier of the branch by adding some variables like stepsize random size and so one...

Ok that seems to work too. This is what a branch looks like:
http://www.dec32.de/public/ast.png

This is the point where i dont know how to go further,
as i got about 1000 messages and not only 40.  Until now i created 25 differnt branches and combined them in Illustrator, what is really time consuming and not what i wanted cause i would like to create it only with processing...

So what i im a looking for is a way to create something like this http://www.dec32.de/public/baumskizze.png(photoshopped)

To give you an idea how i did the creation of the branch so far, i included my code and reduced it to only the growing part. It was 4 times that long but i removed the creation of the leafs not to confuse you and replaced it with only ellipses

Quote:


void setup() {
size(800, 650);
smooth();
background(255);
}

//controlling
int count = 30; //number of steps/leafs
int blattDistance=30; //Distance between the steps

//tendencies to the left or right
float randomLeft=-15;
float randomRight=50;

float stammWeight=8;


//Starting Point
int xx=400;
int yy=1300;
float skalierung= 0.5;

//Variables
int organic;
int[] stammX= new int[count+1];
int[] stammY= new int[count+1];


int vzw=-1;

int endBlattCounter=count;
int i=1;  //Startnummer


void draw() {
 scale(skalierung);

 if(i <= count) {
 
 
   organic= organic+int(random(randomLeft,randomRight));    //tendencies to the left or right
   stammX[0]=xx;
   stammY[0]=yy;
   stammX[i]=xx+organic ;

   stammY[i]=yy-i*blattDistance;

   //StammLook
   stroke(#39302C);
   stammWeight=stammWeight-(stammWeight/count);
   strokeWeight(stammWeight);

   //Stamm Drawing
   line(stammX[i],stammY[i],stammX[i-1],stammY[i-1]);  
 
   //Ast
   strokeWeight(stammWeight);

   float astX=vzw*random(30,50);
   float astY=random(20);

   line(stammX[i],stammY[i],stammX[i]+astX,stammY[i]-astY);

   int blattX=int(stammX[i]+astX);
   int blattY=int(stammY[i]-astY);

   noStroke();
   fill(0,255,0);
   ellipse(blattX,blattY,20,20); //this is just a placeholder for the actual leaf creation which is alot more complex

   i = i + 1;
   
   vzw=vzw*-1; //change of sign to turn the leaf from left to right with every step
 }
}



Re: stuck at this point // growing problem
Reply #1 - Jul 17th, 2008, 1:45pm
 
It seems that your way of coding was good enough to prototype your idea, but it's getting more complicated and now you're stuck : I would say the time has come to use classes and objects and discover the wonderful world of object-oriented programming! :-D

For example, you could think of your tree as an object (Tree) containing smaller objects (Branch) themselves containing smaller objects (Leaf). That would help you structure your idea and make your code clearer, undoubtly.
Re: stuck at this point // growing problem
Reply #2 - Jul 17th, 2008, 1:53pm
 
Since i havent used any classes yet and dont know how what would be a good idea to start with. Any references, examples or something like that?
Re: stuck at this point // growing problem
Reply #3 - Jul 17th, 2008, 2:42pm
 
ok, i found a really basic tutorial about classes but it just gives me an overview about how to use them.

If i split it up into 3 parts, tree, branch, leaf. Is the way i "grew" my branch  a good one? im not sure if creating lines going up in different random directions is how you should do that.

could you maybe give me some more tips about how to structure my programm and how to get started and i guess i will give it a try Smiley


Thank you!
Re: stuck at this point // growing problem
Reply #4 - Jul 17th, 2008, 6:31pm
 
Of course. I just didn't have time to go further, but I'm going to post some code so you can learn from it, maybe.
Re: stuck at this point // growing problem
Reply #5 - Jul 17th, 2008, 7:08pm
 
Thats great, i really appreciate  that. Until i will do  my homework and work through some OOP tutorials i found.
Re: stuck at this point // growing problem
Reply #6 - Jul 18th, 2008, 12:24am
 
Ok, here is how I would proceed. I just reordered your code (and made some adjustements to fit in the classes). It could (and should!) be improved, but understanding this may be a good start.

Code:
// global variables
float skalierung = 0.5;
Tree tree;

// setup
void setup() {
size(800, 650);
smooth();
frameRate(10);
tree = new Tree();
}

// main loop
void draw() {
scale(skalierung);
background(255);
tree.grow();
tree.display();
}

// the tree will be created on this model
class Tree {

Branch[] branches; // tree has branches

// constructor (called when a new tree is created)
Tree() {
branches = new Branch[1]; // that new tree will have one branch
branches[0] = new Branch(400, 1300, -15, 50, 8, 30, 30); // create the (first and only) branch
}

// that tree can grow (make each branch grow)
void grow() {
for (int i = 0; i < branches.length; i++) {
branches[i].grow();
}
}

// that tree can be displayed on screen (display each branch)
void display() {
for (int i = 0; i < branches.length; i++) {
branches[i].display();
}
}

}

// all branches will be created on this model
class Branch {

Leaf[] leaves;
int ageMax; // maximum number of leaves
int blattDistance; // Distance between the steps
float randomLeft; // tendencies to the left or right
float randomRight;
float stammWeight;
int xx; //Starting Point
int yy;
int organic;
int vzw=-1;
int age=-1; //Startnummer (kind of 'age' of the branch)

// constructor method, called when you create a new branch
Branch(int _xx, int _yy, int _randomLeft, int _randomRight, int _stammWeight, int _ageMax, int _blattDistance) {
xx = _xx;
yy = _yy;
randomLeft = _randomLeft;
randomRight = _randomRight;
stammWeight = _stammWeight;
ageMax = _ageMax;
blattDistance = _blattDistance;
leaves = new Leaf[0];
}

// method you call to make the branch grow
void grow() {
if (age++ >= ageMax) return; // increase i and test if > max leaves
organic = organic + int(random(randomLeft,randomRight)); //tendencies to the left or right
vzw=vzw*-1; //change of sign to turn the leaf from left to right with every step
leaves = (Leaf[])append(leaves, new Leaf(xx+organic, yy-age*blattDistance, vzw));
}

// method you call to draw the branch onto the screen
void display() {

// draw the branch itself
if (age > 0) {
line(xx, yy, leaves[0].x, leaves[0].y);
for (int i = 1; i < leaves.length; i++) {
stroke(0); strokeWeight(ageMax-i);
line(leaves[i].x, leaves[i].y, leaves[i-1].x, leaves[i-1].y);
}
// draw each leaf
for (int i = 0; i < leaves.length; i++) {
leaves[i].display();
}
}

}

}

class Leaf {

int x, y; // position (Ast)
int vzw; // orientation (left/right);
int blattX, blattY;

// constructor
Leaf(int _x, int _y, int _vzw) {
x = _x;
y = _y;
vzw = _vzw;
blattX = int(x + vzw*random(30,50));
blattY = int(y - random(20));
}

void display() {
stroke(#39302C); strokeWeight(1);
line(x, y, blattX, blattY);
noStroke(); fill(0,255,0);
ellipse(blattX, blattY, 20, 20);
}

}


Edit: fixed a little mistake.
Re: stuck at this point // growing problem
Reply #7 - Jul 18th, 2008, 9:26am
 
Now that you've got classes, it makes it a lot easier to create new branches, for example.

In the Tree constructor (line 27), you can easily add a second branch (with different parameters). Try this to make that second branch appear :

Code:
branches = new Branch[2];
branches[0] = new Branch(400, 1300, -15, 50, 8, 30, 30);
branches[1] = new Branch(400, 1300, -40, 30, 8, 30, 30);


Of course, you'll have to make some modifications depending on how/when/where you want your different branches to appear/grow.
Re: stuck at this point // growing problem
Reply #8 - Jul 18th, 2008, 4:49pm
 
Just a note to tell I made something similar in idea, giving quite different results...
See Ferns - Static view for an example of possible renderings.
It is a bit unfinished, I will rework on it someday.

All this to say I used (from the start of the design... Wink) classes similar to antiplastik's suggestion, because it is a natural design once you are used to OOP. Natural in both senses of word here... Smiley

I used a slightly different breakdown, working by connected segments: a plant is made of trunk segments, each one having two branches. Branches are made of segments holding each two leaves. Looks like:

abstract class Segment
class Trunk extends Segment
{
 private Trunk m_nextTrunkSegment; // The trunk part attached on this one (smaller)
 private Branch m_leftBranch; // One branch attached to base of this trunk segment
 private Branch m_rightBranch; // The other branch
}
class Branch extends Segment
{
 private Branch m_nextBranchSegment; // The branch part attached on this one (smaller)
 private boolean m_bIsLeftBranch;
 private Leaf m_leftLeaf; // One leaf attached to top of this branch segment
 private Leaf m_rightLeaf; // The other leaf
}
class Leaf extends Segment
{
 private boolean m_bIsLeftLeaf;
}
class Plant
{
 private Trunk m_stump;
}

Segment has a genotype info which describes how the plant is curved, relative proportions, color changes, etc.
It makes more regular plants, which is my goal. I plan to make the ferns to grow from very curved small state to full grown less curved state.

Your design looks very nice too!
Re: stuck at this point // growing problem
Reply #9 - Jul 18th, 2008, 6:01pm
 
@PhilHo: hey, its nice to see what your "farn" project turns out to looks like. im not sure if it helps me at this point as it is more about the creation if the leaf and not the plant itself, isnt it?

@antiplastik: Thanks again for your work, i worked through your code last night and i finally got what you did... but there are still some questions unanswered... good thing i found "the little mistake" you made and corrected it myself before i saw you edited your post and corrected it too, so at least i seem to understand the main idea behind OOP Smiley

anyway, as you said there now has to be a programm that combines the different branches to a tree until now all i do is creating different branches growing from the ground...

but before i start to do i would like to ask some question about your code to understand better what you did.

why do you use 2 methods, grow and display? is there any reason why i shouldnt do it in one? if they grow i want them to be displayed and vice versa.

another thing i havent seen before is :
if (age++ >= ageMax) return;  
i read about "return" on the Processing reference Page but i couldnt figure out what you do at this step.

The same here :
leaves = (Leaf[])append....

did you creat and array for the leafs but instead of defining the number of leafs at the begining you add another leaf to the array until it reaches maxAge?

I guess i will take a closer look  at the code tomorrow or sunday again and try to understand it better and maybe post some more questions then Smiley

Have a great weekend, both of you!
Re: stuck at this point // growing problem
Reply #10 - Jul 21st, 2008, 5:55pm
 
Quote:
why do you use 2 methods, grow and display? is there any reason why i shouldnt do it in one? if they grow i want them to be displayed and vice versa.


In the main loop, I would rather proceed in 2 steps : 1) make things move/grow/collide and 2) display them. Separating treatments and display is useful and a lot easier to handle when your project gets bigger.

Quote:
another thing i havent seen before is :
if (age++ >= ageMax) return;  
i read about "return" on the Processing reference Page but i couldnt figure out what you do at this step.


the keyword return is here used to escape the method. age increases, and if it gets bigger than ageMax, the following code won't execute.

Quote:
The same here :
leaves = (Leaf[])append....

did you creat and array for the leafs but instead of defining the number of leafs at the begining you add another leaf to the array until it reaches maxAge?


Instead of defining the number of leaves at the beginning, I increase the array's size at each new leaf, using the append() method. But since the append() method returns an array of Objects, I need to cast it into an array of Leaf. I know it's a bit tricky. Using an ArrayList instead of an array would have been easier, but I didn't want to confuse you with arraylists. Setting a fixed length for the array would have been easier too, but then you would need to store the number of leaves, so when you iterate you don't get an out-of-bound exception. So I just used that append() method for the purpose of this example.
Page Index Toggle Pages: 1