Skip to content

Archimedian Spiral

by Colin on May 19th, 2009

archimedian

Ever draw spirals in your notebook when you're doodling? Well this will teach you just how to code such a thing!

First off, the math equation...

r = a + bO

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

One Comment
  1. Hi admin, do you monetize your tomorrowevening.com ?
    There is easy way to earn extra money every month, just search on youtube – How to earn with wordai 4

Leave a Reply

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

Subscribe to this comment feed via RSS