/Flash project/src/com/danta/util/ArcHelper.as

https://github.com/just-granada/RadialFortress
ActionScript | 152 lines | 60 code | 22 blank | 70 comment | 7 complexity | b676b127d1fb8e487e1c10080dfa1d8e MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Zemoga Inc
  4. // Copyright 2011 Zemoga Inc
  5. // All Rights Reserved.
  6. //
  7. ////////////////////////////////////////////////////////////////////////////////
  8. package com.danta.util
  9. {
  10. import flash.display.DisplayObject;
  11. import flash.display.Sprite;
  12. /**
  13. * Class description here
  14. *
  15. * @author camilosoto <email>
  16. */
  17. public class ArcHelper
  18. {
  19. //------------------------------------------------------------------------------
  20. //
  21. // Constants
  22. //
  23. //------------------------------------------------------------------------------
  24. //--------------------------------------
  25. // Public
  26. //--------------------------------------
  27. //--------------------------------------
  28. // Private
  29. //--------------------------------------
  30. //------------------------------------------------------------------------------
  31. //
  32. // Static Methods
  33. //
  34. //------------------------------------------------------------------------------
  35. public static function toDeg(rad:Number):Number
  36. {
  37. return 180*(rad/Math.PI);
  38. }
  39. public static function toRad(deg:Number):Number
  40. {
  41. return Math.PI*(deg/180);
  42. }
  43. public static function drawArc(target:Sprite, innerRadius:Number, outerRadius:Number, angleStart:Number, angleEnd:Number, subdivs:int=5):void
  44. {
  45. var i:int;
  46. var angleDist:Number=angleEnd-angleStart;
  47. if(innerRadius>0)
  48. {
  49. target.graphics.moveTo(Math.sin(angleStart)*innerRadius,Math.cos(angleStart)*innerRadius); //move to the initial point
  50. target.graphics.lineTo(Math.sin(angleStart)*outerRadius,Math.cos(angleStart)*outerRadius); //draw the first radius
  51. for(i=1; i<=subdivs; i++) // draw the outer arc
  52. {
  53. target.graphics.lineTo(Math.sin(angleStart + i*angleDist/subdivs)*outerRadius,Math.cos(angleStart + i*angleDist/subdivs)*outerRadius);
  54. }
  55. target.graphics.lineTo(Math.sin(angleEnd)*innerRadius, Math.cos(angleEnd)*innerRadius); //draw the second radius
  56. for(i=1; i<=subdivs; i++) // draw the inner arc
  57. {
  58. target.graphics.lineTo(Math.sin(angleEnd - i*angleDist/subdivs)*innerRadius,Math.cos(angleEnd - i*angleDist/subdivs)*innerRadius);
  59. }
  60. }
  61. else
  62. {
  63. target.graphics.moveTo(0,0);
  64. target.graphics.lineTo(Math.sin(angleStart)*outerRadius,Math.cos(angleStart)*outerRadius); //draw the first radius
  65. for(i=1; i<=subdivs; i++) // draw the aouter arc
  66. {
  67. target.graphics.lineTo(Math.sin(angleStart + i*angleDist/subdivs)*outerRadius,Math.cos(angleStart + i*angleDist/subdivs)*outerRadius);
  68. }
  69. target.graphics.lineTo(0,0); //draw the second radius
  70. }
  71. }
  72. public static function normalize(angle:Number):Number
  73. {
  74. if(angle<0)
  75. {
  76. return 2*Math.PI+angle;
  77. }
  78. else if(angle >Math.PI*2)
  79. {
  80. return angle - Math.PI*2;
  81. }
  82. return angle;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. //
  86. // Constructor
  87. //
  88. ////////////////////////////////////////////////////////////////////////////////
  89. public function ArcHelper()
  90. {
  91. }
  92. //------------------------------------------------------------------------------
  93. //
  94. // Variables
  95. //
  96. //------------------------------------------------------------------------------
  97. //--------------------------------------
  98. // Public
  99. //--------------------------------------
  100. //--------------------------------------
  101. // Private
  102. //--------------------------------------
  103. //------------------------------------------------------------------------------
  104. //
  105. // Properties (getters/setters)
  106. //
  107. //------------------------------------------------------------------------------
  108. //------------------------------------------------------------------------------
  109. //
  110. // Overriden methods
  111. //
  112. //------------------------------------------------------------------------------
  113. //------------------------------------------------------------------------------
  114. //
  115. // Methods
  116. //
  117. //------------------------------------------------------------------------------
  118. //--------------------------------------
  119. // Public
  120. //--------------------------------------
  121. //--------------------------------------
  122. // Private
  123. //--------------------------------------
  124. //------------------------------------------------------------------------------
  125. //
  126. // Event Handlers
  127. //
  128. //------------------------------------------------------------------------------
  129. }
  130. }