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.
Page Index Toggle Pages: 1
create picture (Read 794 times)
create picture
Apr 27th, 2009, 3:49am
 
Hello,
how it is possible to create a 2D picture from this file:
39      18.378  59.466  13.185
40      14.270  54.556  18.227
41      30.119  51.333  33.732
43      26.326  51.100  21.154
44      23.764  55.050  18.338
45      17.697  75.884  26.178
48      22.351  66.269  28.727
49      19.980  63.046  33.535

The first integer is the point label and the three double are the coordinates.

Exist anywhere an example to do this?

Thank you in advance.


Re: create picture
Reply #1 - Apr 27th, 2009, 4:50am
 
you need to load the file into a String array with loadStrings("filename.txt");
then you need to manipulate each line so you get the numbers out seperately.
then you need to run a Float.toFloat() on the different stringed numbers, to turn them into doubles, and store that in another array.
then you draw them.

Something like this
[code]
String[] s = loadStrings("file.txt");
float[][] numbers = new float[s.length][4];
for(int i = 0; i < s.length; i++){
 String[] line = splitTokens(s[i]);
 for(int j = 0; j < line.length; j++){
   numbers[i][j] = Float.toFloat(line[j]);
 }
}

beginShape();
for(int i = 1; i < numbers.length; i++){
 vertex(numbers[i-1][1],numbers[i-1][2],numbers[i-1][3]);
}
endShape();
[code]
Re: create picture
Reply #2 - May 1st, 2009, 1:15am
 
Thank you for your code, but unfortunately I get with processing 1.0.3 this error:
The function toFloat(String) does not exist.

What can I do?
Re: create picture
Reply #3 - May 1st, 2009, 2:34am
 
That's Float.parseFloat(), but you have to catch an exception.
Just use Processing's float() function instead, it will do that for you.
Re: create picture
Reply #4 - May 1st, 2009, 3:36am
 
Thank you, float() works, but now I get this error:
vertex() with x, y, and z coordinates can only be used with a renderer that supports 3D, such as P3D or OPENGL. Use a version without a z-coordinate instead.

I think I need P3D. How can I solve this problem?
Re: create picture
Reply #5 - May 1st, 2009, 5:32am
 
xyz wrote on May 1st, 2009, 3:36am:
I think I need P3D. How can I solve this problem
Well, just use P3D!

Tongue

Look at size()
Re: create picture
Reply #6 - May 1st, 2009, 7:32pm
 
Now I get only an empty window, but without points with this code:
Code:
void setup() {
 size(screen.width, screen.height,P3D);
}

String[] s = loadStrings("file.txt");
float[][] numbers = new float[s.length][4];
for(int i = 0; i < s.length; i++){
String[] line = splitTokens(s[i]);
for(int j = 0; j < line.length; j++){
  numbers[i][j] = float(line[j]);
 
}
}

void draw(){
 beginShape();
   for(int i = 1; i < numbers.length; i++){
    vertex(numbers[i-1][1],numbers[i-1][2],numbers[i-1][3]);
   }
 endShape();
}

What do I wrong?
Page Index Toggle Pages: 1