Skip to content

Logarithmic Spiral

by Colin on May 19th, 2009

logarithmic
Like my previous post, I'll teach you how to draw a spiral using code. This spiral's radius increases per iteration, thus the distance between arms increasing.

First off, the math equation...

r = a * b * O

r = Radius
a = Turn of the spiral
b = Distance between arms
O = Theta (greek character now allowed in post)

Theta is the variable which is constantly updated, so in the instance of showing the spiral being created, we'll say that O = the degree; in other words, O = (O < 360) ? O++ : O = 0;

We also need to determine the X and Y values of where to draw the new point. Since we get the value for r, we simply need to split it up uysing sin and cosine.

Cosine is used to retrieve the X value
Sine is used to retrieve the Y value

Here's our final code:

  1. const speed:Number = 2;
  2. const a:Number = 0.25;
  3. const b:Number = 10;
  4. const midX:Number = 200;
  5. const midY:Number = 150;
  6.  
  7. var angle:int = 0;
  8. var px:Number = 0;
  9. var py:Number = 0;
  10.  
  11. var sp:Sprite = new Sprite ();
  12. sp.graphics.lineStyle (0.1, 0x000000);
  13. sp.graphics.moveTo (midX, midY);
  14. addChild (sp);
  15.  
  16. addEventListener (Event.ENTER_FRAME, _enter);
  17.  
  18. function _enter (e:Event) {
  19. angle -= speed;
  20. // add the midpoints so that the spiral starts from the center of your stage :)
  21. px = midX + (a * Math.cos (toRadians (angle)) * (b * toRadians (angle)));
  22. py = midY + (a * Math.sin (toRadians (angle)) * (b * toRadians (angle)));
  23. sp.graphics.lineTo (px, py);
  24. }
  25.  
  26. function toRadians (degrees:Number) : Number {
  27. return ((degrees / 180) * Math.PI);
  28. }

From → Flash

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS