Map delimiters: can't make vertex() to draw them

edited November 2016 in Questions about Code

Hi all, I am trying to make a map of Buenos Aires neighbourhoods. I've created a csv file with some coordinates to delimit some of the neighbourhoods. I want the program to draw and fill the limits, using beginShape/vertex/endShape. The coordinates are loaded into a table, and then, two loops read each row/column and get the values for each neighbourhood (one row per neighbourhood, with 4 pairs of values, for a 4 sided polygon each). When all the vertex points are drawn for that polygon, the inner loop ends, and the program runs the endShape(CLOSE) to close the limits, and starts the next row (that's why I put the endShape(CLOSE) outside the inner loop).

I can't figure out why it is not working. I've put a println() to check if the values retrieved in each iteration are ok. The values are ok, but vertex does not draw the polygon. I've also tried leaving the image (svg) out, because perhaps, the loop draws it behind the map, but again, no success. Maybe the endShape should not be outside the second loop, meaning that all code between beginShape and endShape must remain together? Any idea? Thanks so much in advance, Marcelo

This is the program (Processing 2):

PShape mapaBA;
Table table;

void setup(){
  size(800,800);
  noLoop();
  table = loadTable("barrios.csv"); // coords for some neighborhoods limits
  mapaBA = loadShape("CABA.svg"); // map of Buenos Aires city
}

void draw(){
    shape(mapaBA,0,0,width,height);
    for (int i=0;i<table.getRowCount();i++){ 
      for (int e =0;e<table.getColumnCount()-1;e=e+2){ 
          beginShape();
          int x = table.getInt(i,e);
          int y = table.getInt(i,e+1);
          println(i, e, x,y); // just to test if values are ok
          vertex(x,y); // should draw a polygon that delimites a neighbourhood, based on coords
        }
          endShape(CLOSE);
    }

}

The csv file contains the following values:

642,320,554,326,564,365,643,355
647,355,563,364,561,401,648,393
559,403,612,400,617,438,567,468
649,394,612,398,618,440,659,431

Answers

  • Answer ✓

    use ctrl-t in the pde editor to indent your code. it will help you spot errors like this

    void draw(){
      shape(mapaBA, 0, 0, width, height);
      for (int i = 0 ; i < table.getRowCount() ; i++){ 
        for (int e = 0 ; e < table.getColumnCount() - 1 ; e = e + 2){ 
          beginShape();
          int x = table.getInt(i, e);
          int y = table.getInt(i, e + 1);
          println(i, e, x, y); // just to test if values are ok
          vertex(x, y); // should draw a polygon that delimites a neighbourhood, based on coords
        }
        endShape(CLOSE);
      }
    }
    

    (beginShape is in the wrong place. or endShape is.)

  • edited November 2016

    Yes, I mentioned this in the post as one of the possible sources. I wasn't aware that this could be actually an error. But your answer make me rewrite that part and problem solved! It was much easier than I thought. I've just moved beginShape() out of the inner loop, and now both begin and end are in the same place. Thanks!

    new code:

    PShape mapaBA;
    Table table;
    
    void setup() {
      size(800, 800);
      noLoop();
      table = loadTable("barrios.csv");
      mapaBA = loadShape("CABA.svg");
    }
    
    void draw() {
      shape(mapaBA, 0, 0, width, height);
      for (int i=0; i<table.getRowCount (); i++) {
        beginShape();
        for (int e =0; e<table.getColumnCount ()-1; e=e+2) {
          int x = table.getInt(i, e);
          int y = table.getInt(i, e+1);
          println(i, e, x, y);
          vertex(x, y);
        }
        endShape(CLOSE);
      }
    }
    
Sign In or Register to comment.