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
array help (Read 678 times)
array help
Nov 13th, 2009, 5:46am
 
hi,

i'm trying to work out the code for reproducing the cover of casey and ben's processing book. i've managed to do the setup of the sketch, and i've almost got the network points to connect to each other, based on evaluation of distance (if dist less than 100, then draw line between ellipses).

but i can't seem to get rid of that 'ArrayIndexOutOfBounds' error. any ideas? thanks in advance.

(i know the structure of the code could be much better, but this is one of my first 'extensive' codes.)

Code:
// NETWORK DIAGRAM VERSION 2

// GLOBAL VARIABLES --------------------------------------------

int canvas = 900;              // size of canvas (square)
int amount = 400;              // amount of points on the screen
int minPtSize = 10;            // min size of points/ellipse's
int maxPtSize = 100;           // max size of points/ellipse's

int[] ptsX = new int[amount];  // declare + create new array
int[] ptsY = new int[amount];  // declare + create new array

// SETUP + DRAW RANDOM ELLIPSES --------------------------------

void setup() {
 size(canvas, canvas);
 smooth();
 background(255);
 noStroke();
 strokeWeight(1.2);
 fill(0, 170, 250, 40);
 noLoop();

 for (int i = 0; i < amount; i++) {        
   int x = rdmValue(0, canvas);          
   int y = rdmValue(0, canvas);            
   ptsX[i] = x;                        
   ptsY[i] = y;                            
 }  
 
 for (int i = 0; i < amount; i++) {        
   float r = random(minPtSize, maxPtSize);  
   ellipse(ptsX[i], ptsY[i], r, r);        
   stroke(0, 100);
   fill(255);
   ellipse(ptsX[i], ptsY[i], 4, 4);        
   noStroke();
   fill(0, 170, 250, 40);
 }    
}

// DRAW --------------------------------------------------------

void draw() {
 stroke(0, 100);
 strokeWeight(0.5);  
 int count = 0;

 for (int i = 0; i < amount; i++) {
   for (int j = 0; j < amount; j++) {
     if (dist(ptsX[count], ptsY[count], ptsX[i], ptsY[i]) < 100) {
       line(ptsX[count], ptsY[count], ptsX[i], ptsY[i]);
     }
   count = count + 1;      
   }
 }
}

// FUNCTION RANDOM VALUE ---------------------------------------

int rdmValue(float top, float bottom) {  
 int f = int(random(top, bottom));  
 return f;                                      
}


Re: array help
Reply #1 - Nov 13th, 2009, 6:12am
 
Simple:

"count = count + 1" is inside the nested 'j' loop.  It needs to be placed after that loop closes:

Code:
for (int i = 0; i < amount; i++) { 
for (int j = 0; j < amount; j++) {
if (dist(ptsX[count], ptsY[count], ptsX[i], ptsY[i]) < 100) {
line(ptsX[count], ptsY[count], ptsX[i], ptsY[i]);
}
// not inside j, otherwise count > amount
}
count = count + 1;
}
Re: array help
Reply #2 - Nov 13th, 2009, 6:20am
 
thanks for your reply.

i see your point. but it still doesn't give me my lines.

the thing that confuses me is that when i put the (count = count + 1) inside j, it gives me exactly what i want for the first loop, before it crashes; lines to the the other ellipses that are within distance 100. any ideas on that? or maybe my whole code-structure is wrong ..
Re: array help
Reply #3 - Nov 13th, 2009, 6:38am
 
OK - my previous reply just addressed what was causing the bug.  The question is what do you think 'count' does?

Looks to me like you don't need it:

Code:
// NETWORK DIAGRAM VERSION 2

// GLOBAL VARIABLES --------------------------------------------

int canvas = 900;              // size of canvas (square)
int amount = 400;              // amount of points on the screen
int minPtSize = 10;            // min size of points/ellipse's
int maxPtSize = 100;           // max size of points/ellipse's

int[] ptsX = new int[amount];  // declare + create new array
int[] ptsY = new int[amount];  // declare + create new array

// SETUP + DRAW RANDOM ELLIPSES --------------------------------

void setup() {
 size(canvas, canvas);
 smooth();
 background(255);
 noStroke();
 strokeWeight(1.2);
 fill(0, 170, 250, 40);
 noLoop();

 for (int i = 0; i < amount; i++) {        
   int x = rdmValue(0, canvas);          
   int y = rdmValue(0, canvas);            
   ptsX[i] = x;                        
   ptsY[i] = y;                            
 }  
 
 for (int i = 0; i < amount; i++) {        
   float r = random(minPtSize, maxPtSize);  
   ellipse(ptsX[i], ptsY[i], r, r);        
   stroke(0, 100);
   fill(255);
   ellipse(ptsX[i], ptsY[i], 4, 4);        
   noStroke();
   fill(0, 170, 250, 40);
 }    
}

// DRAW --------------------------------------------------------

void draw() {
 stroke(0, 100);
 strokeWeight(0.5);  

 for (int i = 0; i < amount; i++) {
   for (int j = 0; j < amount; j++) {
     //  Often the point of these nested loops is to compare two elements in an array
     if (dist(ptsX[i], ptsY[i], ptsX[j], ptsY[j]) < 100) {
       line(ptsX[i], ptsY[i], ptsX[j], ptsY[j]);
     }

   }
 }
}

// FUNCTION RANDOM VALUE ---------------------------------------

int rdmValue(float top, float bottom) {  
 int f = int(random(top, bottom));  
 return f;                                      
}


Note that whilst the above works it's not optimised: IIRC that nested loop means that you land up comparing points more than once...  Look up collision detection optimisation.  As well as resolving that issue you'll find that there are better alternatives to using dist()...
Re: array help
Reply #4 - Nov 13th, 2009, 6:45am
 
you're a lifesaver. thanks!

i'll look up 'collision detection optimisation'.
Re: array help
Reply #5 - Nov 13th, 2009, 6:51am
 
I see blindfish beat to this one but here is my solution which avoids duplicate point comparisons.

Code:
void draw() {
 stroke(0, 20);
 strokeWeight(0.5);  

 for (int i = 0; i < amount - 1; i++) {
   for (int j = i; j < amount; j++) {
     if (dist(ptsX[j], ptsY[j], ptsX[i], ptsY[i]) < 100) {
       line(ptsX[j], ptsY[j], ptsX[i], ptsY[i]);
     }
   }
 }
}


I also took the liberty of changing the alpha value in stroke to increase transaprency of the lines

Re: array help
Reply #6 - Nov 13th, 2009, 7:00am
 
thank you very much. i was wondering about that as well. the duplicate comparisons. great stuff.

it actually looks much better when there isn't 200 lines on top of each other.
Page Index Toggle Pages: 1