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 › Trig Animation Optimization
Page Index Toggle Pages: 1
Trig Animation Optimization (Read 523 times)
Trig Animation Optimization
Apr 15th, 2009, 9:05am
 
Hello all,

This is one of my first sketches in processing:
http://mmas.unca.edu:16080/~djratlif/processing/strings/applet/

I was wondering if people could give me feedback as far as code (and whatever you wish, as long as you might give me some insight as how to do it!)

Since I am fairly new to programing I probablly have redudencies and/or things I could be doing better (maybe speed up processing time).

My code is on the page but I will post it here as well
Code:

//Trig Strings : By Drew Ratliff


//trig stuff
float angle = 0.0;
float speed = .05;
//radius
float range = 300;

Wave myWave;

void setup() {
 size (750,300);
}


void draw () {
 //set background, make random same, translate to middle
 background(0);
 randomSeed(6);
 pushMatrix();
 translate(0,height/2);
 
 
 for(int i = 0; i < 100; i++) {
   //set random blue stroke to strings
   stroke(random(255),random(255),random(200,255),random(100,200));
   
   //make wave with random x pos for bezier curves
   myWave = new Wave(int(random(width)),int(random(width)));
   
   myWave.display();
 }
 
 angle += speed;
 popMatrix();
}



class Wave {
 float b1x;
 float b1y;
 float b2x;
 float b2y;
 float sinval;
 float cosval;

 
 Wave(float x1, float x2) {

   b1x = x1;
   b2x = x2;
 }
 
 void display() {
   
   //Trig Math for motion
   sinval = sin(random(angle));
   cosval = cos(random(angle));
   float b1y =  (sinval * range);
   float b2y = (cosval * range);
   
   //draw string
   noFill();
   beginShape();
   vertex(0,0);
   bezierVertex(b1x,b1y,b2x,b2y,width,0);
   endShape();
 }
}


thanks for listening

<moved from exhibition>
Re: Trig Animation Optimization
Reply #1 - Apr 15th, 2009, 9:23am
 
I don't know if you will gain much from this but a few ideas:

Use an array of Wave objects. Initialize them once in setup() rather than creating a new Wave for each one drawn.

Then add an update(float f1, float f2) method inside the Wave class that you would pass the new random set points.

Unless you really want the colors to change for your presentation, I would recommend adding a color property to the Wave class and only set that once as well.

PS. Push- and popMatrix() are not necessary. They are helpful if you will be drawing several different shapes at different positions/rotations.
Though the gain will probably be unnoticeable.
Re: Trig Animation Optimization
Reply #2 - Apr 15th, 2009, 3:09pm
 
Hey NoahBuddy

thanks for the reply, i do gain much from your comments because that type of feedback is exactly what i was looking for

i did most of what you said except add the update(float f1, float f2) method

by "random set points" I am assuming you mean the set x positions in my bezier curves which are placed randomly, which I define in my constructor:
Code:

Wave(float x1, float x2) {

   b1x = x1;
   b2x = x2;


what is the purpose of making a seperate method for this.  i know update methods are used frequently, but my display method updates my values effected by sin and cos.  

heres all my updated code:
Code:

//Trig Strings : By Drew Ratliff


//trig stuff
float angle = 0.0;
float speed = .05;
//radius
float range = 300;

Wave[] wavers;

void setup() {
 smooth();
 size (750,300);

 wavers = new Wave[100];
 
 for(int i = 0; i < wavers.length; i++) {
   //random set x for...       bezier1           bezier2
   wavers[i] = new Wave(int(random(width)),int(random(width)));
 }

}


void draw () {
 //set background, make random same, translate to middle
 background(0);
 randomSeed(6);
 translate(0,height/2);
 
 for(int i = 0; i < wavers.length; i++) {

   wavers[i].display();
   //wavers[i].update();
 }
 
 angle += speed;
}



class Wave {
 float b1x;
 float b1y;
 float b2x;
 float b2y;
 float sinval;
 float cosval;
 color neons;

 
 Wave(float x1, float x2) {

   b1x = x1;
   b2x = x2;
   neons = color(random(255),random(255),random(200,255),random(100,200));
 }
 
 void display() {
   
   //Trig Math for motion
   sinval = sin(random(angle));
   cosval = cos(random(angle));
   float b1y =  (sinval * range);
   float b2y = (cosval * range);
   
   stroke(neons);
   
   //draw string
   noFill();
   beginShape();
   vertex(0,0);
   bezierVertex(b1x,b1y,b2x,b2y,width,0);
   endShape();
 }
 
 /*void update() {
   
   sinval = sin(random(angle));
   cosval = cos(random(angle));
   float b1y =  (sinval * range);
   float b2y = (cosval * range);
 }*/
}


i know i probablly sound like an idiot, im just trying to make sense of it to learn for myself

thx for replying and giving me your input
Re: Trig Animation Optimization
Reply #3 - Apr 15th, 2009, 4:12pm
 
The way you did it is fine.

I was just suggesting to reduce the number of times you create a new object, which you did. :]

You can also speed it up by not using smooth() or alpha on the colors.
But then it wouldn't be as pretty. Wink

Edit: Oh, and you might try P2D or P3d for the renderer.
Re: Trig Animation Optimization
Reply #4 - Apr 16th, 2009, 2:18pm
 
hah yea the point is to be pretty... i think...

i didnt know what P2D was or P3D was till you mentioned it.  looked it up briefly but then found out all the cool shit i can do with processing and video capture (which uses P2D i believe).

Page Index Toggle Pages: 1