PhiLho wrote on May 29th, 2010, 8:26am:Good advices above...
Classes might seem intimidating but you can use them in their simplest form, a way to group together a number of variables (a name, some booleans, integers and float numbers, etc.).
Side note:
when I do a search loop like this one, even a short one as shown, I always add a
break; after the index assignment, as there is no reasons to continue the search...
Indeed, good advices. I've posted these before, but maybe it's handy to have some code examples. Below are part of my custom 'standard' functions and classes, but they may very well help you. I've been through the same process when loading data; I used to save the columns to separate arrays, but classes are the way to store data. And these are in fact very logical when you use them.
Code:int returnIndexOf(String[] arr, String var) {
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(var)) {
return i;
}
}
return -1;
}
int returnIndexOf(int[] arr, int var) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == var) {
return i;
}
}
return -1;
}
etc.
and the "Point class", one of the simplest examples of using classes to store data, instead of separate xpos and ypos arrays (or even twodimensional coordinate arrays)
Code:class Point {
float x, y, z;
Point(float x, float y) {
this.x = x;
this.y = y;
}
Point(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
}
And example usage:
Code:Point[] coordinates = new Point[0];
void setup() {
size(500, 500);
frameRate(30);
}
void draw() {
background(255);
noFill();
beginShape();
for(int i = 0; i < coordinates.length; i++) {
vertex(coordinates[i].x, coordinates[i].y);
}
endShape();
}
void mouseMoved() {
Point mousepos = new Point(mouseX, mouseY);
coordinates = (Point[]) append(coordinates, mousepos);
}