/com/lele/MathTool/LeleMath.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 158 lines | 142 code | 7 blank | 9 comment | 36 complexity | 9bd12a1f67a12516c79d8866e4c02a9a MD5 | raw file
  1. package com.lele.MathTool
  2. {
  3. import flash.display.Sprite;
  4. import flash.geom.Point;
  5. /**
  6. * ...
  7. * @author Lele
  8. */
  9. public class LeleMath
  10. {
  11. public function LeleMath()
  12. {
  13. }
  14. public static function GetDistance(point1:Point, point2:Point):Number
  15. {
  16. return Math.sqrt((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y));
  17. }
  18. public static function CheckArea(num:Number, arg1:Number, arg2:Number):Boolean//计算是否超出定义域
  19. {
  20. //默认arg1大于arg2..注意,这里的陈述只对函数内部有效
  21. if (arg2 > arg1)
  22. {
  23. if (num > arg2)
  24. return false;
  25. if (num < arg1)
  26. return false;
  27. return true;
  28. }
  29. else//arg1>arg2
  30. {
  31. if (num > arg1)
  32. return false;
  33. if (num < arg2)
  34. return false;
  35. return true;
  36. }
  37. return false;//默认
  38. }
  39. public static function GetDigree(point1:Point, point2:Point):Number//返回一个角度,from->to
  40. {
  41. var arcSource:Number = (point2.x - point1.x) / Math.sqrt((point2.x - point1.x) * (point2.x - point1.x) + (point2.y - point1.y) * (point2.y - point1.y));
  42. if (point2.y < point1.y)
  43. {
  44. return 360.0 - Math.acos(arcSource) * (180.0 / Math.PI);
  45. }
  46. return Math.acos(arcSource) * (180.0 / Math.PI);
  47. }
  48. public static function DealRDifference(digree:Number):Number
  49. {
  50. return 360 - digree;
  51. }
  52. public static function GetDirectionSimple(digree:Number):String
  53. {
  54. if (digree > 360) { digree-= 360; }
  55. if (digree > 0 && digree <= 90)
  56. {
  57. return "ur";
  58. }
  59. if (digree > 90 && digree <= 180)
  60. {
  61. return "lu";
  62. }
  63. if (digree > 180 && digree <= 270)
  64. {
  65. return "dl";
  66. }
  67. if (digree > 270 && digree <= 360)
  68. {
  69. return "rd";
  70. }
  71. return "dl";
  72. }
  73. public static function GetDirection(digree:Number):String
  74. {
  75. if (digree > 360) { digree-= 360; }
  76. if (digree > 337.5 || digree <= 22.5)
  77. {
  78. return "rr";
  79. }
  80. if (digree > 22.5 && digree <= 67.5)
  81. {
  82. return "ur";
  83. }
  84. if (digree > 67.5 && digree <= 112.5)
  85. {
  86. return "uu";
  87. }
  88. if (digree > 112.5 && digree <= 157.5)
  89. {
  90. return "lu";
  91. }
  92. if (digree > 157.5 && digree <= 202.5)
  93. {
  94. return "ll";
  95. }
  96. if (digree > 202.5 && digree <= 247.5)
  97. {
  98. return "dl";
  99. }
  100. if (digree > 247.5 && digree <= 292.5)
  101. {
  102. return "dd";
  103. }
  104. if (digree > 292.5 && digree <= 337.5)
  105. {
  106. return "rd";
  107. }
  108. return "dd";
  109. }
  110. public static function NextGaussian2(a:Number, b:Number):Number //a为顶点,b为扁度
  111. {
  112. var r1:Number = Math.random();
  113. var r2:Number = Math.random();
  114. var u:Number = Math.sqrt((-2) * Math.log(r1)) * Math.cos(2 * Math.PI * r2);
  115. var z:Number = a + u * Math.sqrt(b); return (z);
  116. }
  117. public static function ScaleWithRange(source:Sprite, rangeX:Number, rangeY:Number):Sprite
  118. {
  119. var scalFac:Number = 1;
  120. if (source.width < rangeX && source.height < rangeY)
  121. {
  122. if ((rangeX - source.width) > (rangeY - source.height))
  123. {
  124. //on y
  125. scalFac = rangeY / source.height;
  126. }
  127. else
  128. {
  129. //on x
  130. scalFac = rangeX / source.width;
  131. }
  132. }
  133. else
  134. {
  135. if ((source.width-rangeX)>(source.height-rangeY))
  136. {
  137. //on x
  138. scalFac = rangeX / source.width;
  139. }
  140. else
  141. {
  142. //on y
  143. scalFac = rangeY / source.height;
  144. }
  145. }
  146. source.width = source.width * scalFac;
  147. source.height = source.height * scalFac;
  148. return source;
  149. }
  150. }
  151. }