FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   optimising code
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: optimising code  (Read 664 times)
spork

WWW Email
optimising code
« on: Apr 24th, 2003, 5:29pm »

so i just grabbed this yesterday, and am converting some stuff i've done in flash into p5 code, and was wondering if there is a better way to do this program:
 
Code:

// first attempt at something with this - Geoff Stearns www.deconcept.com
 
int numDots = 100;
int dotSpacing = 1;
float myRadian;
float[] dotAngle = new float[numDots];
float[] dotsXPos = new float[numDots];
float[] dotsYPos = new float[numDots];
int[] dotRads = new int[numDots];
float[] dotSpeed = new float[numDots];
float newX, newY;
 
void setup() {
  size(300, 300);
  background(255);
  stroke(52, 102, 153);
  for (int i=0;i<numDots;i++) {
    dotRads[i] = i * dotSpacing;
    dotSpeed[i] = i * 0.05;
    dotAngle[i] = 0;
  }
}
 
void loop() {
int centerX = width/2;
int centerY = height/2;
  for (int i=0;i<numDots;i++) {
    myRadian = radians(dotAngle[i]);
    dotsXPos[i] = dotRads[i]*cos(myRadian)+centerX;
    dotsYPos[i] = dotRads[i]*sin(myRadian)+centerY;
    if (i > 0) {
 line(dotsXPos[i], dotsYPos[i], dotsXPos[(i-1)], dotsYPos[(i-1)]);
    }    
    dotAngle[i] += dotSpeed[i];
  }
}
 
spork

WWW Email
Re: optimising code
« Reply #1 on: Apr 24th, 2003, 5:30pm »

could i use push() or pop() for this instead of a bunch of arrays?
 
still not sure how exactly push and pop works, but they look neato.
 
Processing

WWW Email
Re: optimising code
« Reply #2 on: Apr 25th, 2003, 11:07am »

It's really a matter of preference, but I would make a Dot class rather than having all of those loose arrays.
There are some examples showing how to construct an array of objects in the Learning/Examples section.
 
fry


WWW
Re: optimising code
« Reply #3 on: May 1st, 2003, 5:22am »

push/pop won't help in this case, though the following may be simpler:
 
Code:
void loop() {  
  int centerX = width/2;  
  int ceterY = height/2;  
  beginShape(LINE_STRIP);
  for (int i=0;i<numDots;i++) {  
    myRadian = radians(dotAngle[i]);  
    dotsXPos[i] = dotRads[i]*cos(myRadian)+centerX;  
    dotsYPos[i] = dotRads[i]*sin(myRadian)+centerY;  
    vertex(dotsXPos[i], dotsYPos[i]);      
    dotAngle[i] += dotSpeed[i];  
  }
  endShape();
}

 
a line strip keeps track of all the points so you don't have to do that extra 'if' for connecting them by hand.
 
benelek

35160983516098 WWW Email
Re: optimising code
« Reply #4 on: May 2nd, 2003, 4:58am »

the conversion from degrees to radians is unnecessary, if you lower the dotSpeed coefficient:
 
Code:
int numDots = 100;  
int dotSpacing = 1;  
float myRadian;  
float[] dotAngle = new float[numDots];  
float[] dotsXPos = new float[numDots];  
float[] dotsYPos = new float[numDots];  
int[] dotRads = new int[numDots];  
float[] dotSpeed = new float[numDots];  
float newX, newY;  
 
void setup() {  
  size(300, 300);  
  background(255);  
  stroke(52, 102, 153);  
  for (int i=0;i<numDots;i++) {  
    dotRads[i] = i * dotSpacing;  
//changed line:
    dotSpeed[i] = i * 0.001;
    dotAngle[i] = 0;  
  }  
}  
 
void loop() {  
  int centerX = width/2;  
  int centerY = height/2;  
  beginShape(LINE_STRIP);  
  for (int i=0;i<numDots;i++) {  
//changed line:
    myRadian = dotAngle[i];
    dotsXPos[i] = dotRads[i]*cos(myRadian)+centerX;  
    dotsYPos[i] = dotRads[i]*sin(myRadian)+centerY;  
    vertex(dotsXPos[i], dotsYPos[i]);  
    dotAngle[i] += dotSpeed[i];  
  }  
  endShape();  
}  
 
 
 
Pages: 1 

« Previous topic | Next topic »