Storing a Two-Dimensional PVector Array in Memory

Hello Everybody!

I have a sketch with a Two-Dimensional PVector Array (with 5 arrays of 2 PVectors each). When assigning values to each PVector using mouse clicks, I print each array of 2 PVectors to the console, followed by a comma.

What I get after running the sketch is this:

[ 91.0, 399.0, 0.0 ] [ 82.0, 323.0, 0.0 ], [ 81.0, 271.0, 0.0 ] [ 87.0, 206.0, 0.0 ], [ 101.0, 146.0, 0.0 ] [ 134.0, 100.0, 0.0 ], [ 193.0, 91.0, 0.0 ] [ 258.0, 106.0, 0.0 ], [ 298.0, 168.0, 0.0 ] [ 331.0, 271.0, 0.0 ]

Now, what I want to do is use these values to create another two-dimensional array in a different sketch.

I tried using this code, but it doesn't seem to work:

PVector [][] people = {{[130.0, 372.0, 0.0], [132.0, 340.0, 0.0 ]}, {[ 131.0, 282.0, 0.0 ], [ 124.0, 240.0, 0.0 ]}, {[ 118.0, 210.0, 0.0 ], [ 114.0, 171.0, 0.0 ]}, {[ 119.0, 135.0, 0.0 ], [ 143.0, 112.0, 0.0 ]}, {[ 171.0, 105.0, 0.0 ], [ 210.0, 117.0, 0.0]}}

How can I use the values printed in the console to define the same 2d array of PVector's in a different sketch?

Thanks!!!

Answers

  • edited January 2014 Answer ✓

    [130.0, 372.0, 0.0] and such are merely a String representation of a PVector's 3 fields -> x, y, z!

    To get a real PVector, we gotta use keyword new to instantiate 1:

    final PVector[][] people = {
      {new PVector(130, 372), new PVector(132, 340)}, 
      {new PVector(131, 282), new PVector(124, 240)}
    };
    
    for (PVector[] pArr: people) {
      println(pArr);
      println();
    }
    
    exit();
    
  • Answer ✓

    Ah! A sample of how to save a List<PVector> as a file.
    With some adaptations, might ease your work, who knows?

    // forum.processing.org/one/topic/saving-mouseevent-data-in-an-array
    // forum.processing.org/two/discussion/160/exporting-data
    
    import java.util.List;
    
    final static List<PVector> coords = new ArrayList();
    
    final static color BG = -1, FG = 0300, BORDER = 0;
    final static short DIM = 20, BOLD = 2, FPS = 100;
    
    void setup() {
      size(640, 480);
      smooth();
      noLoop();
      frameRate(FPS);
    
      fill(FG);
      stroke(BORDER);
      strokeWeight(BOLD);
      background(BG);
    }
    
    void draw() {
      print(coords.size() + " - ");
    }
    
    void mouseDragged() {
      redraw();
      coords.add( new PVector(mouseX, mouseY) );
      ellipse(mouseX, mouseY, DIM, DIM);
    }
    
    void keyTyped() {
      if (key != ' ' & key != RETURN & key != ENTER)  return;
    
      final String archive = dataPath("MouseCoords.txt");
    
      final int num = coords.size();
      final String[] coordsTxt = new String[num];
    
      for (int i = 0; i != num; ++i) {
        //final PVector coord = coords.get(i);
        //coordsTxt[i] = coord.x + "," + coord.y;
    
        final int[] coord = int( coords.get(i).array() );
        coordsTxt[i] = coord[0] + "," + coord[1];
      }
    
      saveStrings(archive, coordsTxt);
      println("\n\nMOUSE COORDS SAVED!\n");
    }
    
  • Great, thanks for the help!

Sign In or Register to comment.