Archimedian Spiral
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:
const speed:Number = 2; const a:Number = 0.25; const b:Number = 10; const midX:Number = 200; const midY:Number = 150; var angle:int = 0; var px:Number = 0; var py:Number = 0; var sp:Sprite = new Sprite (); sp.graphics.lineStyle (0.1, 0x000000); sp.graphics.moveTo (midX, midY); addChild (sp); addEventListener (Event.ENTER_FRAME, _enter); function _enter (e:Event) { angle -= speed; // add the midpoints so that the spiral starts from the center of your stage :) px = midX + (a * Math.cos (toRadians (angle)) * (b * toRadians (angle))); py = midY + (a * Math.sin (toRadians (angle)) * (b * toRadians (angle))); sp.graphics.lineTo (px, py); } function toRadians (degrees:Number) : Number { return ((degrees / 180) * Math.PI); }
