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.
Page Index Toggle Pages: 1
Eyes (Read 355 times)
Eyes
Jul 13th, 2008, 7:26am
 
I downloaded some code from ejohn.org page and was trying to make some changes. Trying to understand arrays I added some to make the code more flexible, but something is wrong. Note that I commented the original code, which actually works but its not preciselly what I want.

/*Eye e1, e2, e3, e4, e5;*/
int eyeMax = 100;
Eye[] e = new Eye[eyeMax];

void setup()
{
 size(1024, 768);
 smooth();
 noStroke();
 
 for (int i=1; i<=e.length; i++){
   int xRand = round(random(width));
   int yRand = round(random(height));
   int sRand = round(random(height/7));
   e[i] = new Eye(xRand, yRand, sRand);
 }

/*  e1 = new Eye( 50,  16,  int(random(500)));
 e2 = new Eye( 64,  85,  40);  
 e3 = new Eye( 90, 200, 120);
 e4 = new Eye(150,  44,  40);
 e5 = new Eye(175, 120,  80);*/
}

void draw()
{
 background(102);
 
 for (int i=1; i<=eyeMax; i++){
  e[i].update(mouseX, mouseY);
  e[i].display();
 }
   
/*  e1.update(mouseX, mouseY);
 e2.update(mouseX, mouseY);
 e3.update(mouseX, mouseY);
 e4.update(mouseX, mouseY);
 e5.update(mouseX, mouseY);

 e1.display();
 e2.display();
 e3.display();
 e4.display();
 e5.display();*/
}

class Eye
{
 int ex, ey;
 int size;
 float angle = 0.0;
 
 Eye(int x, int y, int s) {
   ex = x;
   ey = y;
   size = s;
}

 void update(int mx, int my) {
   angle = atan2(my-ey, mx-ex);
 }
 
 void display() {
   pushMatrix();
   translate(ex, ey);
   fill(255);
   ellipse(0, 0, size, size);
   rotate(angle);
   fill(153);
   ellipse(size/4, 0, size/2, size/2);
   popMatrix();
 }
}

Re: Eyes
Reply #1 - Jul 13th, 2008, 9:39am
 
Eye[] eye = new Eye[100];
has just 100 eyes, from eye[0] to eye[99],
not "eye[1] to eye[100]"

use
for (int i=0; i<e.length; i++)
other than
for (int i=1; i<=e.length; i++)
Re: Eyes
Reply #2 - Jul 13th, 2008, 9:43am
 
There is an error in the non commented code: loop must go from 0 to e.length - 1: for (i = 0; i < e.length; i++)
Common idiom in Java (and C and lot of other programming languages).
Re: Eyes
Reply #3 - Jul 14th, 2008, 10:34pm
 
Thank you very much PhiLho. Classiclll: I've tried realized that but my error was that little "=". I must say that I'm not used to java and some of the error codes thrown by processing.
But with the time I'll be capable of not posting in this forums for every silly error I found in the code.
Sorry for my bad english also. XD
Page Index Toggle Pages: 1