Skip to content

Instantly share code, notes, and snippets.

@aprilandjan
Created April 1, 2017 13:01
Show Gist options
  • Save aprilandjan/2a85ad6a2b54a3b461117335522f0409 to your computer and use it in GitHub Desktop.
Save aprilandjan/2a85ad6a2b54a3b461117335522f0409 to your computer and use it in GitHub Desktop.
bunch of utility methods about Math
/**
* ȡ������֮��������
* @param a
* @param b
* @returns {*}
*/
export function randomNum (a, b) {
return Math.random() * (b - a) + a
}
/**
* ȡ������֮����������
* @param a
* @param b
*/
export function randomInt (a, b) {
var max = Math.max(a, b) + 1
var min = Math.min(a, b)
return Math.floor(this.randomNum(min, max))
}
/**
* ��val��ֵ��������[inputMin��inputMax]�еı��� ��ӳ��[outputMin, outputMax]��Χ��
* @param val
* @param inputMin
* @param inputMax
* @param outputMin
* @param outputMax
* @returns {*}
*/
export function mapping (val, inputMin, inputMax, outputMin, outputMax) {
return ((outputMax - outputMin) * ((val - inputMin) / (inputMax - inputMin))) + outputMin
}
/**
* ��������֮��ľ���
* @param ax
* @param ay
* @param bx
* @param by
* @returns {number}
*/
export function distance (ax, ay, bx = 0, by = 0) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2))
}
/**
* �ж�һ����ֵ�Ƿ������䷶Χ��
* @param val
* @param a
* @param b
* @returns {boolean}
*/
export function inRange (val, a, b) {
var max = Math.max(a, b)
var min = Math.min(a, b)
return val >= min && val <= max
}
/**
* �������C(a, b)
* @param from
* @param within
* @returns {number}
*/
export function combination (from, within) {
return arrangement(from, within) / arrangement(within, within)
}
/**
* ��������A(a, b)
* @param from
* @param within
* @returns {number}
*/
export function arrangement (from, within) {
var result = 1
for (var i = 0; i < within; i++) {
result *= (from - i)
}
return result
}
/**
* 限制某个数的大小在一定范围内
* @param val
* @param a
* @param b
* @returns {number}
*/
export function limit (val, a, b) {
var max = Math.max(a, b)
var min = Math.min(a, b)
if (val > max) {
val = max
}
if (val < min) {
val = min
}
return val
}
// =============
// 旋转相关的
// =============
/**
* 让一个任意弧度值回归(-PI~PI)
* @param val
* @returns {number}
*/
export function formatMathRad (val) {
while (val > Math.PI) {
val -= 2 * Math.PI
}
while (val < -Math.PI) {
val += 2 * Math.PI
}
return val
}
/**
* 让一个任意角度值回归(-180~180)
* @param val
*/
export function formatMathDeg (val) {
while (val > 180) {
val -= 360
}
while (val < -180) {
val += 360
}
return val
}
/**
* 让一个任意的旋转弧回归(0~2PI)
* @param val
*/
export function formatRotationRad (val) {
while (val >= 2 * Math.PI) {
val -= 2 * Math.PI
}
while (val < 0) {
val += 2 * Math.PI
}
return val
}
/**
* 让一个任意角度值回归(0, 360)
* @param val
*/
export function formatRotationDeg (val) {
while (val >= 360) {
val -= 360
}
while (val < 0) {
val += 360
}
return val
}
/**
* 判断圆域内的 一个角度是否在一个弧度区间内
* Todo: any better idea?
* @param r 弧度
* @param from 弧度
* @param to 弧度
*/
export function inRadRange (r, from, to) {
var result = true
r = this.formatMathRad(r)
from = this.formatMathRad(from)
to = this.formatMathRad(to)
if (from <= to) { // 处于-PI~PI的度数下
result = r >= from && r <= to
} else {
result = (r >= from && r <= Math.PI) || (r <= to && r >= -Math.PI)
}
return result
}
/**
* 根据提供的控制点序列,计算贝塞尔曲线值
* @param t
* @param pts
*/
export function getBezierValue (t, pts) {
if (t > 1) {
t = 1
}
// 阶数
var order = pts.length - 1
// 计算出的结果坐标
var p = {x: 0, y: 0}
var rt = 1 - t
for (var i = 0; i <= order; i++) {
var factor = this.combination(order, i) * Math.pow(rt, order - i) * Math.pow(t, i)
p.x += factor * pts[i].x
p.y += factor * pts[i].y
}
return p
}
/**
* 根据提供的概率分布数组,随机出一个序号(从0开始)
*/
export function randomProbability (pArr) {
var r = Math.random()
var sum = 0
var index = 0
for (var i = 0; i < pArr.length; i++) {
sum += pArr[i]
if (r > sum) {
index++
} else {
break
}
}
return index
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment