We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I would like to fill a certain position in an array with the values of an earlier position of this array? I tried it with a simple = but that doesn't seem to work ...
How do I need to write this?
Thanks!
Answers
"that doesn't work" = no help to us. what doesn't work?
col[i][j] = col[i][j - 2] is fine, on a syntax level. but what happens when j = 0 or j = 1? in those cases j-2 is negative and you can't have a negative index into an array.
Ah, ok, sorry ... what I meant was is that it seems as if the values don't get transferred ...
What I'm actually trying to do is to mirror values ... (5x5 grid, the left two columns should be mirrored on the right) ... there's probably a better way to do this?? ;)
Here's the actual (noob) code:
for (int j=0;j<sz;j+=step) { m = 1; for (int i=0;i<sz;i+=step) { if (i < sz/2) { c = (random(1) > .5)? 255:0; //black or white? col[j][i]= c; } else { col[j][i] = col[j][i-2*m]; m++; } } }
i would have an outer loop (x) that ran over half a row (plus the central column)
and an inner loop (y) that ran through every line
choose a random colour
set [x, y] to the colour
set [size - x][y] to the colour - reflection
end inner loop
end outer loop
Ok, I did it like this now and it works (but I need to still get rid of the magic number 16 an replace it through a variable):
for (int j=0;j<sz;j+=step) { m = 1; for (int i=0;i<sz/2;i+=step) { c = (random(1) > .5)? 255:0; //black or white? col[j][i]= c; col[j][i+16/m] = c; m++; } }
You say you have a 5x5 array and you want vertical reflection of the columns do you mean
0 1 2 1 0 or 0 1 2 0 1
Also what is the value of
step
and is it a variable or constant?Is the purpose of
m
and 16 simply to get the reflection? If so there are much better ways of doing it.I forgot to ask is the array always 5x5 and is sz=5?
quark:
here's my final code: http://pastebin.com/umz39Yus ... I'm sure it can be enhanced!? there are a couple of things that look inefficient to me but I don't know how to do it better right now ...
and here's the final image: http://p5art.tumblr.com/post/64950925460/space-invaders-revisited-inspired-by-jared
It took me a while to work out what is happening but I got there in the end.
Each invader is 15 x 15 pixels when displayed but it only has a grid resolution of 5 x 5 - each grid square would be either black or white and would require 3 x 3 pixels.
It meant that in your code each invader had a 15 x 15 array when in fact it only needed a 5 x 5 array. This made the reflection code messy.
So in the code below the first line defines the size of the resolution grid i.e. 5, the second variable is the magnification factor 3 giving a display size of 15 same as before.
The rest of the code has been adjusted accordingly. I have added two extra attributes to the Invader class so that each invader can have a different grid resolution and magnification if wanted later.
The code will work for any grid resolution (size) and magnification (mag) so try different values in the first two lines.
Wow, thanks a lot, quark! :)
And sorry for the confusion ...