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 & HelpSyntax Questions › Creating instances of a
Page Index Toggle Pages: 1
Creating instances of a (Read 397 times)
Creating instances of a
Jun 8th, 2008, 4:24pm
 
I want to create an under-water scene with some algaes. I tried to build one with the bezierVertex function (which looked ok), but I wanted to use a class, that duplicates that algae several times at a different position, with a different size and a different color maybe.
I don't know if I get this right, but to me it seems like I have to send all the parameters because of the x and y coordinates of the anchor points. Therefore a class would be useless, wouldn't it?

I'm a total beginner with Processing. There might be a different solution which I just can't think of.

Thanks for your help,
Franni
Re: Creating instances of a
Reply #1 - Jun 8th, 2008, 4:56pm
 
You can encapsulate all your data into a class, and then draw from within the class.

e.g.
Code:
class algae
{
float xpos,ypos;
float[] xCoords,yCoords;

algae()
{
xpos=random(width);
ypos=random(height);
xCoords=new float[13];
yCoords=new float[13];
for(int i=0;i<12;i++)
{
xCoords[i]=random(30,100)*cos(TWO_PI/12.0*i);
yCoords[i]=random(30,100)*sin(TWO_PI/12.0*i);
}
xCoords[12]=xCoords[0];
yCoords[12]=yCoords[0];
}

void draw()
{
beginShape();
vertex(xpos+xCoords[0],ypos+yCoords[0]);
for(int i=0;i<=9;i+=3)
{
bezierVertex(xpos+xCoords[i+1],ypos+yCoords[i+1],xpos+xCoords[i+2],ypos+yCoords[i+2],xpos+xCoords[i+3],ypos+yCoords[i+3]);
}
endShape();
}
}

algae fred; //can easily be an array of algae

void setup()
{
size(300,300);
fred=new algae();
smooth();
strokeWeight(2);
}

void draw()
{
background(0);
fill(0,255,0,128);
stroke(0,255,0,230);
fred.draw();
}


That's just a complete guess at how you're using bezierVertex and the like BTW, but shows that you can just use bezierVertex in your class, you don't have to send the parameters into or out of your class, and each instance of an algae object, has a unique set of data.
Re: Creating instances of a
Reply #2 - Jun 8th, 2008, 5:10pm
 
Thanks for your quick reply.

My code looks like that so far:

class algenKlasse {

algenKlasse(which parameters??? )

}

void draw(){
beginShape();
vertex(393, 218);
bezierVertex(400, 171, 398, 159, 385, 102);
bezierVertex(385, 102, 380, 129, 391, 184);
endShape();
}
}

Can you please tell me on this code which parameters I have to change in order to maintain the form of the algae? Or is the use of a bezierVertex completely wrong?
Re: Creating instances of a
Reply #3 - Jun 8th, 2008, 5:43pm
 
Example:
Code:
class AlgenKlasse {
int posX, posY;
color col;

AlgenKlasse()
{
posX = 100; posY = 100;
col = #00FF00;
}

AlgenKlasse(int x, int y, color c)
{
posX = x; posY = y;
col = c;
}

void draw(){
fill(col);
pushMatrix();
translate(posX, posY);
beginShape();
vertex(393, 218);
bezierVertex(400, 171, 398, 159, 385, 102);
bezierVertex(385, 102, 380, 129, 391, 184);
endShape();
popMatrix();
}
}

AlgenKlasse ak1, ak2, ak3;

void setup()
{
smooth();
noLoop();// No animation
size(800, 800);

ak1 = new AlgenKlasse();
ak2 = new AlgenKlasse(-100, -50, #55FF88);
ak3 = new AlgenKlasse(0, 300, #11AA55);
}

void draw()
{
ak1.draw();
ak2.draw();
ak3.draw();
}

Of course, you better set the coordinates (eg. the bottom of the algae) to 0,0, so posX and posY are exact placement.
Re: Creating instances of a
Reply #4 - Jun 8th, 2008, 5:46pm
 
I also am a beginner, though I'm trying to construct examples and lessons for use in a class in the Fall.
I came up with the following example. The display screen is blank. When you click the mouse, a flower appears. The color and the number of petals are randomly assigned.


void setup()
 {
   size (600,400);
   background(255);
 }
 
 
void draw () {
}

void mousePressed() {
  int nc = int(random(5));
  fill(random(255),random(255),random(255));
  int np = int(random(3,8));
  Flower f = new Flower(mouseX,mouseY,40,50,np);
 
}

class Flower {
 
 Flower (float sx, float sy, float wdx, float wdy, int npetals) {
   
   translate(sx,sy);
   for (int p=0; p<=npetals; p++) {
     rotate (2*PI/npetals);
     beginShape();
      vertex(0,0);
      bezierVertex(wdx,-wdy, 2*wdx, 3*wdy, 3,3);
     endShape();
   }
   translate(-sx,-sy);
  }
}

Here is a variant: press down, drag and release mouse to set the shapes of the petals.

int firstmousex;
int firstmousey;

void setup()
 {
   size (600,400);
   background(255);
 }
 
 
void draw () {
}
void mousePressed() {
 firstmousex = mouseX;
 firstmousey = mouseY;  
}
void mouseReleased() {
  int nc = int(random(5));
   //more red and blue, less green
  fill(random(200,255),random(100),random(100,200));
  int np = int(random(3,8));
  int wdx = mouseX-firstmousex;
  int wdy = mouseY-firstmousey;
  Flower f = new Flower(firstmousex,firstmousey,wdx,wdy,np);
 
}

class Flower {
 
 Flower (float sx, float sy, float wdx, float wdy, int npetals) {
   
   translate(sx,sy);
   for (int p=0; p<=npetals; p++) {
     rotate (2*PI/npetals);
     beginShape();
      vertex(0,0);
      bezierVertex(wdx,-wdy, 2*wdx, 3*wdy, 3,3);
     endShape();
   }
   translate(-sx,-sy);
  }
}



Regarding your example, I didn't know you could draw within a class.

(I had to do this a couple of times to get rid of the smily face. Sorry.)
Re: Creating instances of a
Reply #5 - Jun 8th, 2008, 6:32pm
 
@ Philho
Perfect! This is exactly how I want it. Can you help me with one more thing?

What if I want to place each instance of the "ak" randomly in the area of x between 0 and 200 and y between 0 and 800?
I guess I have to use a loop, but where and how?

@ Jeanine
I also appreciate your help. Philho already solved my problem. Thanks anyway!
Re: Creating instances of a
Reply #6 - Jun 9th, 2008, 4:26pm
 
Funnily, I made a similar sketch recently, except I was translating ferns instead of algae...
But I did it stupidly, recreating the ferns on each iteration (recomputing the points) instead of using translate()... So I will rewrite it in a smarter way.
Although I plan to still recompute the ferns as they must evolve with time. I am not sure here if it is better to recreate immutable ferns on each draw call (Java have to garbage collect them after) or to update the coordinates instead. I suppose I must try and see the framerate in each case...

Anyway, to your problem: as I wrote, I have reset the coordinates of your vertices to have a good neutral starting point.
I made a loop to create several algae, then in the draw loop I just translate them...
That's just a quick hack, you can add up to this code, extending the class to parametrize the vertices, making a different translation for each algae (then probably letting the class doing that), etc.

Code:
class AlgenKlasse
{
int posX, posY;
color sCol, fCol;

AlgenKlasse() // Default constructor, with hopefully sensible default values
{
posX = width / 2; posY = height / 2;
sCol = #005500;
fCol = #00FF00;
}

AlgenKlasse(int x, int y, color sc, color fc)
{
posX = x; posY = y;
sCol = sc; fCol = fc;
}

void draw()
{
stroke(sCol);
fill(fCol);

pushMatrix();
translate(posX, posY);

beginShape();
// I suppose this is the origin point, so I set these coordinates to 0
// and substract them from the other vertices
vertex(0, 0);
bezierVertex(7, -47, 5, -59, -8, -116);
bezierVertex(-8, -116, -13, -89, -2, -34);
endShape();

popMatrix();
}
}

final int ALGAE_NB = 20;
AlgenKlasse[] ak;
int curPos = 0;

void setup()
{
smooth();
size(800, 800);

ak = new AlgenKlasse[ALGAE_NB];
ak[0] = new AlgenKlasse(); // Just to show default constructor use... ;)
for (int i = 1; i < ALGAE_NB; i++)
{
ak[i] = new AlgenKlasse(int(random(10, 200)), int(random(70, height)),
color(0, random(64, 200), random(0, 100)), color(0, random(128, 255), random(0, 128)));
}
}

void draw()
{
background(11, 55, 77);
pushMatrix();
translate(curPos, 0);

for (int i = 0; i < ALGAE_NB; i++)
{
ak[i].draw();
}

popMatrix();
curPos++;
}
Page Index Toggle Pages: 1