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 › connecting constructors in classes
Page Index Toggle Pages: 1
connecting constructors in classes (Read 841 times)
connecting constructors in classes
Nov 19th, 2009, 2:59am
 
hey all,

when i generate two surfaces sur1 and sur2, i can't access their vertices (xk,yk,zk) and draw lines between them. this is probably easy, but i am completely stuck.

thanks for the help.
best
v



class Surface{
 float  xk, yk, zk, x, y, z, xx, yx, zx;
 int ra, brivi;
 // ArrayList xk, yk, zk;
 float angle = 0;
 ArrayList xlist;
 Surface(){
 }

 Surface(int brojivica, int radius, float xa, float ya, float za){        
   brivi = brojivica;
   ra = radius;
   x = xa;
   y = ya;
   z = za;
 }

 void forma(){
   xlist = new ArrayList();
   stroke(2);
   beginShape();
   for(int j=0;j<=brivi;j++){
     xk = cos(radians(angle))*ra;
     yk = sin(radians(angle))*ra;
     vertex(xk+x,yk+y,zk+z);
     angle+=360.0/brivi;
     xlist.add(xk);
   }
   endShape(CLOSE);  
 }
}
Re: connecting constructors in classes
Reply #1 - Nov 19th, 2009, 6:07am
 
Once you've created your objects you can easily reference their properties (as long as they're not explicitly made private) like this:

Code:
println(sur1.xk);
println(sur1.yk);
println(sur1.zk);


...You can do that from within your main body of code; though not obviously not from within the Surface class itself.
Re: connecting constructors in classes
Reply #2 - Nov 19th, 2009, 8:00am
 
thanks, but this is the problem, because once i call this from the draw function, i get all the instances of the class displayed.

for example if i create 3 surfaces/objects, when i run this:
println(sur1.xk);
i would get something like this in the terminal:

30.00
23.00
10.00

i could add this to a list, but the problem is that the number of xk can vary depending on the object and that means it is not easy to track it and reference it. it is probably possible, tough...

i just rewrote everything by creating a clearer setup with multi-dimensional list that include objects...


tx anyways,
t

Re: connecting constructors in classes
Reply #3 - Nov 19th, 2009, 9:13am
 
All those values are coming from the same Surface object: 'sur1'.

In your forma() method you run through a for loop assigning a new value to xk in each iteration.  Therefore at the end of draw() xk equals the last value defined in the for loop and that value gets printed.  Because in the next draw loop the starting value of xk is different that means the end value, after running through the for loop, will be different too; so a different value gets printed.  Each loop you get a different value and you'll get a number of different values equal to 'brivi+1', since that's the number of iterations your loop goes through because your for loop continues till j<=brivi.

Well - it's something along those lines anyway.  Here's the proof - a single surface object prints several different values for xk:

Code:
Surface surf1;
Surface surf2;
Surface surf3;


void setup() {
 size(400,300,P3D);
 surf1 = new Surface(3,50,200,150,100);
}

void draw() {
 background(0);
 surf1.forma();
}


class Surface{
float  xk, yk, zk, x, y, z, xx, yx, zx;
int ra, brivi;
// ArrayList xk, yk, zk;
float angle = 0;
ArrayList xlist;
Surface(){
}

Surface(int brojivica, int radius, float xa, float ya, float za){        
  brivi = brojivica;
  ra = radius;
  x = xa;
  y = ya;
  z = za;
}

void forma(){
  xlist = new ArrayList();
  stroke(2);
  beginShape();
  for(int j=0;j<brivi;j++){
    xk = cos(radians(angle))*ra;
    yk = sin(radians(angle))*ra;
    vertex(xk+x,yk+y,zk+z);
    angle+=360.0/brivi;
    xlist.add(xk);
    println(xk);
  }
  endShape(CLOSE);  
  println("-----------");
}
}
Page Index Toggle Pages: 1