col[i][j] = col [i][j-2] ?

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!

Tagged:

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++; } } }

  • Answer ✓

    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++; } }

  • edited October 2013

    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?

  • edited October 2013

    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

  • Answer ✓

    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.

    int size = 6; // grid resolution
    int mag = 3; // magnification factor
    
    int invaderSize  = size * mag; // actual display size
    int padding = int(invaderSize/2);
    int tileSize = invaderSize + padding;
    int mx, my;
    ArrayList invaders;
    
    void setup() {
      // Calculate the number of tiles
      mx = int(900/tileSize);
      my = int(600/tileSize);
      size(mx*tileSize+padding, my*tileSize + padding);
      background(255);
      stroke(0);
    
      invaders = new ArrayList();
      int px, py;
      for (int x = 0; x < mx; x++) {
        px = padding/2 + x * tileSize;
        for (int y = 0; y < my; y++) {
          py = padding/2 + y * tileSize;
          Invader invader = new Invader(px, py, size, mag);
          invaders.add(invader);
        }
      }
      for (int i=0; i<invaders.size();i++) {
        Invader inv = (Invader) invaders.get(i);
        inv.display();
      }
    }
    
    void draw() {
    }
    
    void keyPressed() {
      save(random(1234)+".gif");
    }
    void mouseClicked() {
      setup();
    }
    
    class Invader {
      float x, y;
      int mag;
      int size;
      int[][] col;
    
      Invader(float _x, float _y, int _size, int _mag) {
        x = _x;
        y = _y;
        size = _size;
        mag = _mag;
        col = new int[size][size];
        int halfSize = round(size/2.0);
        println(size + "   " +  halfSize);
        for (int j=0; j < size; j++) {
          for (int i=0; i<= halfSize; i++) {
            int c = (random(1) > .5)? 255:0; //black or white?
            col[j][i] = c;
            col[j][size - 1- i] = c;
          }
        }
      }
    
      void display() {
        for (int j=0; j<size; j++) {
          for (int i=0; i<size; i++) {
            fill(col[j][i]);
            noStroke();
            rect(x+i*mag, y+j*mag, mag, mag);
          }
        }
      }
    }
    
  • Wow, thanks a lot, quark! :)

    And sorry for the confusion ...

Sign In or Register to comment.