Dynamically Draw A Logarithmic Spiral

Submitted by:Pitter Alderson

Date added:06 March, 2014

Category:ActionScript 3

Need to adjust this code to be encapsulated. Linestyle is not being called correctly.

Tags: draw , logarithmic

Code Snippet:

    //
// centerX-- X origin of the spiral.
// centerY-- Y origin of the spiral.
// radius--- Distance from origin to outer arm.
// sides---- Number of points or sides along the spiral's arm.
// coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)
// rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees)
//
function logSpiral(centerX, centerY, radius, sides, coils, rotation){
//
with(this){// Draw within the clip calling the function.
//
// Start at the center.
moveTo(centerX, centerY);
//
// How far to rotate around center for each side.
var aroundStep = coils/sides;// 0 to 1 based.
//
// Convert aroundStep to radians.
var aroundRadians = aroundStep * 2 * Math.PI;
//
// Convert rotation to radians.
rotation *= 2 * Math.PI;
//
// For every side, step around and away from center.
for(var i=1; i<=sides; i++){
//
// How far away from center
var away = Math.pow(radius, i/sides);
//
// How far around the center.
var around = i * aroundRadians + rotation;
//
// Convert 'around' and 'away' to X and Y.
var x = centerX + Math.cos(around) * away;
var y = centerY + Math.sin(around) * away;
//
// Now that you know it, do it.
lineTo(x, y);
}
}
}
//
//
lineStyle(2, 0x000000);// Black.
// (centerX, centerY, radius, sides, coils, rotation)
logSpiral( 250, 200, 320, 700, 14, 0);
lineStyle(2, 0xD0D0D0);// Gray.
logSpiral( 250, 200, 320, 700, 14, .5);
//
 
 

Comments