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 › How to simplify the following explosion
Page Index Toggle Pages: 1
How to simplify the following explosion? (Read 3012 times)
How to simplify the following explosion?
May 20th, 2010, 5:42am
 
Hi all, this has been puzzling me for over 4 days and
it has been taking up all of my spare time. Does anyone
knows how to simplify the following code?

if (exploding == 1) {

location [0] [ random_between_0_and_3 [0] ]  -= 2;
location [0] [ random_between_0_and_3 [1] ] += 2;
location [0] [ random_between_0_and_3 [2] ]  -= 2;

location [1] [ random_between_0_and_3 [3] ]  -= 2;
location [1] [ random_between_0_and_3 [4] ] += 2;
location [1] [ random_between_0_and_3 [5] ]  -= 2;


.....(etcetera).....

location [8] [ random_between_0_and_3 [24] ]  -= 2;
location [8] [ random_between_0_and_3 [25] ] += 2;
location [8] [ random_between_0_and_3 [26] ]  -= 2;

}


This is not my full program code, only the part i need to simplify
because i have a similar code for rotation when exploding, and for
location and rotation when imploding. This makes my code very long.
To explain a bit about what this small section of my program code
will do, the following comments are appropriate:

What this code visually will help to do is exploding
my composition that exists of 9 different CUBES. Location [a] [b] will
correspond to the movement of CUBE. [a] will determine the number
of one of the 9 CUBES. [b] will determine wether the movement
of the CUBE occurs parrallel with the x-, the y- or the z- axis.

Everytime an explosion needs to occur, elsewhere in my
code there gets put a random number between 0 and 3
in every element of the array random_between_0_and_3.
So random_between_0_and_3 [c] will determine the [b]
of location [a] [b]. This is done to randomize the explosion
every time an explosion is needed.

Every CUBE gets three random_between_0_and_3 [c]'s
as [b] of location [a] [b]. This is done to make the randomized
explosion not only occur in straight ways.

The -= 2; or += 2; will make the CUBES actually move.
-= 2; will create a negative movement, += 2; will create
a positive movement. I need both positive and negative
movements because otherwise the CUBES will not seem
to explode in all directions. In the small section of my program
code above there are more -= 2;'s than += 2;'s. This should
also get balanced somehow to get a more realistic explosion.


If I need to post the full code of my program, please do ask
me to and I will do so. I am really looking for a way to simplify
the explosion-code and would like to thank all of you in advance.

Wink Greets, Vincent Verheyen.


Re: How to simplify the following explosion?
Reply #1 - May 20th, 2010, 6:18am
 
Let see if I can find a pattern in your numerous lines...
Code:
if (exploding == 1) {
int idx = 0;
int incr = 2;
for (int i = 0; i < location.length; i++) {
for (int j = 0; j < 3; j++) {
location[i][ random_between_0_and_3[idx++] ] += incr;
incr = -incr;
}
}
}

It is untested, and I invert the increment on some trios (ie. I do += -= += on half of the trios instead of -= += -= on all trios).
If you don't want that, it can be easily changed.
Re: How to simplify the following explosion?
Reply #2 - May 20th, 2010, 8:32am
 
PhiLho you really are a life saver!
Your code worked like a charm, and i'm loving it!
I really mean to thank you! It's perfect!

Thanks PhiLho!
http://www.youtube.com/watch?v=8SiCz8wt2O4

Greets, Vincent Verheyen.
Re: How to simplify the following explosion?
Reply #3 - May 20th, 2010, 8:40am
 
just for fun, my 2 cents Wink
Code:
for (int i = 0; i <= 26; i++) {
 int a = 2 * (1 - 2*abs(i%3 - 1));
 location[i/3][random_between_0_and_3[i]] += a);
}
Re: How to simplify the following explosion?
Reply #4 - May 20th, 2010, 9:48am
 
Cool!
But slightly more cryptic... Tongue
Re: How to simplify the following explosion?
Reply #5 - May 21st, 2010, 1:34am
 
philho seems to like extra variables, antiplastik is using voodoo 8)

my take would be this:
Code:

for (int i = 0 ; i <= 8 ; i++) {
 location [i] [ random_between_0_and_3 [i * 3] ]  -= 2;
 location [i] [ random_between_0_and_3 [i * 3 + 1] ]  += 2;
 location [i] [ random_between_0_and_3 [i * 3 + 2] ]  -= 2;
}
Re: How to simplify the following explosion?
Reply #6 - May 21st, 2010, 1:36am
 
um, ignore the {code} tags - work wiki uses curly brackets rather than square ones. i also appear to have lost ability to edit posts... and should've previewed...

edit: i can now edit again.
Re: How to simplify the following explosion?
Reply #7 - May 21st, 2010, 4:15am
 
@koogy: don't worry, i still can edit yours  Tongue

damned, you were right, your way is much easier to read!! but less challenging, though
Re: How to simplify the following explosion?
Reply #8 - May 21st, 2010, 8:17am
 
@antiplastik, or even more cryptically?:
Code:

int a = (i%3&1)*4-2;

Re: How to simplify the following explosion?
Reply #9 - May 21st, 2010, 10:55am
 
oh I love that  Cheesy
Re: How to simplify the following explosion?
Reply #10 - May 22nd, 2010, 1:55am
 
freaks Smiley
Re: How to simplify the following explosion?
Reply #11 - May 25th, 2010, 10:18am
 
Thanks all of you.
I have now put up a link to exhibit the full program.

http://processing.org/discourse/yabb2/num_1274808222.html#0.

Greets,
(look into Einstein's writings about science and belief)
Vincent Verheyen.
Re: How to simplify the following explosion?
Reply #12 - May 25th, 2010, 1:33pm
 
i love how antiplastik loves that  Smiley
Page Index Toggle Pages: 1