XYZ + RGB from array

Hi, Any hint how this piece of code make better? Thanks to iainmaxwell tutorial I made simple diagram, but for every color I'm using extra loop and void. Can anybody give me any hint how to read r/g/b from csv and add to single loop?

ArrayList pointListedu1; // arraylist to store the points in
ArrayList pointListedu2;   
 

void setup() {
  size(1000, 500, OPENGL);
  background(255);

 
  pointListedu1 = new ArrayList();    // instantiate an ArrayList
  importTextFileedu1();               // call our custom function
  pointListedu2 = new ArrayList();    
  importTextFileedu2();   


void draw() {

  //-------------------------------- GENERAL
  background(0,0,50);
  lights();
  stroke(110);
        
  //-------------------------------- VISUALIZE THE POINT SET

     
        stroke(255, 215, 0);        // gold 
        for (int i = 0; i < pointListedu1.size (); ++i) {        
        PVector V = (PVector) pointListedu1.get(i);
        ellipse(V.x, V.y, 0.1, 0.1);
}        
        stroke(255, 165, 0);        // orange
        for (int i = 0; i < pointListedu2.size (); ++i) {        
        PVector V = (PVector) pointListedu2.get(i);
        ellipse(V.x, V.y, 0.1, 0.1);
}        


void importTextFileedu1() {  

  String[] strLines = loadStrings("edu1.txt"); // the name and extension of the file to import!

  for (int i = 0; i < strLines.length; ++i) {
    String[] arrTokens = split(strLines[i], ',');       // use the split array with character to isolate each component
    float xx = float(arrTokens[0]);                     // cast string value to a float values!
    float yy = float(arrTokens[1]);                     // cast string value to a float values!
    float zz = float(arrTokens[2]); 

    pointListedu1.add( new PVector(xx, yy, zz) );             // add values to a new array slot
  }
}
void importTextFileedu2() {  

  String[] strLines = loadStrings("edu2.txt"); // the name and extension of the file to import!

  for (int i = 0; i < strLines.length; ++i) {
    String[] arrTokens = split(strLines[i], ',');       // use the split array with character to isolate each component
    float xx = float(arrTokens[0]);                     // cast string value to a float values!
    float yy = float(arrTokens[1]);                     // cast string value to a float values!
    float zz = float(arrTokens[2]); 

    pointListedu2.add( new PVector(xx, yy, zz) );             // add values to a new array slot
  }
}
Tagged:

Comments

  • edited January 2015

    By chance you meant a general loadCoordsFile() method which returns a List<PVector>? 8-|

    // forum.processing.org/two/discussion/9216/xyz-rgb-from-array
    
    import java.util.List;
    
    List<PVector> loadCoordsFile(String path, char delim) {
      String[] lines = loadStrings(path);
      assert lines != null : "\"" + path + "\" wasn't found!";
    
      List<PVector> vecs = new ArrayList<PVector>(lines.length);
    
      for (int i = 0; i != lines.length; ++i) {
        float[] coords = float(split(lines[i], delim));
    
        float x = coords.length > 0? coords[0] : 0;
        float y = coords.length > 1? coords[1] : 0;
        float z = coords.length > 2? coords[2] : 0;
    
        vecs.add(new PVector(x, y, z));
      }
    
      return vecs;
    }
    
    static final String NAME = "edu", EXT = ".txt";
    static final int QTY = 2;
    final List<PVector>[] points = new List[QTY];
    
    void setup() {
      size(1000, 600, P3D);
      smooth(8);
      frameRate(60);
      strokeWeight(4);
    
      for (int i = 0; i != QTY; points[i++] =
        loadCoordsFile(NAME + i + EXT, ','));
    
      printArray(points);
    }
    
    void draw() {
      background(0, 0, 50); // dark indigo
      lights();
    
      stroke(255, 215, 0); // gold
      for (PVector v : points[0])  point(v.x, v.y, v.z);
    
      stroke(255, 165, 0); // orange
      for (PVector v : points[1])  point(v.x, v.y, v.z);
    }
    
Sign In or Register to comment.