Dynamically Draw A Spiral


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

Need to fix encapsulation on this.


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 spiral(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 step away from center for each side.
  18. var awayStep = radius/sides;
  19. //
  20. // How far to rotate around center for each side.
  21. var aroundStep = coils/sides;// 0 to 1 based.
  22. //
  23. // Convert aroundStep to radians.
  24. var aroundRadians = aroundStep * 2 * Math.PI;
  25. //
  26. // Convert rotation to radians.
  27. rotation *= 2 * Math.PI;
  28. //
  29. // For every side, step around and away from center.
  30. for(var i=1; i<=sides; i++){
  31. //
  32. // How far away from center
  33. var away = i * awayStep;
  34. //
  35. // How far around the center.
  36. var around = i * aroundRadians + rotation;
  37. //
  38. // Convert 'around' and 'away' to X and Y.
  39. var x = centerX + Math.cos(around) * away;
  40. var y = centerY + Math.sin(around) * away;
  41. //
  42. // Now that you know it, do it.
  43. lineTo(x, y);
  44. }
  45. }
  46. }
  47. //
  48. //
  49. //
  50. // spiral(centerX, centerY, radius, sides, coils, rotation).
  51. //
  52. // Big center spirals.
  53. lineStyle(27, 0x0000FF);// Blue.
  54. spiral(250, 210, 200, 200, 4, 0);
  55. lineStyle(27, 0xFF0000);// Red.
  56. spiral(250, 210, 127, 200, 2.5, .5);
  57. //
  58. // Small corner spirals.
  59. lineStyle(4, 0xFF00FF);// Magenta.
  60. spiral(50, 50, 50, 200, 6, 0);// Big.
  61. spiral(125, 50, 25, 200, 2, .5);// Small.
  62. lineStyle(4, 0x00FFFF);// Cyan.
  63. spiral(450, 50, 50, 200, -6, .5);// Big.
  64. spiral(375, 50, 25, 200, -2, 0);// Small.
  65. lineStyle(4, 0xFFFF00);// Yellow.
  66. spiral(50, 350, 50, 200, -4, 0);// Big.
  67. spiral(125, 350, 25, 200, -3, .5);// Small.
  68. lineStyle(4, 0x00FF00);// Green.
  69. spiral(450, 350, 50, 200, 4, .5);// Big.
  70. spiral(375, 350, 25, 200, 3, 0);// Small.
  71. //
  72. //

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.