Make a grid from a linear index

edited November 2016 in Questions about Code

Hi everyone, this is my first time writing in the forum, all the other times I had a question, someone already did it and some other one answered!

This time I have the problem that with a csv file which goes to 120 rows, I pass all that data in the constructor, but I haven`t find a way to put the 120 objects in a 60 x 20 grid.

Objet[] o;  
void setup() {  
 size(1000, 800);  
 table = loadTable("data.csv", "header");  
 o = new Objeto[table.getRowCount()];  
 for (int i = 0; i < table.getRowCount(); i++) {  
 int inde = i;  
  TableRow r = table.getRow(i);  
  String date = r.getString("date");  
  int num = r.getInt("num");  
  int time = r.getInt("time");  
  int score = r.getInt("score");  

  o[i] = new Objet(index, date, num, time, score);  
} 

Answers

  • @nicotron

    I believe this recent post would provide you a partial answer to your question. When you talk about a grid, do you refer like a 2D graph? Check the post. i would recommend the grafica library:

    https://forum.processing.org/two/discussion/19180/plot-graph#latest

    Also please format your code. Edit your post (click on the gear in your previous post), select your code and hit ctrl+o.

    Kf

  • Thanks your the link. I will read it.
    I'm trying to make this:
    From this:
    Screen Shot 2016-11-24 at 12.23.54 AM

    To this:
    Screen Shot 2016-11-24 at 12.24.53 AM

    In which I only have the index of the list of objects

  • A simple example is here. I am using the map function to map the range of the input values into a second range. You can have a look at the documentation for any of the commands I used here or for any other clarification:. The reference at:

    https://processing.org/reference/

    The data I am using is a pairwise coordinates that have the following format

    a single letter separated from a single number by a comma

    Kf

    String[] data = {"a,3", "c,5", "b,7", "b,2", "i,8", "z,6", "q,9", "q,5"};
    
    int radius=15;
    
    void setup() {
      size(600, 600);
      rectMode(CENTER);
      ellipseMode(CENTER);
      noLoop();
      noStroke();  //So it does not draw borders
    }
    
    void draw() {
      background(92);
      fill(0);
      rect(width*0.5, height*0.5, width*0.8, height*0.8); //Using only 80% by 80% of the drawing area
    
      for (int i=0; i<data.length; i++) {
        String[] tokens = data[i].split(",");
        printArray(tokens);
        float point1=map(tokens[0].charAt(0),'a','z',width*0.1,width*0.9);  //80% of the width
        float point2=map(tokens[1].charAt(0),'0','9',height*0.1,height*0.9);  //80% of the height
        fill(color(random(55,255)));
        ellipse(point1,point2,radius,radius);
      }
    }
    
  • I'm not sure that's what he's asking.

    My guess is

    for (int i = 0 ; i < 120 ; i++) {
       ellipse(i % 12 * spacing, i / 12 * spacing, 5, 5);
    }
    

    Ie going from a single linear index to 2d

  • (Above is interested and written on phone so probably full of syntax errors)

Sign In or Register to comment.