Appending x,y values to an indexed array

Hi, I am creating a matrix with two for loops, 'i' and 'j'. I would like to append the values to an empty list 'ptList' in this fashion: [0] i, j [1] i, j [2] i, j etc...

any ideas would be appreciated as I think this should be simple since I can do it quite easily in python.

//colour lists
color [] dragonKingdom = {#1143D9, #0D8BD9, #6BE5F2, #F2E205, #F9C3B3};
color[] palette = dragonKingdom;

int d = 50; //matrix subdivision values
int[] ptList = new int [50];

void setup() {
  size(800, 400);
  background(palette[4]);
  ellipseMode(CENTER);

attractorPt();

  //create list of x,y pts matrix values
  for (int i = 0; i < width; i += d) {
    for (int j = 0; j < height; j += d) {
      //int [] ptList = append(ptList, [i, j]);
      strokeWeight(5);
      stroke(palette[int(random(1, 5))]);
      point(i,j);

    }
  }
}


//attractor point method
void attractorPt() {
  stroke(palette[3]);
  strokeWeight(20);
  point(random(width),random(height));
}

Answers

  • edited June 2016
    // forum.Processing.org/two/discussion/16923/
    // appending-x-y-values-to-an-indexed-array#Item_1
    
    // GoToLoop (2016-Jun-01)
    
    static final color[] PALETTE = {
      #1143D9, #0D8BD9, #6BE5F2, #F2E205, #F9C3B3
    };
    
    static final int W = 800, H = 400, SMOOTH = 4;
    static final int DIAM = 25, GAP = 50; 
    static final int COLS = W/GAP + 1, ROWS = H/GAP + 1;
    static final int GRID = COLS*ROWS;
    
    final ShortTuple[] dots = new ShortTuple[GRID];
    
    void settings() {
      size(W, H);
      smooth(SMOOTH);
      noLoop();
    }
    
    void setup() {
      if (width != W)  settings();
      strokeWeight(DIAM);
    }
    
    void draw() {
      background(0);
    
      for (int x = 0; x < COLS; ++x)  for (int y = 0; y < ROWS; ++y) {
        final int i = x*GAP, j = y*GAP;
        dots[x*ROWS + y] = new ShortTuple(i, j);
    
        stroke(PALETTE[(int) random(PALETTE.length)]);
        point(i, j);
      }
    
      printArray(dots);
      println();
      println(GRID, COLS, ROWS, DIAM, GAP, W, H);
    }
    
    static class ShortTuple {
      final short x, y;
    
      ShortTuple (int i, int j) {
        x = (short) i;
        y = (short) j;
      }
    
      String toString() {
        return "[ " + x + "," + y + " ]";
      }
    }
    
  • edited June 2016

    ... since I can do it quite easily in Python.

    Still very novice in Python. Nonetheless, here's my attempt: >-)
    No need for an extra ShortTuple class though, given Python already got tuples builtin. :P

    # forum.Processing.org/two/discussion/16923/
    # appending-x-y-values-to-an-indexed-array#Item_2
    
    # GoToLoop (2016-Jun-01)
    
    PALETTE = 0xff1143D9, 0xff0D8BD9, 0xff6BE5F2, 0xffF2E205, 0xffF9C3B3
    COLORS  = len(PALETTE)
    
    W, H, SMOOTH = 800, 400, 4
    DIAM, GAP = 25, 50
    COLS, ROWS = W/GAP + 1, H/GAP + 1
    GRID = COLS*ROWS
    
    dots = GRID*[None]
    
    def settings(): size(W, H), smooth(SMOOTH), noLoop()
    def setup(): width != W and settings(), strokeWeight(DIAM)
    
    def draw(colsCount=tuple(range(COLS)), rowsCount=tuple(range(ROWS))):
        background(0)
    
        for x in colsCount:
            i, r = x*GAP, x*ROWS
    
            for y in rowsCount:
                j = y*GAP
                dots[r + y] = i, j
    
                stroke(PALETTE[int(random(COLORS))])
                point(i, j)
    
        print dots, ENTER, ENTER, GRID, COLS, ROWS, DIAM, GAP, W, H
    
  • @gotoloop: I appreciate the elegance of your code. :)>- Also, the way you are solving some problems of randomly choosing palette indices and how to set up a grid are quite helpful. Nevertheless, the method you have employed to create the x,y tuples is rather complex for me at this point in my learning. Is there a simpler way?

    Here is how I can achieve it in python (just a snippet b/c code is for Rhino:

        #loop to create list of short values
        for i in range(iMax):
            for j in range(jMax):
    
                x = i
                y = j
                z = k
                ptList.append([x,y,z])
    

    As you can see, it is so simple, I wonder if I use IntList or another functionality in processing/java. I am mostly confused with this code.

        final int i = x*GAP, j = y*GAP;
        dots[x*ROWS + y] = new ShortTuple(i, j);
    

    Why do you multiply by x*Rows and then add why? Also, this array seems to be either a class or embedded within a class. It is elegant, but a bit advanced for me :(

    final ShortTuple[] dots = new ShortTuple[GRID];

    Thank you again. My end goal is to measure the list of x,y values against the attractor point.

  • Now that I have done more research, it seems that a 2 dimensional array will solve the issue. This example is essentially what I want, but I am not sure what to set the 'myArray[i][j] equal to as I simply wish for it to contain the values for the action of creating ellipses.

    size(200,200);
    int cols = width;
    int rows = height;
    
    // Declare 2D array
    int[][] myArray = new int[cols][rows];
    
    // Initialize 2D array values
    for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
        myArray[i][j] = ???
      }
    }
    
  • edited June 2016

    ... the method you have employed to create the (x,y) tuples is rather complex for me...

    I was trying to mirror Java Mode's sketch in Python Mode as much as possible. :-\"
    In Java, array's length is determined at the time of its instantiation and can't be changed later.

    Processing's append() function doesn't actually change the passed array's length btW.
    Instead it creates another 1 w/ length + 1. Then internally arrayCopy() everything from passed array to the new 1, appends passed value parameter to it and finally returns the newly array.

    As you can see, append() shouldn't be abused for performance reasons.
    At most for sporadically append() a new element for when mousePressed() and such.

    Otherwise, if its length changes too much within a short range of time...
    Or we simply can't easily determine the length for it, we're better off going w/ some ArrayList.
    For int, float & String, containers IntList, FloatList & StringList are even better options.

    Why do you multiply by x*ROWS and then add + y...

    B/c it's a double loop. And iterators x & y behave as a 2D index pair.
    Given that dots[] is a 1D array, that 2D index gotta be converted to 1D index 1st. :-B

    If dots were an ArrayList instead, we'd use its add() method: dots.add(new ShortTuple(i, j));
    Similar case in Python if we replace dots = GRID*[None] w/ dots = []: dots.append((i, j))

    As you see now, we don't need to worry about getting the right index at the moment of initializing the container when its initial length is 0. ;;)

    Anyways for both Java & Python, assigning values by index is faster than changing container's length in order to append() some value at its tail. $-)

    ... this array seems to be either a class or embedded within a class.

    Yup, array dot[] can only store references of the "homemade" nested class ShortTuple. :P

    Obviously we can change it to Processing's own PVector class:
    https://Processing.org/reference/PVector.html

    Or if you prefer, Java's Point class:
    http://docs.Oracle.com/javase/8/docs/api/java/awt/Point.html

    Or anything else which can store at least 2 values to form a coordinate pair: ;))

    // forum.Processing.org/two/discussion/16923/
    // appending-x-y-values-to-an-indexed-array#Item_5
    
    // GoToLoop (2016-Jun-02)
    
    static final color[] PALETTE = {
      #1143D9, #0D8BD9, #6BE5F2, #F2E205, #F9C3B3
    };
    
    static final int W = 800, H = 400, SMOOTH = 4, FPS = 10;
    static final int DIAM = 25, GAP = 50; 
    static final int COLS = W/GAP + 1, ROWS = H/GAP + 1;
    static final int GRID = COLS*ROWS;
    
    import java.util.List;
    import java.awt.Point;
    
    //final Point[] dots = new Point[GRID];
    final List<Point> dots = new ArrayList<Point>(GRID);
    
    void settings() {
      size(W, H);
      smooth(SMOOTH);
      noLoop();
    }
    
    void setup() {
      if (width != W)  settings();
      frameRate(FPS);
      strokeWeight(DIAM);
    
      for (int x = 0; x < COLS; ++x)  for (int y = 0; y < ROWS; ++y) {
        final int i = x*GAP, j = y*GAP;
    
        //dots[x*ROWS + y] = new Point(i, j);
        dots.add(new Point(i, j));
      }
    
      printArray(dots);
      println();
      println(GRID, COLS, ROWS, DIAM, GAP, W, H);
    }
    
    void draw() {
      background(0);
    
      for (final Point d : dots) {
        stroke(PALETTE[(int) random(PALETTE.length)]);
        point(d.x, d.y);
      }
    }
    
    void keyPressed() {
      redraw();
    }
    
    void mousePressed() {
      keyPressed();
    }
    
  • edited June 2016 Answer ✓
    # forum.Processing.org/two/discussion/16923/
    # appending-x-y-values-to-an-indexed-array#Item_6
    
    # GoToLoop (2016-Jun-02)
    
    PALETTE = 0xff1143D9, 0xff0D8BD9, 0xff6BE5F2, 0xffF2E205, 0xffF9C3B3
    COLORS  = len(PALETTE)
    
    W, H, SMOOTH, FPS = 800, 400, 4, 10
    DIAM, GAP = 25, 50
    COLS, ROWS = W/GAP + 1, H/GAP + 1
    GRID = COLS*ROWS
    
    #dots = GRID*[None]
    dots = []
    
    def settings(): size(W, H), smooth(SMOOTH), noLoop()
    
    def setup():
        width != W and settings(), frameRate(FPS), strokeWeight(DIAM)
        colsCount, rowsCount = range(COLS), range(ROWS)
    
        for x in colsCount:
            i, r = x*GAP, x*ROWS
    
            for y in rowsCount:
                j = y*GAP
    
                #dots[r + y] = i, j
                dots.append((i, j))
    
        print dots, ENTER, ENTER, GRID, COLS, ROWS, DIAM, GAP, W, H
    
    
    def draw():
        background(0)
    
        for d in dots:
            stroke(PALETTE[int(random(COLORS))])
            point(d[0], d[1])
    
    
    def keyPressed(): redraw()
    mousePressed = keyPressed
    
  • Thank you once again for your immensely enlightened response. It may take me a few days or more to digest this information; however, I am realizing that I might have over complicated the program for myself. Nevertheless, I think your 'ShortTuple' will come in handy.

    I know that I can take segments of your ideology and apply it towards a more concise version of the program that uses either Array lists or PVectors. I will do some more research and thanks again for such a comprehensive response!

Sign In or Register to comment.