How to read from a text file to arraylist of pvectors

Hi,

I have a text file having data in the form of:

x & y co-ordinates:

880.4895 373.5595

797.7594 551.85364

582.87476 590.6258

in each line, i have to store these 3 lines in a arraylist of pvector each having x & y co-ordinates arraylist of size 3.

Can you please give me a sample code on how to split and load this data into arralist of pvectors.

Thanks in advance.

Answers

  • edited January 11

    https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Still very confusing. Are those 3 coordinate pairs in the same line in their file like this?
    880.4895 373.5595 797.7594 551.85364 582.87476 590.6258

    Do you wanna an ArrayList of PVector[] array?
    And each PVector[] stores 3 PVector coords.?

    import java.util.List;
    final List<PVector[]> trioVecs = new ArrayList<PVector[]>();
    
  • edited January 11

    @GoToLoop Yes those are three co-ordinate pairs each pair in one line of the text file. Should be read to an arraylist of pvectors

    Each pair in one separate line of text file, should be read into an arraylist of pvectors(x,y) of size same as the number of lines in text file.

  • @GoToLoop Yes in this case, we have to read each line and store x & y coordinates in arraylist of pvectors, whereas the default z will be zero. Can you please tell me how to read the input text file,

    I usually do read it when it is a string of inputs, but in this case each line will have a pair of co-ordinates, Please show how to read and load the arraylist of pvectors

  • edited January 11 Answer ✓
    // forum.Processing.org/two/discussion/20185/
    // how-to-read-from-a-text-file-to-arraylist-of-pvectors#Item_4
    
    // GoToLoop (2017-Jan-10)
    
    PVector[] coords;
    
    String[] pairs = loadStrings("coords.txt");
    int len = pairs.length;
    coords = new PVector[len];
    
    for (int i = 0; i < len; ++i) {
      float[] pair = float(trim(splitTokens(pairs[i])));
      coords[i] = new PVector(pair[0], pair[1]);
    }
    
    printArray(coords);
    exit();
    

    880.4895 373.5595
    797.7594 551.85364
    582.87476 590.6258
    
  • Thank you very much! That answers all.. :) :) really appreciate your help.

Sign In or Register to comment.