Putting an ArrayList PVector in a class?

edited May 2016 in How To...

Hello! I'm trying to figure out a way to put route lat lon coordinates (200 rows) from datasource1 inside a 'mover' class where the other variables (speed, age, type etc.) come from datasource2. The datasource2 has 10,000 movers (i.e. 10,000 rows of data), each of which needs to follow the route (200 lat lon pairs from datasource1).

My question is: Does anyone know if it is possible to include an ArrayList PVector (the lat lon coordinate pairs) inside a class of movers that has other attributes such as speed? Or can anyone suggest alternative approaches? I haven't found any examples of similar projects.

I have drawn the route as continuous spline curve (curveVertex) and was planning to create the movers as curvePoints, but not so sure anymore. Any constructive ideas most welcome!

Tagged:

Answers

  • I am not sure I fully understood your need (tired after a long day of work), but to answer your subject: yes, you can put an array list of PVectors in a class. No problem.

  • Thanks @PhiLho for confirming that it should be possible. I haven't got it working and have not found examples, so wasn't sure if it was even possible. Are you aware of any examples that shows how to make an ArrayList of Objects that contain an ArrayList of PVectors, and then how to access/iterate through the PVector values?

    Here's a where I am at the moment:

    ArrayList<PVector> coords;
    ArrayList<Mover> movers; 
    
    void setup()
    {
     //this is all fine...
    }
    
    void loadRoute() //this works fine
    {
      String[] data = loadStrings("route.csv");
      coords = new ArrayList<PVector>();
      for(int i = 0; i< data.length; i++)
      {
        String[] thisRow = split(data[i], ",");
        float x = map(float(thisRow[2]), lonmin, lonmax, 0, width);
        float y = map(float(thisRow[1]), latmin, latmax, height, 0);
    
        PVector location = new PVector(x,y);
        coords.add(location); 
      }
    }
    
    void loadMovers() //This is currently the main problem! How to add ArrayList<PVector> coords into the ArrayList<Mover>? And afterwards, how to access the values?
    {
      String[] results = loadStrings("movers.csv");
      movers = new ArrayList<Mover>();
      for(int r = 0; r< results.length; r++)
      {
        String[] thatRow = split(results[r], ",");
        Mover run = new Mover(); 
        run.speed = float(thatRow[2]);
        run.age = int (thatRow[4]);
    
        movers.add(run); //now this only has speed and age, but still no PVector coords
      }  
    }
    
    void draw() {
      background(255);
      drawRoute();
      drawMovers();
    }
    
    void drawRoute() //this works fine
    {
      noFill();
      stroke(0);
      beginShape();
      for (int i=0; i<coords.size (); i++){
        PVector p = (PVector) coords.get(i);
        if (i==0) vertex(p.x, p.y);
        curveVertex(p.x, p.y);
        if (i==coords.size()-1) vertex(p.x, p.y);
      }
      endShape();
    }
    
    void drawMovers() //this doesn't work! Haven't solved how to access the PVector values inside the mover class, once the loadMovers is sorted.
    {
      noStroke();
      for (int i = 0; i<= movers.size (); i++) {
        PVector a = movers.get(i-1);
        PVector b = movers.get(i);
        PVector c = movers.get(i+1);
        PVector d = movers.get(i+2);
        float t = (frameCount/300.0)*speed%1;
        float _x = curvePoint(a.x, b.x, c.x, d.x, t);
        float _y = curvePoint(a.y, b.y, c.y, d.y, t);
      }
      fill(200,0,200);
      ellipse(_x, _y, 3, 3);
    } 
    
    class Mover //do I need to do something else here?
    {
      float speed;
      int age;
      ArrayList<PVector> coords;
    }
    
  • Answer ✓

    You can do run.coords.add(somePVector); for example.

    Or make loadRoute to return the list of coords, which will be declared locally to the function, not globally. Then you can do run.coords = loadRoute(); in loadMovers().

  • Thanks! I'll give it a go this weekend.

  • edited April 2015

    Thanks @PhiLho I managed to put the ArrayList of PVectors inside an ArrayList of Mover objects. However, I have not yet figured out how to iterate through the ArrayList of PVectors inside the objects (pls see the void drawMovers in the code below). I keep getting IndexOutOfBoundsException notification, I guess because the ArrayList of PVectors size is 200, whereas the ArrayList of Movers is 10,000. Should it be another for loop straight after Mover currentMover = (Mover) movers.get(i)? Or something else? Thanks for all your help so far, it has helped me progress with this piece of work.

    Here's my current code:

    ArrayList<PVector> coords; 
    //floats x, y, _x,_y, speed and max&min lat&lon listed here
    
    ArrayList<Mover> movers; //Arraylist of objects with coords
    
    void setup()
    {
      size(800, 600);
      loadMovers();
      ArrayList<PVector> coords;
      ArrayList<PVector> loadRoute;
      float x;
      float y;
      float _x;
      float _y;
    
      ArrayList<Mover> movers; 
      float speed;
      PVector a, b, c, d;
    }
    
    ArrayList<PVector> loadRoute() 
    {
      String[] data = loadStrings("route.csv");
      coords = new ArrayList<PVector>();
      for(int i = 0; i< data.length; i++)
      {
        String[] thisRow = split(data[i], ",");
        float x = map(float(thisRow[2]), lonmin, lonmax, 0, width);
        float y = map(float(thisRow[1]), latmin, latmax, height, 0);
    
        PVector location = new PVector(x,y);
        coords.add(location); 
      }
      return coords;
    }
    
    void loadMovers()
    {
      String[] results = loadStrings("movers.csv");
      movers = new ArrayList<Mover>();
      for(int r = 0; r< results.length; r++)
      {
        String[] thatRow = split(results[r], ",");
        Mover run = new Mover(); 
        run.speed = float(thatRow[10]);
        run.getPosition = loadRoute();
    
        movers.add(run);
      }  
    }
    
    void draw() {
      background(255);
      drawRoute();
      drawMovers();
      noLoop();
    }
    
    void drawRoute()
    {
      noFill();
      stroke(0);
      beginShape();
      for (int i=0; i<coords.size (); i++){
        PVector p = (PVector) coords.get(i);
        if (i==0) vertex(p.x, p.y);
        curveVertex(p.x, p.y);
        if (i==coords.size()-1) vertex(p.x, p.y);
      }
      endShape();
    }
    
    void drawMovers() 
    // Here's the tricky bit: How to iterate through the PVector (temp) 
    //inside the ArrayList of Movers, when temp.size is 200, whereas movers.size is 
    //10,000? I need to return the ArrayList of PVectors for each Mover object
    {
      noStroke();
      for (int r = 0; r<= movers.size (); r++) {
        Mover currentMover = (Mover) movers.get(i);
        for (int i = 0; i<= currentMover.getPosition.size (); i++) {
           PVector temp = (PVector) currentMover.getPosition.get(i);
           //PVector a = temp.get(i-1);
           //PVector b = temp.get(i);
           //PVector c = temp.get(i+1);
           //PVector d = temp.get(i+2);
           //float t = (frameCount/300.0)*speed%1;
           //float _x = curvePoint(a.x, b.x, c.x, d.x, t);
           //float _y = curvePoint(a.y, b.y, c.y, d.y, t);
           println(temp); // this returns the ArrayList of PVectors, but it gives IndexOutOfBoundsException notification`          
      }
    }
          //fill(200,0,200);
         //ellipse(_x, _y, 3, 3);
    } 
    
    class Mover
    { 
      float speed;
      ArrayList<PVector> getPosition;
    
    }
    
  • Thanks @GoToLoop. I've read about variable scopes as you suggested, but have still not been able to figure out the answer to my problem.

    Could you specify what you think I should do, so that the ArrayList of PVectors inside each Mover object gets iterated through properly? Each Mover (10,000 of them in the ArrayList of Movers) has to follow the same route (the ArrayList of 200 PVectors). I've slightly edited the code above to show my current thinking (pls see the drawMovers), but am still getting the same IndexOutOfBoundsException notification.

Sign In or Register to comment.