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 › grid with individual grid members ...
Page Index Toggle Pages: 1
grid with individual grid members ... (Read 561 times)
grid with individual grid members ...
Sep 14th, 2006, 12:43am
 
Hi all,

I have been working on making a grid of 25 circles, where I would like to change each array member (i.e. circle [1]) without changing the rest of the array members. Right now I can only figurfe out how to repeat the first line 5 times.
How can I put 25 individual circles in a grid of 5 *5?

Here's my code so far:

import processing.opengl.*;

int ellipseAmt = 25;
float[] ellipXpos = new float[ellipseAmt];
float[] ellipYpos = new float[ellipseAmt];
float[] ellipXz = new float[ellipseAmt];
float[] ellipYz = new float[ellipseAmt];



void setup(){
 size(300,300,OPENGL);
 noStroke();
 fill(255,0,0);
 
 for(int i=0; i<ellipseAmt; i=i+1) {
   ellipXz[i] =20; ellipYz[i] =20;
 }  
 
}
void draw(){
 background(0);
 translate(50, 50);
 pushMatrix();
 ellipYpos[1]=mouseX;
 ellipXz[1] = mouseY*0.2; ellipYz[1] = mouseY*0.2;
 for(int i=0; i<ellipseAmt/5; i=i+1) {
   //ellipse(ellipXpos[i], ellipYpos[i]+i*30, ellipXz[i], ellipYz[i]);
   for(int j=0; j<ellipseAmt/5; j=j+1) {
     ellipse(ellipXpos[j]+j*30, ellipYpos[j]+i*30, ellipXz[j], ellipYz[j]);
   }
 }  
 popMatrix();

 
 /*
 for(int i=0; i<ellipseAmt/5; i=i+1) {
   //ellipse(ellipXpos[i], ellipYpos[i]+i*30, ellipXz[i], ellipYz[i]);
   for(int j=0; j<ellipseAmt/5; j=j+1) {
     ellipse(ellipXpos[j]+j*30, ellipYpos[j]+i*30, ellipXz[j], ellipYz[j]);
   }
 }
 */
 
}

Re: grid with individual grid members ...
Reply #1 - Sep 14th, 2006, 1:38am
 
There's a couple of operators you seem to be unaware of.

++ and --

They add or subtract only one from a variable but they're also much faster than saying x += 1 or x = x + 1 (and quicker to type).

You might find managing your ellipses easier if you made objects out of them. I've put an example below of how you might do it, showing how objects do the handy trick of looking out for themselves.
Code:
MyEllipse [] myEllipse = new MyEllipse[25];
void setup(){
size(300, 300);
for(int i = 0; i < myEllipse.length; i++){
myEllipse[i] = new MyEllipse((i % 5) * 50, (i / 5) * 50, 40, 40);
}
}
void draw(){
background(0);
smooth();
for(int i = 0; i < myEllipse.length; i++){
myEllipse[i].draw();
}
}
class MyEllipse{
float x, y, wide, high;
MyEllipse(float x, float y, float wide, float high){
this.x = x;
this.y = y;
this.wide = wide;
this.high = high;
}
void draw(){
if(over()){
fill(255);
}
else{
fill(255, 255, 0);
}
ellipse(x, y, wide, high);
}
boolean over(){
if(dist(mouseX, mouseY, x, y) < wide / 2){
return true;
}
return false;
}
}

If you're not au fait with classes and objects, Shiffman does a better job of explaining it than I can:

http://itp.nyu.edu/icm/shiffman/week3/index.html
Page Index Toggle Pages: 1