AS3 MathUtils静态类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3 MathUtils静态类相关的知识,希望对你有一定的参考价值。

Credit goes to Bruno Imbrizi.
  1. package com.brunoimbrizi.utils {
  2.  
  3. /**
  4. * Class MathUtils
  5. *
  6. * @author Bruno Imbrizi
  7. */
  8.  
  9. public class MathUtils {
  10.  
  11.  
  12. /**
  13. * Returns a number constrained between min and max.
  14. * @param num
  15. * @param min
  16. * @param max
  17. * @return
  18. */
  19. public static function constrain(num:Number, min:Number = 0, max:Number = 1):Number
  20. {
  21. if (num < min) return min;
  22. if (num > max) return max;
  23. return num;
  24. }
  25.  
  26.  
  27. /**
  28. * Returns a random number between min and max.
  29. *
  30. * @param min
  31. * @param max
  32. * @return
  33. */
  34. public static function randomRange(min:Number, max:Number, round:Boolean = false):Number
  35. {
  36. var num:Number = (min + Math.random() * (max - min));
  37.  
  38. if (round) return Math.round(num);
  39. return num;
  40. }
  41.  
  42.  
  43. /**
  44. * Returns the percentage of a number in a given range.
  45. * Example: num = 15 range 10 to 20 // outputs 0.5
  46. *
  47. * @param num
  48. * @param min
  49. * @param max
  50. * @param constrainMin Returns 0 if num < min.
  51. * @param constrainMax Returns 1 if num > max.
  52. * @return
  53. */
  54. public static function rangeToPercent(num:Number, min:Number, max:Number, constrainMin:Boolean = false, constrainMax:Boolean = false):Number
  55. {
  56. if (constrainMin && num < min) return 0;
  57. if (constrainMax && num > max) return 1;
  58. return (num - min) / (max - min);
  59. }
  60.  
  61.  
  62. /**
  63. * Returns the number that corresponds to the percentage in a given range.
  64. * Example: percent = 0.5 range 10 to 20 // outputs 15
  65. *
  66. * @param percent
  67. * @param min
  68. * @param max
  69. * @return
  70. */
  71. public static function percentToRange(percent:Number, min:Number, max:Number):Number
  72. {
  73. return (percent * (max - min)) + min;
  74. }
  75.  
  76.  
  77. /**
  78. * Re-maps a number from one range to another. The output is the same as inputing the result of rangeToPercent() into percentToRange().
  79. * Example: num = 10, min1 = 0, max1 = 100, min2 = 0, max2 = 50 // outputs 5
  80. * @param num
  81. * @param min1
  82. * @param max1
  83. * @param min2
  84. * @param max2
  85. * @return
  86. */
  87. public static function map(num:Number, min1:Number, max1:Number, min2:Number, max2:Number, round:Boolean = true, constrainMin:Boolean = true, constrainMax:Boolean = true):Number
  88. {
  89. if (constrainMin && num < min1) return min2;
  90. if (constrainMax && num > max1) return max2;
  91.  
  92. var num1:Number = (num - min1) / (max1 - min1);
  93. var num2:Number = (num1 * (max2 - min2)) + min2;
  94. if (round) return Math.round(num2);
  95. return num2;
  96. }
  97.  
  98.  
  99. /**
  100. * Converts radians to degrees.
  101. *
  102. * @param radians
  103. * @return
  104. */
  105. public static function radiansToDegrees(radians:Number):Number
  106. {
  107. return (radians * 180 / Math.PI);
  108. }
  109.  
  110.  
  111. /**
  112. * Converts degrees to radians.
  113. *
  114. * @param degrees
  115. * @return
  116. */
  117. public static function degreesToRadians(degrees:Number):Number
  118. {
  119. return (degrees * Math.PI / 180);
  120. }
  121.  
  122.  
  123. /**
  124. * Returns 1 if the value is >= 0. Returns -1 if the value is < 0.
  125. *
  126. * @param num
  127. * @return
  128. */
  129. public static function sign(num:Number):int {
  130. if (num < 0) return -1
  131. return 1;
  132. }
  133. }
  134.  
  135.  
  136. }

以上是关于AS3 MathUtils静态类的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3 DrawPixelBorderAroundStage静态类

片段内部静态类和gradle问题

使用字符串的 AS3 目标控制器类变量

片段 null 必须是公共静态类才能从实例状态正确重新创建

[AS3]在反射中使用静态方法

在android studio中升级repo v9后,片段必须是公共静态类崩溃错误