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 & HelpPrograms › problem with expanding an Vector3D[]
Page Index Toggle Pages: 1
problem with expanding an Vector3D[] (Read 292 times)
problem with expanding an Vector3D[]
Sep 16th, 2008, 5:08pm
 
hi everyone,

i want to write a programm that draws voronoi cells based on a particle system. yet i didn´t come too far. my problem is that i always get arrayIndexOutOfBounds. i want to use the expand function but that seems not to work.
thank you for every answer.
here is the code

import noc.*;


Particle[] p;
int numP = 50;

float xpos,ypos,dia;

void setup(){
 
 size(400,400);
 
 
 
 p = new Particle[numP];
 
 for(int i=0;i<numP;i++){
   
   xpos = random(0,width);
   ypos = random(0,height);
   dia = 10;
   
   p[i] = new Particle(xpos,ypos,dia);
   
 }
 
}

void draw(){
 
 background(0);
 
 Vector3D[] allPts = new Vector3D[numP];
 
 for(int i=0;i<numP;i++){
 
   allPts[i] = new Vector3D(p[i].xpos,p[i].ypos);
 }
 
 for(int i=0;i<numP;i++){
   
   p[i].display();
   
   Vector3D pt = allPts[i];    
   connect(pt,allPts);
   
 }
 
}

void connect(Vector3D pt,Vector3D[] allPts){
 
 float d;
 float maxD = 10.0;
 int numRel,numPts;
 
 int c = 0;
 Vector3D[] vecRel = new Vector3D[c];
   
 numPts = allPts.length;

 for(int i=0;i<numPts;i++){
   
   Vector3D vecA = new Vector3D();
   vecA.sub(pt,allPts[i]);
   d = vecA.magnitude();
   
   /*###############################################
   vecRel does not expand
   */
   
   if(d<maxD){
     
     println("c = " + c);
     vecRel[c] = allPts[i];
                   
     c += 1;
     vecRel = (Vector3D[]) expand(vecRel,c);
     println("test");
   }  
 }
 
 //######################################################
   
}
Re: problem with expanding an Vector3D[]
Reply #1 - Sep 16th, 2008, 6:43pm
 
look into using one of the other java collection types like Vector or ArrayList - you can append things to these very easily.
Re: problem with expanding an Vector3D[]
Reply #2 - Sep 17th, 2008, 12:39am
 
thank you very much, ArrayList helped.
Page Index Toggle Pages: 1