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 › run multiple copies of a class!!
Page Index Toggle Pages: 1
run multiple copies of a class!! (Read 1120 times)
run multiple copies of a class!!
Jul 26th, 2009, 5:27pm
 
hey! i asked some questions a few days ago and i was recomended to use arrays and for loops to do what i wanted! the thing is i don't know how and now Im trying to undestand!

what i need to know is, if i have a class how can i run several objects without having todo  things like Bolas b1 = new Bolas(); Bolas b2 = new Bolas();

because  i want to run like 50 or something!

here's my code Code:
Bolas b1 = new Bolas ();
PImage img;

void setup(){
// size(2480,3508);
size(800,600);
smooth();
//img = loadImage("LOVE.jpg"); // later i'm coloring the ellipses according to their xy position within the image

}

void draw(){
b1.desenhar();
}
Re: run multiple copies of a class!!
Reply #1 - Jul 26th, 2009, 10:51pm
 
Hope that helps you to understand how to create an array of objects and call them etc.

Code:
Bolas[] Bolases = new Bolas[30];

void setup(){
size(800,600);
smooth();
for (int i = 0; i < Bolases.length; i++) {
float x= random(width);
float y= random(height);
Bolases[i] = new Bolas(x,y);
}

}

void draw(){

for (int i = 0; i < Bolases.length; i++) {
Bolases[i].move();
Bolases[i].drawing();
}
}


class Bolas {
float x,y;
float speed;

Bolas(float _x, float _y) {
x = _x;
y = _y;
speed = random(3);
}

void move(){
x+=random(-speed,speed);
y+=random(-speed,speed);
}

void drawing(){
ellipse(x,y,5,5);

}

}


Re: run multiple copies of a class!!
Reply #2 - Jul 27th, 2009, 1:22pm
 
thanks a lot! i did understand it! Grin

i have other question, not related to this one but i don't want to open other topic! and since is in the same program!

i have this class and i want it to pick up the color from the respective xy position relative to the picture i have loaded in the setup, but im using floats because im dealing with noise(); if it was random(); i'd use int x = (int) random(); but with noise it doesn't work! can someone answer?

Code:
  class Bolas{

float xplus = 0.01;
float yplus = 0.01;

float xx = random(width);
float yy = random(height);


void desenhar(){
//noStroke();

// float x = noise(xx)*width;
//float y = noise(yy)*height;
int x = (int)noise(xx)*width;
int y = (int)noise(yy)*height;
xx += xplus;
yy += yplus;
color col = img.get(x,y);
fill(col);

ellipse(x, y, 30, 30);

}
}
Re: run multiple copies of a class!!
Reply #3 - Jul 27th, 2009, 1:40pm
 
It should work, but realize that noise() will always return a value lower than zero, which means when you truncate the float value to convert to int, you're cutting out all the decimal places which store all the values that noise() returns. In other words,
Code:
int x = (int)noise(some_value); 


will always return zero no matter what.
Re: run multiple copies of a class!!
Reply #4 - Jul 27th, 2009, 1:53pm
 
yep he is right. this way it should work :
int(noise(xx)*width)  etc... just put the int arround the everything
Re: run multiple copies of a class!!
Reply #5 - Jul 27th, 2009, 2:00pm
 
solved it! thanks!
i made a int x2 = (int)x;

it worked
Re: run multiple copies of a class!!
Reply #6 - Jul 27th, 2009, 2:07pm
 
dont understand how that should work but well if it does... anyway you should just move the paranthesis
Re: run multiple copies of a class!!
Reply #7 - Jul 27th, 2009, 3:48pm
 
Cedric, (int) foo is just a cast of a float to an int, with the side effect of dropping the decimal part. int() is just a convenience function (actually it is handled at pre-processing level, you won't find a int() function in the code, since that's a keyword).
Re: run multiple copies of a class!!
Reply #8 - Jul 27th, 2009, 3:59pm
 
sorry, i dont understand.
Are you correcting me? was i wrong, or my solution? if so, please explain it again, so i understand the difference, i always thought there is no between (int)x and int(x);
Re: run multiple copies of a class!!
Reply #9 - Jul 28th, 2009, 1:20am
 
You wrote: "dont understand how that should work", I supposed it was about the cast against the int(). Sorry if I misunderstood the sentence.

Basically, yes, int(x) vs. (int) x are the same. At least for float type.
int() is more like a facility offered to new comers, offering a familiar syntax as an alternative to the cast.
Although there is more to int(): it also offers conversions not available to cast, like int("42") which internally calls Integer.parseInt(). So int() is more orthogonal than the cast.
Re: run multiple copies of a class!!
Reply #10 - Jul 28th, 2009, 1:30am
 
i was replying to his

Quote:
solved it! thanks!
i made a int x2 = (int)x;

solution and wondering what he did there...

But thanks for explaining!
Re: run multiple copies of a class!!
Reply #11 - Jul 28th, 2009, 5:43am
 
Cedric wrote on Jul 28th, 2009, 1:30am:
i was replying to his
solution and wondering what he did there...
But thanks for explaining!



Let me explain, i wanted to use img.get(x,y) but my x and y when converted to a whole number were 0 because i was rounding the number with an int before it was multiplied so it would always make the result be 0! that's what  NoChinDeluxe explained! so i figured that if i made my x and y a noise float and then make 2 other(x2 and y2) variables equal to x and y respectively and making them int x2 = (int)x; so the number was rounded i could use the get() method. And it worked!

ill post my code and class here

if you want you can see the results in http://www.flickr.com/photos/brt-design/

here's the code Code:
import processing.pdf.*;

Bolas[] Bolases = new Bolas[100];
PImage img;
void setup(){
size(2480,3508,PDF,"Love5.pdf");
//size(800,600);
smooth();
img = loadImage("LOVE.jpg");
for (int i = 0; i < Bolases.length; i++) {
float x= random(width);
float y= random(height);
Bolases[i] = new Bolas();

}
background(255);

}

void draw(){


for (int i = 0; i < Bolases.length; i++) {
Bolases[i].desenhar();

}

if(frameCount == 300){
endRecord();
exit();
}
}




and here's the class Code:
  class Bolas{

float xplus = 0.02;
float yplus = 0.02;
float xinc = random(100,200);
float yinc = random(100,200);

float xx = random(width)*xinc;
float yy = random(height)*yinc;




void desenhar(){
stroke(255);
//noStroke();

float x3 = random(xx);
//float y3 = noise(yy);
float x = noise(xx)*width;
float y = noise(yy)*height;
xx += xplus;
yy += yplus;
int x2 = (int)x;
int y2 = (int)y;

color col = img.get(x2,y2);
fill(col);
float prop = random(90,100);
//float prop = random(9,20);
ellipse(x, y, prop, prop);
println(x3);


}
}


thanks again for all the help! i wouldn't do these simple thing without it! Smiley
Re: run multiple copies of a class!!
Reply #12 - Jul 28th, 2009, 5:46am
 
ok, i still would use my solution thought Smiley
Re: run multiple copies of a class!!
Reply #13 - Jul 28th, 2009, 5:50am
 
yeah but i only have seen it after i solved it on my own! but its really cool that you have given me an alternative because now i can have both to use some next time Wink thanks
Page Index Toggle Pages: 1