We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › array for 3d coordinates
Page Index Toggle Pages: 1
array for 3d coordinates (Read 490 times)
array for 3d coordinates
Apr 20th, 2009, 12:12am
 
hi,
id like to store 10 last positions for each particle so i could draw a trail for them.
what would be the best and most efficient way to store and update the position list?
im really nb on processing so im bit unsure what data type id should choose for it. currently im using PVector for all the other vector variables. can there be  PVector[]? and for updating the array, is there any better solution than using maybe append and subset to add new and delete the oldest position.

thanks!
Re: array for 3d coordinates
Reply #1 - Apr 20th, 2009, 3:11am
 
As you have discovered there are problems using an array in this situation, one solution is to use a LinkedList. The program below demonstrates how a linked list might be used to store just the last 10 values added.

Code:

import java.util.*;

PVector pv;
LinkedList list;
int nbrToRemember = 10;

void setup(){
list = new LinkedList();

// simulate movement from 0,0,0 to 99,99,99
for(int i = 0; i < 100; i++){
pv = new PVector(i,i,i);
addNewPosition(pv);
}
displayList();
}

// Add a new PVector but only remeber the last 'nbrToRemember'
void addNewPosition(PVector p){
list.addFirst(p);
while(list.size() > nbrToRemember)
list.removeLast();
}

// Loop through the vetors and do something with them
// this could be part of your draw() method
void displayList(){
PVector tempPv;
ListIterator iter = list.listIterator();
while(iter.hasNext()){
tempPv = (PVector)iter.next();
println(tempPv);
}
}
Re: array for 3d coordinates
Reply #2 - Apr 20th, 2009, 5:23am
 
thanks Q!
thats exactly what i needed.
Page Index Toggle Pages: 1