1 | (function(global, factory) {
|
2 | typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() :
|
3 | typeof define === "function" && define.amd ? define(factory) :
|
4 | (global.extendedMath = factory());
|
5 | } (this, function() {
|
6 |
|
7 | "use strict";
|
8 |
|
9 | if(typeof require !== "undefined") {
|
10 | if(typeof utilities === "undefined") {
|
11 | global.utilities = require("extra-utilities");
|
12 | }
|
13 | }
|
14 |
|
15 | var extendedMath = { };
|
16 |
|
17 | extendedMath.HalfPI = 1.57079632679489661923;
|
18 | extendedMath.QuarterPI = 0.78539816339744830962;
|
19 | extendedMath.TwoPI = 6.28318530717958647693;
|
20 |
|
21 | extendedMath.clamp = function(value, min, max) {
|
22 | return utilities.isInvalidNumber(value) || utilities.isInvalidNumber(min) || utilities.isInvalidNumber(max) ? NaN : value < min ? min : value > max ? max : value;
|
23 | };
|
24 |
|
25 | extendedMath.distance = function(a, b) {
|
26 | return utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) ? NaN : Math.abs(b - a);
|
27 | };
|
28 |
|
29 | extendedMath.radiansToDegrees = function(value) {
|
30 | return utilities.isInvalidNumber(value) ? NaN : value * (180 / Math.PI);
|
31 | };
|
32 |
|
33 | extendedMath.degreesToRadians = function(value) {
|
34 | return utilities.isInvalidNumber(value) ? NaN : value * (Math.PI / 180);
|
35 | };
|
36 |
|
37 | extendedMath.compareAnglesDegrees = function(a, b) {
|
38 | if(utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b)) {
|
39 | return NaN;
|
40 | }
|
41 |
|
42 | if(a == b) {
|
43 | return 0;
|
44 | }
|
45 |
|
46 | var c = a % 360;
|
47 | var d = b % 360;
|
48 |
|
49 | if(c < 0) {
|
50 | c += 360;
|
51 | }
|
52 |
|
53 | if(d < 0) {
|
54 | d += 360;
|
55 | }
|
56 |
|
57 | if(c === d) {
|
58 | return 0;
|
59 | }
|
60 |
|
61 | return Math.cos(extendedMath.degreesToRadians(a - b) + (Math.PI / 2)) < 0 ? -1 : 1;
|
62 | };
|
63 |
|
64 | extendedMath.compareAnglesRadians = function(a, b) {
|
65 | return utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) ? NaN : extendedMath.compareAnglesDegrees(extendedMath.radiansToDegrees(a), extendedMath.radiansToDegrees(b));
|
66 | };
|
67 |
|
68 | extendedMath.lerp = function(a, b, amount) {
|
69 | if(utilities.isInvalidNumber(a) || utilities.isInvalidNumber(b) || utilities.isInvalidNumber(amount)) {
|
70 | return NaN;
|
71 | }
|
72 |
|
73 | if(amount === 0) {
|
74 | return a;
|
75 | }
|
76 | else if(amount === 1) {
|
77 | return b;
|
78 | }
|
79 |
|
80 | return a + (b - a) * amount;
|
81 | };
|
82 |
|
83 | extendedMath.normalize = function(value, min, max) {
|
84 | return utilities.isInvalidNumber(value) || utilities.isInvalidNumber(min) || utilities.isInvalidNumber(max) ? NaN : (value - min) / (max - min);
|
85 | };
|
86 |
|
87 | return extendedMath;
|
88 |
|
89 | }));
|