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
   Programs
(Moderators: fry, REAS)
   spirograph - speed?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: spirograph - speed?  (Read 720 times)
kevin

WWW
spirograph - speed?
« on: Jul 14th, 2003, 8:51pm »

Hi all.
 
Sorry for the couple of questions but I've had a quiet weekend and have just discovered p5.
 
Anyways, I'm trying to draw a spiropgraph.
The general algoritm is defined here:
http://www.wordsmith.org/~anu/java/spirograph.html
 
Now, I've got it working nicely where the graph slowly 'builds up', but I'd  like to be able to modify it in realtime and I can't seem to 'speed up' the code.
 
I have a feeling my method for drawing the graph is the problem.
 
Here's the code, as small as possible so you can take a look.
 
Code:

// setup variables
float x, y, R, r, t, O, loggedX, loggedY;
 
size(500,500);
noBackground();
 
R = 25;
r = 5;
t = 0;
O = random(10);
 
// center graph
translate(width/2, height/2);
 
// Draw Spirograph loop
for (t=0;t<O;t=t+0.1) {
 
  // spirograph equation
  x = (R+r)*cos(t) - (r+O)*cos(((R+r)/r)*t);
  y = (R+r)*sin(t) - (r+O)*sin(((R+r)/r)*t);
 
  // draw point
  point(x,y);
}

 
Also, i've looked at the other spirograph made in proce55ing, but the code it's
a little over my head at the moment. http://www.readonly.net/P5/spirograph/
 
Hope you can give me some pointers,
 
thanks
 
- Kevin
 
benelek

35160983516098 WWW Email
Re: spirograph - speed?
« Reply #1 on: Jul 15th, 2003, 8:21am »

Hi Kevin, welcome to p5! (hope you enjoy your stay, in the case of an emergency the exits are here... and here...)
 
a few pointers:
 
1. "t" can be defined within the for() loop, so you don't need to take care of it beforehand (this also makes the for() loop run a little faster). ie, "for(float t=0; t<..."
 
2. it took me a minute to figure out you were using the variable named "O" and not the number zero!
 
3. "t=t+0.1" is the same as "t+=0.1". but don't actually know if this makes the operation any faster...
 
4. you're doing a whole lot of math more than once. things like "((R+r)/r)*t" can be defined in another variable once at the begining of the for() loop, so you'd save time on each pass at the loop.
 
 
hope this's helpful,
 
~benelek.
 
kevin

WWW
Re: spirograph - speed?
« Reply #2 on: Jul 15th, 2003, 10:39am »

Hey, thanks for the welcome.
 
Some good tips there, I'll definately give them a shot when I get home.
 
Cheers
 
fry


WWW
Re: spirograph - speed?
« Reply #3 on: Jul 15th, 2003, 8:44pm »

i think the big one is the translate.. if you get rid of that by just adding width/2 and height/2 to your x and y, then you may see a big jump.
 
if you wanna get fancy, you can make an array of a 1000 or so sine and cosine values, since they're slow to calculate, and that'll give you some further speed.
 
also, replacing point(x, y) with vertex(x, y) and putting beginShape(POINTS) before your loop and endShape() after it may give you further speed, though i'm not sure.
 
kevin

WWW
Re: spirograph - speed?
« Reply #4 on: Jul 15th, 2003, 10:45pm »

Thanks guys for all the help.
I implemented some of the things and got something working.
 
Nothing amazing, but I thought you might be curious what I came up with:
 
http://www.multiblah.com/exps/p5/spiro_speedtest_01/
 
Thanks
 
Pages: 1 

« Previous topic | Next topic »