wah! problem with loadStrings - help!
in
Programming Questions
•
11 months ago
Hi, first time asking a question here.
I am loading data from a text file into Processing. The text file contains data about a set of trees I wish to draw in Processing.
The file (TreeFile.txt) looks like this:
Tree1_,50,250,84,0.12,0.47,0.38,21
Tree2_,770,250,100,0.11,0.26,0.62,25
There are 33 trees in TreeFile.txt: Tree1 through Tree33
The key to the line is:
Tree Name, x coordinate, y coordinate, height, first branch , second branch, third branch, trunk width
As you can see, each line has first a text element, and then 7 subsequent float/integer elements.
I wish to draw each of the 33 trees using the 7 float/integer elements, and then display the corresponding name (Tree Name) below each tree I have drawn.
However, I am getting hung up on Strings, Arrays, and Split functions. I haven't found a way to help split the file into strings and arrays, and then tell Processing to consider the first text element on each line as a text element and the remainder as floats/integers. So far I have only found code examples that show me how to turn the file into uniformly floats/integers. Therefore, when it runs, each tree has a label underneath it, but it only reads as (?) and the PrintLn in the console reveals Tree Name element to be a NAN - I assume that means 'Not a Number' or something.
Can someone help me write the code so that Processing recognizes the first element as a text (Char?) element and the rest as Float elements so I can both draw a tree and label it with its name? Here's the code (yes I know the number of code lines involving the tree-drawing are somewhat inefficient, but let's address that later):
//begin code
Tree[] trees;
void setup() {
size(1000,800);
smooth();
String [] data = loadStrings("TreeFile.txt");
trees = new Tree [data.length];
for (int i = 0; i < trees.length; i++) {
float[] values = float(split(data[i],","));
trees [i] = new Tree(values [0], values[1], values[2], values[3], values [4], values [5], values[6], values [7]);
println(values);
}
}
void draw() {
background(255);
for (int i = 0; i < trees.length; i++) {
trees[i].display();
}
save("trees.png");
}
class Tree {
float c,x,y,h,b1,b2,b3,w2;
Tree(float c_, float x_, float y_, float h_, float b1_, float b2_, float b3_, float w2_) {
c = c_;
x = x_;
y = y_;
h = h_;
b1 = b1_;
b2 = b2_;
b3 = b3_;
w2 = w2_;
}
void display() {
stroke(255);
strokeWeight (0);
fill(0,255,0,h*1.5);
ellipse(x,y-h*1.5*.75,h*(1+(h-60)/100),h);
//trunk
stroke(0);
strokeWeight (h/15);
line(x,y,x,y-h*1.5);
//branch 1
strokeWeight(b1*100/15);
line(x,(y-h*1.5*.65),(x-h/4),(y-h*1.5*.65-(10*b1)));
line(x,(y-h*1.5*.65),(x+h/4),(y-h*1.5*.65-(10*b1)));
//branch 2
strokeWeight(b2*100/15);
line(x,(y-h*1.5*.75),(x-h/3),(y-h*1.5*.75-(10*1/(h/50))));
line(x,(y-h*1.5*.75),(x+h/3),(y-h*1.5*.75-(10*1/(h/50))));
//branch 3
strokeWeight(b3*100/15);
line(x,(y-h*1.5*.85),(x-h/4),(y-h*1.5*.85-(10*1/(h/50))));
line(x,(y-h*1.5*.85),(x+h/4),(y-h*1.5*.85-(10*1/(h/50))));
text (c,x,y);
}
}
//end code
Some help would be great!! Thank you!
Mike
1