123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import { Vec2 } from "cc";
- export class MathUtil {
- public randomSeed: number = null
-
- static getRandom(min: number, max: number) {
- if (min == max) {
- return min;
- }
- return (min >= max) ? 0 : Math.floor(Math.random() * (max - min + 1)) + min;
- }
-
- static getRandomBySeed(min: number, max: number, seed: number, isFloor?: boolean) {
- max = max || 1
- min = min || 0
- seed = (seed * 9301 + 49297) % 233280
- let rnd = seed / 233280
- if (isFloor) {
- return Math.floor(min + rnd * (max - min))
- } else {
- return min + rnd * (max - min)
- }
- }
-
- static getRandomPercentage(percentage: number) {
- let random = this.getRandom(0, 100)
- if (random < percentage) {
- return true
- }
- return false
- }
-
- static getRandomBySumList(count: number, sum: number): number[] {
- var residue = sum;
- var value = 0;
- var result: Array<number> = [];
- for (let i = 0; i < count; i++) {
- value = this.getRandom(1, residue - 1);
- if (i == count - 1) {
- value = residue;
- } else {
- residue -= value;
- }
- result.push(value);
- }
- return result;
- }
-
- static getAngle(pos1: Vec2, pos2: Vec2) {
- return Math.atan2(pos1.y - pos2.y, pos1.x - pos2.x) * (180 / Math.PI);
- }
-
- static angleToRadian(angle: number) {
- return angle * Math.PI / 180;
- }
-
- static radianToAngle(radian: number) {
- return radian / Math.PI * 180;
- }
-
- static getVectorAngle(vector1: Vec2, vector2: Vec2) {
- return (Math.atan2(vector2.y, vector2.x) - Math.atan2(vector1.y, vector1.x)) * (180 / Math.PI);
- }
-
- static angleToVector(angle: number) {
- let radian = this.angleToRadian(angle);
- return new Vec2(Math.cos(radian), Math.sin(radian)).normalize();
- }
-
- static vectorToAngle(vector: Vec2) {
- let dir = vector.normalize();
- let radian = dir.signAngle(new Vec2(1, 0));
- return -this.radianToAngle(radian);
- }
- static isInteger(num: number) {
- return (num <= 0) ? false : Math.round(num) === num;
- }
-
-
-
-
- static accMul(arg1, arg2) {
- var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
- try { m += s1.split(".")[1].length } catch (e) { }
- try { m += s2.split(".")[1].length } catch (e) { }
- return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
- }
-
-
-
-
- static accAdd(arg1, arg2) {
- var r1, r2, m;
- try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
- try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
- m = Math.pow(10, Math.max(r1, r2));
- return (arg1 * m + arg2 * m) / m;
- }
-
- static accSub(arg1, arg2) {
- var r1, r2, m, n;
- try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
- try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
- m = Math.pow(10, Math.max(r1, r2));
-
-
- n = (r1 >= r2) ? r1 : r2;
- return ((arg2 * m - arg1 * m) / m).toFixed(n);
- }
- }
|