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 › classes and arrays
Page Index Toggle Pages: 1
classes and arrays (Read 510 times)
classes and arrays
Aug 25th, 2009, 6:16pm
 
i am trying to store the coordinates of each krosnja object so that i can use them, but what i get is coordinates for all 10 objects i initiated. i don't know how can i separate them and compare the coordinates later. what i am trying to do is to compare the x,y from k[0] object and k[1...etc].
like : dist(x(k[0]), y(k[0]), x1(k[1]), y1(k[1])

if someone can help that would be great.

thanks!

int currentk =0;
int numb = 10;
int xmos, ymos;


krosnja[] k = new krosnja[numb];
//float[]x = new float[numb];
//float[]y = new float[numb];


void setup(){

 size(1000,600);
 background(255);
 smooth();
 frameRate(25);
 stroke(0);
 strokeWeight(0.7);
 for (int i =0; i < numb; i++){
   k[i] = new krosnja();
   k[i].kros(random(width), random(height), random(3));

 }
}

void draw(){
 //if (keyCode == UP){
 for(int i = 0; i<numb; i++){
   k[i].crtaj();
 }
}  

void mousePressed(){

 xmos = mouseX;
 ymos = mouseY;
 k[currentk].kros(xmos, ymos, random(3));
 currentk++;

 if (currentk >= numb){
   currentk = 0;
 }
}
class krosnja{

 float xa, ya, step, stepx, stepy;
 float[]x = new float[numb];
 float[]y = new float[numb];
 float[][]xy = {
   x,y    };
 float di;
 int x1, y1, x2, y2;


 void kros(float xxx, float yyy, float stp){
   xa = xxx;
   ya = yyy;
   step = stp;
 }


 void crtaj(){
   stepx = step;
   stepy = step;    
   point(xa,ya);

   xa=xa+stepx*random(-1,1);
   ya=ya+stepy*random(-1,1);    


   if (xa > width-50){
     stepx = -step;
     xa = width-50;
   }
   else
     if (xa < 0){
       xa = 10;
       stepx = step;
     }
     else
       if (ya > height -50){
         ya = height - 50;
         stepy = -step;
       }  
       else    
         if (ya < 0){
         ya = 10;
         stepy = step;
       }
 }
}







Re: classes and arrays
Reply #1 - Aug 25th, 2009, 6:31pm
 
to be honest im a bit confused about your x and y naming and what they are used for...



anyway, for example to get the distance between object 1 and 2 just write :

println(dist(k[0].xa, k[0].ya, k[1].xa, k[1].ya));
Re: classes and arrays
Reply #2 - Aug 25th, 2009, 7:04pm
 
thanks a lot for the help!

i should read the manuals more thoroughly.

t
Page Index Toggle Pages: 1