We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Adding lines to my sketch
Page Index Toggle Pages: 1
Adding lines to my sketch (Read 381 times)
Adding lines to my sketch
Feb 11th, 2009, 6:52pm
 
Hi, im sure this is very straightforward, but can someone tell me what im doing wrong? I am basically trying to map lines between points I have specified on an image using a CSV table. When I try and add lines between points i.e. line(19, 25, 70, 90); I get a load of errors. I am sure this is down to me inserting the command in the wrong place, or using incorrect syntax, however I have tried several variations with no luck. Code is below, below that I have put the code for the table.

Any help greatly appreciated!


PImage mapImage;
Table locationTable;
int rowCount;

void setup() {
 size(600, 563);
 mapImage = loadImage("europe_plastic.png");
 //Make a data table from a file that contains
 //the coordinates of each country.
 locationTable = new Table("locations.tsv");
 // The row count will be used a lot, so store it globally.
 rowCount = locationTable.getRowCount();
}
 void draw() {
   background(255);
   image(mapImage, 0, 0);
   
   //Drawing attributes for the ellipses.
   smooth();
   fill(192, 0, 0);
   noStroke();
   
   // Loop through the rows of the locations file and draw the points.
   for (int row = 0; row < rowCount; row++) {
     float x = locationTable.getFloat(row, 1);  // column 1
     float y = locationTable.getFloat(row, 2);  // column 2
     ellipse(x, y, 4, 4);
   }
 }
line(19, 25, 70, 90);
}

**TABLE**


class Table {
 String[][] data;
 int rowCount;
 
 
 Table() {
   data = new String[10][10];
 }

 
 Table(String filename) {
   String[] rows = loadStrings(filename);
   data = new String[rows.length][];
   
   for (int i = 0; i < rows.length; i++) {
     if (trim(rows[i]).length() == 0) {
       continue; // skip empty rows
     }
     if (rows[i].startsWith("#")) {
       continue;  // skip comment lines
     }
     
     // split the row on the tabs
     String[] pieces = split(rows[i], TAB);
     // copy to the table array
     data[rowCount] = pieces;
     rowCount++;
     
     // this could be done in one fell swoop via:
     //data[rowCount++] = split(rows[i], TAB);
   }
   // resize the 'data' array as necessary
   data = (String[][]) subset(data, 0, rowCount);
 }


 int getRowCount() {
   return rowCount;
 }
 
 
 // find a row by its name, returns -1 if no row found
 int getRowIndex(String name) {
   for (int i = 0; i < rowCount; i++) {
     if (data[i][0].equals(name)) {
       return i;
     }
   }
   println("No row named '" + name + "' was found");
   return -1;
 }
 
 
 String getRowName(int row) {
   return getString(row, 0);
 }


 String getString(int rowIndex, int column) {
   return data[rowIndex][column];
 }

 
 String getString(String rowName, int column) {
   return getString(getRowIndex(rowName), column);
 }

 
 int getInt(String rowName, int column) {
   return parseInt(getString(rowName, column));
 }

 
 int getInt(int rowIndex, int column) {
   return parseInt(getString(rowIndex, column));
 }

 
 float getFloat(String rowName, int column) {
   return parseFloat(getString(rowName, column));
 }

 
 float getFloat(int rowIndex, int column) {
   return parseFloat(getString(rowIndex, column));
 }
 
 
 void setRowName(int row, String what) {
   data[row][0] = what;
 }


 void setString(int rowIndex, int column, String what) {
   data[rowIndex][column] = what;
 }

 
 void setString(String rowName, int column, String what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = what;
 }

 
 void setInt(int rowIndex, int column, int what) {
   data[rowIndex][column] = str(what);
 }

 
 void setInt(String rowName, int column, int what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }

 
 void setFloat(int rowIndex, int column, float what) {
   data[rowIndex][column] = str(what);
 }


 void setFloat(String rowName, int column, float what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }
 
 
 // Write this table as a TSV file
 void write(PrintWriter writer) {
   for (int i = 0; i < rowCount; i++) {
     for (int j = 0; j < data[i].length; j++) {
       if (j != 0) {
         writer.print(TAB);
       }
       if (data[i][j] != null) {
         writer.print(data[i][j]);
       }
     }
     writer.println();
   }
   writer.flush();
 }
}
 
   



Re: Adding lines to my sketch
Reply #1 - Feb 12th, 2009, 7:29am
 
could you upload an archieve with the files needed ?
Re: Adding lines to my sketch
Reply #2 - Feb 12th, 2009, 11:43am
 
The } after the line() call, before the **TABLE** line, matches nothing.
You can see that in PDE by clicking after the brace: the matching one is highlighted.
So your line() call is outside of setup and draw, and the syntax is incorrect.
Re: Adding lines to my sketch
Reply #3 - Feb 12th, 2009, 11:58am
 
Here are the project files. So just to clarify, I am just trying to draw a line between any 2 points on the image,  but am unsure exactly where to insert this in the code.

https://www.yousendit.com/download/U0d6ZXQySytkMnVGa1E9PQ
Re: Adding lines to my sketch
Reply #4 - Feb 12th, 2009, 12:26pm
 
Quote:
to draw a line between any 2 points

Not sure what you mean. All points will have a lines going to all other points?

Otherwise, a simple way is to rewrite:
Code:
float px, py;
for (int row = 0; row < rowCount; row++) {
float x = locationTable.getFloat(row, 1); // column 1
float y = locationTable.getFloat(row, 2); // column 2
ellipse(x, y, 4, 4);
if (row > 0) {
line(px, py, x, y);
}
px = x; py = y;
}

In the first case, you have to do a double loop.
Re: Adding lines to my sketch
Reply #5 - Feb 12th, 2009, 2:36pm
 
like PhiLho says, if thats what you want, connect every line, with every point, thats what you need.

Just initialize px,py at the beginning, and add a stroke, and it will work:

Quote:
float px = 0;
float py = 0;
for (int row = 0; row < rowCount; row++) {
 float x = locationTable.getFloat(row, 1);  // column 1
 float y = locationTable.getFloat(row, 2);  // column 2
 ellipse(x, y, 4, 4);
 if (row > 0) {
   stroke(0);
  line(px, py, x, y);
 }
 px = x; py = y;




But one more thing i can help you with... just one advice...
DONT USE that cheesy Plastic wrapp PS filter background!
Page Index Toggle Pages: 1