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 › Right way or should it be done different Classes
Page Index Toggle Pages: 1
Right way or should it be done different? Classes? (Read 471 times)
Right way or should it be done different? Classes?
Jun 3rd, 2008, 4:07am
 
As I'm new to programming and just started to understand the basics of it I'm not sure if this is the right way done - or if you are supposed to do it different. Im asking myself if a "class" would be the right thing here but thats maybe because I dont unterstand 100% the idea of a "class" itself. If there are any tutorials dealing with the use of classes that would be great. Anyway. Here is my Problem.

I started with the Idea of greating different leafs which differ in size and bending. This is working so far.
I used the mouseX,Y for testing only, these variables should be fix at the and and set different for every leaf.
But im not sure if this is the proper way to do this, and how would it be possible to add another leaf just at a different possition. What is working with one leaf here doenst work if I add another one. Just adding another  " blatt(xx,yy,anzahl,drehung,abstand,neigung);" doesnt work. So maybe you know that to do.

Thank you so far!

Quote:



void setup() {
 size(400,400);
 smooth();
 frameRate(25);
 noStroke();
 background(0);
}

//Eindrehung der Blätter ca. 2 - 30
float drehung= radians( 15);

// Abstand 1 bis max 2
float abstand= 2.0;

//Neigung +-0.005 bis +-0.2
float neigung= -0.0;

//Koordinaten Baum
int xx=200;
int yy=300;

//anzahl Äste
int anzahl=12;

void draw() {

 background(255);
 stroke(0);
 line(0,285,400,285);
 noStroke();

 //set to mouse for testing only
 int anzahl= mouseY/28;


 blatt(xx,yy,anzahl,drehung,abstand,neigung);


}

void blatt(int xx, int yy, int anzahl,float drehung, float abstand, float neigung){
 translate(xx,yy);    
 for(int i=anzahl+4; i>=4; i--){

   //set to mouse for testing only
   rotate(+0.0015*(mouseX-200));
   translate(0,-i*abstand);

   strang(i,drehung,1);
   strang(i,-drehung,-1);
 }

}

void strang(float i,float drehung,int vzw) {
 i-=0.6;

 if(i>1){
   pushMatrix();
   rotate(drehung);

   fill(0,0,0);
   ellipse(0,0,i,i/5);


   translate(0,-i/2);
   strang(i,drehung,vzw);
   popMatrix();

 }
}


Re: Right way or should it be done different? Clas
Reply #1 - Jun 3rd, 2008, 10:57am
 
I think the reason that just calling it again won't work is because you have some translate commands that aren't wrapped in push/popMatrix calls, so the movement is accumulating, and you're losing things.

As for using classes, yes it would make it simpler in a larger program, but for this use, avoiding them is adequate.
Re: Right way or should it be done different? Clas
Reply #2 - Jun 3rd, 2008, 12:01pm
 
Thank you, i will try to fix the translate Problem.
As this is going to be larger and should consist arround 100-1000 leafes and more at the end, maybe classes would be a good Idea. Are there any good explanations arround? the Processing.org Reference just doesnt do it this time for me. Smiley
Re: Right way or should it be done different? Clas
Reply #3 - Jun 3rd, 2008, 4:29pm
 
In some cases, classes can be overkill, but yet they remain a nice way to structure things and might help keeping a reasoning clean...
I used classes (a generic one, "abstract" and some concrete ones derived from the first) in a fern drawing sketch I made.
I planned to release it formally, and somehow to use this code to introduce some concepts of usage of classes (and interfaces), but I haven't written anything yet, nor finalized my code.
If you are curious, you can take a look at the current code at http://www.autohotkey.net/~PhiLho/Transferts/Ferns.7z (link can break without notice!).

At the simplest state, you can think of a class as a way to make a data structure, to hold together related variables. It corresponds to C's struct construct.
Example:
Code:
class GeomVector
{
float x, y; // Coordinates of base of vector
float ang; // Angle of vector with base (X) axis
float len; // Length of vector
}

You can use it this way:
Code:
GeomVector v = new GeomVector(); 


After the new keyword, you have a call to the constructor of the vector. Here, it is the default constructor provided by the compiler, doing nothing.
Your vector has no data, you have to fill its fields:
Code:
v.x = 10.0; v.y = 5.0; v.ang = PI/4; v.len = 20.0; 


You can then pass the vector as function parameter, return it, store it in an array, etc.

Now, usually you add methods to a class, which are functions related to the data stored in the class. Or just related to the purpose of the class.
The first one is usually a constructor, providing a convenient way of filling the data:
Code:
class GeomVector
{
float x, y; // Coordinates of base of vector
float ang; // Angle of vector with base (X) axis
float len; // Length of vector

GeomVector(float _x, float _y, float _ang, float _len)
{
x = _x; y = _y; ang = _ang; len = _len;
}
}

// Now created with:
GeomVector v = new GeomVector(10.0, 5.0, PI/4, 20.0);

Note that the constructor has the same name as the class and no return value. I prefer to distinguish the parameters from the class fields, but they can have the same name. In this case, the init looks like: this.x = x; etc.
You can add convenience functions to the class. A usual idiom, sometime overkill in Processing, is to declare the class fields as private (no direct access) and create accessors, ie. getters and setters, methods allowing to set and read the values in the class.
Other possible methods for this class could be a translation method (pass a vector as parameter, move (recompute) x and y along this vector) and a rotation one (pass an angle, change vector's angle accordingly), etc.
Re: Right way or should it be done different? Clas
Reply #4 - Jun 3rd, 2008, 4:34pm
 
Wow, thank you. That was a great help. I guess ive solved this case.

Thanks again.
Page Index Toggle Pages: 1