Crafty.math
一组通用(但不常用)的工具函数集合。
方法
- .abs()
- .amountOf()
- .clamp()
- .degToRad()
- .distance()
- .lerp()
- .negate()
- .radToDeg()
- .randomElementOfArray()
- .randomInt()
- .randomNumber()
- .squaredDistance()
- .withinRange()
Back to top
Crafty.math.amountOf()
public Number Crafty.math.amountOf(Number checkValue, Number minValue, Number maxValue)
- checkValue
介于最小值与最大值之间的数字。
- minValue
最小值
- maxValue
最大值
- [返回]
根据指定值在最小值与最大值之间的位置,返回0到1之间的一个数。公式:(checkValue-minValue)/(maxValue-checkValue)
如果指定值在最小值与最大值之间,则返回一个0到1之间的数值。
Back to top
Crafty.math.clamp()
public Number Crafty.math.clamp(Number value, Number min, Number max)
- value
数字
- max
最大值
- min
最小值
- [返回]
介于最小值与最大值之间的数值
将值限制在指定的范围内。(如果指定值小于最小值则返回最小值,大于最大值则返回最大值,否则返回给定值。)
Back to top
Crafty.math.distance()
public Number Crafty.math.distance(Number x1, Number y1, Number x2, Number y2)
- x1
第一点 x 坐标
- y1
第一点 y 坐标
- x2
第二点 x 坐标
- y2
第二点 y 坐标
- [返回]
两点间的距离
计算两点间的距离
Back to top
Crafty.math.lerp()
public Number Crafty.math.lerp(Number value1, Number value2, Number amount)
- value1
第一个值
- value2
第二个值
- amount
第二个值与第一个值的比率
- [返回]
线性插值结果
线性插值。如果 amount 为零则返回第一个值,如果 amount 为1则返回第二个值。(实际是 Crafty.math.amountOf 的逆运算)
Back to top
Crafty.math.negate()
public Number Crafty.math.negate(Number percent)
- percent
返回
-1
的概率
- [返回]
1 或 -1
随机返回 1
或 -1
。
Back to top
Crafty.math.radToDeg()
public Number Crafty.math.radToDeg(Number angle)
- angleInRad
弧度值
- [返回]
角度值
弧度转角度
Back to top
Crafty.math.randomElementOfArray()
public Object Crafty.math.randomElementOfArray(Array array)
- array
指定数组
- [返回]
随机返回数组中的一个元素
从指定的数组中随机选择一个元素。
Back to top
Crafty.math.randomInt()
public Number Crafty.math.randomInt(Number start, Number end)
- start
最小值
- end
最大值
- [返回]
一个介于最小值和最大值之间的随机整数
返回一个介于指定范围内的整数。
Back to top
Crafty.math.randomNumber()
public Number Crafty.math.randomNumber(Number start, Number end)
- start
最小值
- end
最大值
- [返回]
一个介于最小值和最大值之间的数值。
返回一个介于指定范围内的数值。
Back to top
Crafty.math.squaredDistance()
public Number Crafty.math.squaredDistance(Number x1, Number y1, Number x2, Number y2)
- x1
第一个点的 x 坐标
- y1
第一个点的 y 坐标
- x2
第二个点的 x 坐标
- y2
第二个点的 y 坐标
- [返回]
两点间距离的平方
计算两点间距离的平方
Back to top
Crafty.math.withinRange()
public Boolean Crafty.math.withinRange(Number value, Number min, Number max)
- value
指定值
- min
最小值
- max
最大值
- [返回]
如果指定值介于最小值与最大值之间则返回 true
检查一个值是否在指定的范围内。