-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMath.js
More file actions
137 lines (95 loc) · 2.66 KB
/
Math.js
File metadata and controls
137 lines (95 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
///// 2975ff
//////////////////////////////////////////////////////////////
// MATH
/*
Math is Javascript is a literal object. No constructor, but initialized.
Adding method with Math.method rather than Math.prototype.method
*/
/*
static public final float sq(float a) {
return a*a;
}
*/
Math.sq = function(a) {
return a*a;
}
/*
static public final int constrain(int amt, int low, int high) {
return (amt < low) ? low : ((amt > high) ? high : amt);
}
*/
Math.constrain = function(amt, low, high) {
return (amt < low) ? low : ((amt > high) ? high : amt);
}
/*
static public final float degrees(float radians) {
return radians * RAD_TO_DEG;
}
static final float DEG_TO_RAD = PI/180.0f;
static final float RAD_TO_DEG = 180.0f/PI;
*/
Math.degrees = function(radians) {
return radians * (180/Math.PI);
}
Math.radians = function(degrees) {
return degrees*(Math.PI/180);
}
/*
static public final float mag(float a, float b) {
return (float)Math.sqrt(a*a + b*b);
}
*/
Math.mag = function(a, b) {
return Math.sqrt(a*a + b*b);
}
/*
static public final float dist(float x1, float y1, float x2, float y2) {
return sqrt(sq(x2-x1) + sq(y2-y1));
}
*/
Math.dist = function(x1, y1, x2, y2) {
return Math.sqrt(Math.sq(x2-x1) + Math.sq(y2-y1));
}
/*
static public final float lerp(float start, float stop, float amt) {
return start + (stop-start) * amt;
}
*/
Math.lerp = function(start, stop, amt) {
return start + (stop-start) * amt;
}
/**
* Normalize a value to exist between 0 and 1 (inclusive).
* Mathematically the opposite of lerp(), figures out what proportion
* a particular value is relative to start and stop coordinates.
*/
/*
static public final float norm(float value, float start, float stop) {
return (value - start) / (stop - start);
}
*/
Math.norm = function(value, start, stop) {
return (value - start) / (stop - start);
}
/**
* Convenience function to map a variable from one coordinate space
* to another. Equivalent to unlerp() followed by lerp().
*/
/*
static public final float map(float value,
float istart, float istop,
float ostart, float ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
*/
Math.map = function (value, istart, istop, ostart, ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
/*
static public final double map(double value,
double istart, double istop,
double ostart, double ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
*/
/////////////////////////////////////////////////////////////