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 › looping through 2D array without repeating
Page Index Toggle Pages: 1
looping through 2D array without repeating (Read 1149 times)
looping through 2D array without repeating
Dec 21st, 2009, 8:06am
 
hey guys. when playing arround with a bouncing ball collition programm i learned that you can easy some clever loops so you dont need to check AB and BA... i did that with

Code:
  for(int i=0; i<  ball.length; i++){ 
   for(int j=i+1; j<  ball.length; j++){
   
   }
 }


my question now is how would this look like if i have a 2d Array of balls. and to check for collition or set attractions.
In my case i created a 2d matrix using an 2d array.

Code:
balls[x][y] 



how do i loop through these without doing it twice.
I just cant make it work.
Re: looping through 2D array without repeating
Reply #1 - Dec 21st, 2009, 8:46am
 
Could you put some more code inside the inner for loop so it could be looked at? I don't see problem with your loops being cleaver and faster but since you only access half the array index then your code has to do something like array[i][j]=array[j][i]=results maybe for collision and array[i][j]=-(array[j][i]=results) for attraction ; or else. It depends on what you have in the inner loop so list your code.

Giles has written a charged particle program with similar code. Look under programs board for charged particles.
Re: looping through 2D array without repeating
Reply #2 - Dec 21st, 2009, 9:31am
 
ok, lets say i want to create an attraction between every particle.

i did it like this before :

Code:
 for(int i=0; i<  ball.length; i++){ 
   for(int j=i+1; j<  ball.length; j++){
physics.makeAttraction(ball[i],ball[j],-620, 50 );

   }
 }


but now i dont have a 1d array containing the balls[] but a 2d array

Code:
 physics.makeAttraction( mouse, ball[X][X],ball[X][X],-620, 50 );  



but i dont know what to put in the X's do i need more then 2 loops to loop through them? and how would i write it so it doesnt set the attraction twice because like mentioned AB and BA is actually the same. Dont need to do that twice. I hope its a bit clearer now.
Re: looping through 2D array without repeating
Reply #3 - Dec 21st, 2009, 12:27pm
 
Quote:
In my case i created a 2d matrix using an 2d array.

Code:
balls[x][y] 


I don't believe you exactly... How did you create that array?

Maybe something like this: (warning: untested code)

Code:
int numX = 30;
int numY = 20;
Ball balls1d[] = new Ball[numY * numX];
Ball balls2d[][] = new Ball[numY][numX];

int i = 0; // 1D index

for (int y = 0; y < numY; y++)
{
 for (int x = 0; x < numX; x++)
 {
   // Create ONE ball object with TWO references
   balls2d[y][x] = balls1d[i] = new Ball();
   i++; // the 1d index for the next ball
 }
}


Remember: the arrays only hold REFERENCES to the ball objects, and you can have as many references as you like to each object.

So, the above code (or some working version based on it) should allow you to access in two ways:

Code:
int x = 3; // column 4
int y = 5; // row 6
Ball theBall = balls2d[y][x];
Ball theSameBall = balls1d[y * numX + x]; // 5 * 30 + 3 = 153


So, you can use balls1d the same was as you used to use the original array.

The real question, though, is why are you creating a 2D array in the first place? I'm supposing each ball bounces around independently, and its location has little to do with its 2D array indices.

-spxl
Re: looping through 2D array without repeating
Reply #4 - Dec 21st, 2009, 4:53pm
 
This depends on the physics function. If the function adjusts properties (acceleration) of both balls within one call, then there is no need to do what I suggest and I definitely will not use 2D array for reference. There are only "length" many balls so only that many elements in a 1d array is needed. If I were given such a 2d array as you described, I might try balls[i][i] under one for loop but again, tell us more about how balls[][] were created.
Re: looping through 2D array without repeating
Reply #5 - Dec 21st, 2009, 5:13pm
 
ok, i am sorry maybe i have to explain better what i am trying to do.
i am using traerphysics to create a mesh (10x10 matrix) of particles.
thats the reason why I use this 2d array because its ease to adress a single particle as "i" and "j" are his coodinates.

So when i played arround with traer physisc earlier and had some free floating particles stored in a 1d array and wanted every particle to have a attraction on every other particle i used these 2 for loops mentioned above.

What i now want to do is exactly the same. i have this mesh where every particle is connected to other ones by a spring. Now i want to create a negative attraction from any particle to any other particle. But dont want to repeat myself.

Hope its a bit clearer. Sorry for my vague explaination at first.
Re: looping through 2D array without repeating
Reply #6 - Dec 23rd, 2009, 7:30am
 
Code:
  for(int i=0; i< balls1d.length; i++){ 
   for(int j = i+1; j< balls1d.length; j++){
// stuff    
   }
 }

Page Index Toggle Pages: 1