Dynamically Draw A Logarithmic Spiral


/ Published in: ActionScript 3
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. //
  2. //
  3. // centerX-- X origin of the spiral.
  4. // centerY-- Y origin of the spiral.
  5. // radius--- Distance from origin to outer arm.
  6. // sides---- Number of points or sides along the spiral's arm.
  7. // coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)
  8. // rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees)
  9. //
  10. function logSpiral(centerX, centerY, radius, sides, coils, rotation){
  11. //
  12. with(this){// Draw within the clip calling the function.
  13. //
  14. // Start at the center.
  15. moveTo(centerX, centerY);
  16. //
  17. // How far to rotate around center for each side.
  18. var aroundStep = coils/sides;// 0 to 1 based.
  19. //
  20. // Convert aroundStep to radians.
  21. var aroundRadians = aroundStep * 2 * Math.PI;
  22. //
  23. // Convert rotation to radians.
  24. rotation *= 2 * Math.PI;
  25. //
  26. // For every side, step around and away from center.
  27. for(var i=1; i<=sides; i++){
  28. //
  29. // How far away from center
  30. var away = Math.pow(radius, i/sides);
  31. //
  32. // How far around the center.
  33. var around = i * aroundRadians + rotation;
  34. //
  35. // Convert 'around' and 'away' to X and Y.
  36. var x = centerX + Math.cos(around) * away;
  37. var y = centerY + Math.sin(around) * away;
  38. //
  39. // Now that you know it, do it.
  40. lineTo(x, y);
  41. }
  42. }
  43. }
  44. //
  45. //
  46. lineStyle(2, 0x000000);// Black.
  47. // (centerX, centerY, radius, sides, coils, rotation)
  48. logSpiral( 250, 200, 320, 700, 14, 0);
  49. lineStyle(2, 0xD0D0D0);// Gray.
  50. logSpiral( 250, 200, 320, 700, 14, .5);
  51. //
  52. //

URL: http://www.pixelwit.com/blog

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.