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 › append a PVector to an array
Page Index Toggle Pages: 1
append a PVector to an array (Read 1489 times)
append a PVector to an array
Jan 27th, 2010, 2:35am
 
How can i?

Atm i get "cannot convert from object to PVector[]"
(this function is inside a class, incase that matters)

Code:
  void gridPointsToPVector(){

PVector v[];

for(int i = 0; i < g.length; i++){
if(g[i] == true){
x = i % w;
y = (i / w) % h;
z = i / (w * h);
v = append(v, x);
//PVector(x, y, z);
}
}

}


o yeah if i use:

       v = append(v, PVector(float(x), float(y), float(z)));

then i get the function PVector (float, float, float) does not exist.
And without the float i get the same error with ints.
Re: append a PVector to an array
Reply #1 - Jan 27th, 2010, 3:37am
 
Read the docs Wink

Quote:
When using an array of objects, the data returned from the function must be cast to the object array's data type. For example: SomeClass[] items = (SomeClass[]) append(originalArray, element).


So perhaps try:

Code:
v = (PVector[]) append(v, x); 

Re: append a PVector to an array
Reply #2 - Jan 27th, 2010, 3:45am
 
maybe not, but why don't you use an ArrayList?

Code:

ArrayList ddd = new ArrayList();

void gridPointsToPVector(){  
   for(int i = 0; i < g.length; i++){
if(g[i] == true){
 x = i % w;
 y = (i / w) % h;
 z = i / (w * h);
 ddd.add(new PVector(x,y,z));
}
}

// and then to retrieve the vectors
PVector temp = (PVector) ddd.get(i);

Re: append a PVector to an array
Reply #3 - Jan 27th, 2010, 4:11am
 
blindfish wrote on Jan 27th, 2010, 3:37am:
So perhaps try:

Code:
v = (PVector[]) append(v, x); 



Shocked I shouldn't try and answer questions so quickly from work.  That obviously won't work as you're not appending a PVector to the PVector array.  That should have been:

Code:
v = (PVector[]) append(v, new PVector(x, y, z)); 



(I didn't properly check to see what 'x' was in the context of your code.)

Though as GianCarlo says ArrayLists are pretty useful in this context...
Re: append a PVector to an array
Reply #4 - Jan 27th, 2010, 5:12am
 
I didn't saw arrays operations. Since when it's in processing? I missed that... So processing with a slight taste of LISP, hein?

Ok some words on Arrays Vs ArrayLists. Arrays have fixed length and ArrayList could grow. This means that with Arrays, it's difficult to add something to a list. This is why they came with ArrayList, which is more flexible, but slower.

Since Fry is a generous programmer, he had a look to how Arrays manipulations could be simplified. To add an element to an Array, you need to basically create a new Array (longer), copy everything in the new Array and then add the element. This was done with System.arraycopy() (which is Java). This is what append(), expand(), shorten() commands are doing.

Code:
PVector v[]; //<--Here, your list is not initialized 


Then in your code, when you try to add an element to a list that is not yet initialized, it might be problematic. You should try:
Code:
PVector[] v = new PVector[0]; //Empty Array of type PVector 



Then also, PVector is not a method, it is an object. You need to place 'new' in front of it:
Code:
PVector myVector = new PVector(x, y, z); 



So you end up with this:
Code:
void gridPointsToPVector(){
   
   PVector[] v = new PVector[0];
   
   for(int i = 0; i < g.length; i++){
if(g[i] == true){
 x = i % w;
 y = (i / w) % h;
 z = i / (w * h);
 v = append(v, new PVector(x, y, z));
}
   }
   
 }


As said, ArrayList may be usefull as well in your case, but, it is very different.
Re: append a PVector to an array
Reply #5 - Jan 27th, 2010, 5:43am
 
gll wrote on Jan 27th, 2010, 5:12am:
So you end up with this:
Code:
void gridPointsToPVector(){
   
   PVector[] v = new PVector[0];
   
   for(int i = 0; i < g.length; i++){
if(g[i] == true){
 x = i % w;
 y = (i / w) % h;
 z = i / (w * h);
 v = append(v, new PVector(x, y, z));
}
   }
   
 }



No - this will cause an error: "cannot convert from Object to PVector[]"

I know my initial response had a mistake, but that doesn't mean I was wrong in principle:  As per the docs you must cast append as a PVector array:

Code:
PVector[] foo = new PVector[0];

void setup() {
 size(100,100);
 println(foo.length);
 // remove the cast - (PVector[]) - and see what happens ;)
 foo = (PVector[])append(foo, new PVector(1,2,3));
 println(foo.length);
}
Re: append a PVector to an array
Reply #6 - Jan 27th, 2010, 5:54am
 
Right, I never used Array operators before. You need to cast your array before:
Code:
void gridPointsToPVector(){

PVector[] v = new PVector[0];

for(int i = 0; i < g.length; i++){
if(g[i] == true){
x = i % w;
y = (i / w) % h;
z = i / (w * h);
v = (PVector[]) append(v, new PVector(x, y, z));
}
}

}
Re: append a PVector to an array
Reply #7 - Jan 27th, 2010, 6:06am
 
gll wrote on Jan 27th, 2010, 5:12am:
ArrayList, which is more flexible, but slower.

True... and false!
Array operations are probably much slower when appending, truncating, etc. because there is an array copy each time. An array list do the same but maintains a size information so it can have a greater capacity, thus do a number of append at nearly no cost.
Now, in read mode, arrays are unbeatable, indeed. And they are much cheaper on memory when dealing with raw values (integers, floats, booleans...).
Re: append a PVector to an array
Reply #8 - Jan 28th, 2010, 6:07am
 
the array list is much faster in my case for building up (around 10 seconds).
After that i don't notice any difference between them so for this case i go with the arraylist.

thx
Page Index Toggle Pages: 1