-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgs-utils-math.js
More file actions
263 lines (229 loc) · 7.67 KB
/
Copy pathgs-utils-math.js
File metadata and controls
263 lines (229 loc) · 7.67 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"use strict";
const GSUmathPrec = n => +n.toPrecision( 15 );
// .............................................................................
function GSUmathMod( n, m ) { return ( ( n % m ) + m ) % m; }
function GSUmathLogN( x, y ) { return Math.log( y ) / Math.log( x ); }
function GSUmathEaseInCirc( n, pow = 2 ) { return 1 - Math.sqrt( 1 - n ** pow ); }
function GSUmathEaseOutCirc( n, pow = 2 ) { return Math.sqrt( 1 - ( n - 1 ) ** pow ); }
// .............................................................................
function GSUmathRound( val, step = 1 ) { return GSUmathPrec( Math.round( val / step ) * step ); }
function GSUmathFloor( val, step = 1 ) { return GSUmathPrec( Math.floor( val / step ) * step ); }
function GSUmathCeil( val, step = 1 ) { return GSUmathPrec( Math.ceil( val / step ) * step ); }
function GSUmathFix( val, dec = 0 ) {
return GSUisNum( val )
? +val.toFixed( dec )
: val?.map?.( n => +n.toFixed( dec ) ) || +val || 0;
}
// .............................................................................
function GSUmathSum( ...arr ) { return arr.reduce( ( sum, n ) => sum + +n, 0 ) || 0; }
function GSUmathAvg( ...arr ) { return GSUmathSum( ...arr ) / arr.length || 0; }
function GSUmathStack( arr, x ) {
arr.pop();
arr.unshift( x );
return arr;
}
// .............................................................................
function GSUmathSign( n ) { return n >= 0 ? `+${ n }` : `${ n }`; }
// .............................................................................
function GSUmathInRange( n, min, max ) {
if ( n === "" || ( !GSUisStr( n ) && !GSUisNum( n ) ) ) {
return false;
}
return min < max
? min <= n && n <= max
: max <= n && n <= min;
}
function GSUmathApprox( n, x, diff ) { return GSUmathInRange( n, x - diff, x + diff ); }
function GSUmathClamp( n, min, max ) {
return min < max
? Math.max( min, Math.min( n || 0, max ) )
: Math.max( max, Math.min( n || 0, min ) );
}
// .............................................................................
function GSUmathFloatReadable( n ) {
if ( !n || !GSUisNum( n ) ) {
return [ 0, "" ];
}
const abs = Math.abs( n );
const uni = GSUmathFloatReadable.$units;
const ind = uni.findIndex( u => abs >= u[ 0 ] );
if ( ind === -1 ) {
return [ n, "" ];
}
let unit = uni[ ind ];
let rounded = Math.round( ( n / unit[ 0 ] ) * 1000 ) / 1000;
if ( Math.abs( rounded ) >= 1000 && ind > 0 ) {
unit = uni[ ind - 1 ];
rounded = Math.round( ( n / unit[ 0 ] ) * 1000 ) / 1000;
}
return [ rounded, unit[ 1 ] ];
}
GSUmathFloatReadable.$units = [
[ 1e12, "T" ],
[ 1e9, "G" ],
[ 1e6, "M" ],
[ 1e3, "K" ],
];
// .............................................................................
function GSUmathWaveSine( len ) {
return GSUnewArray( len, i => Math.sin( ( i / ( len - 1 ) ) * Math.PI * 2 ) );
}
function GSUmathWaveSquare( len ) {
return GSUnewArray( len, i => i < len / 2 ? 1 : -1 );
}
function GSUmathWaveSawtooth( len ) {
const len2 = ( len - 1 ) * .50;
return GSUnewArray( len, i => i < len2 ? i / len2 : -1 + ( i - len2 ) / len2 );
}
function GSUmathWaveTriangle( len ) {
const len25 = ( len - 1 ) * .25;
const len75 = ( len - 1 ) * .75;
return GSUnewArray( len, i =>
i < len25 ? i / len25 :
i < len75
? +1 - ( i - len25 ) / len25
: -1 + ( i - len75 ) / len25
);
}
const GSUmathWaveFns = {
sine: GSUmathWaveSine,
square: GSUmathWaveSquare,
sawtooth: GSUmathWaveSawtooth,
triangle: GSUmathWaveTriangle,
};
// .............................................................................
function GSUmathRealImagToXY_sub( a, b, t ) {
return a.reduce( ( val, ai, i ) => {
const tmp = Math.PI * 2 * i * t;
return val + ai * Math.cos( tmp ) + b[ i ] * Math.sin( tmp );
}, 0 );
}
function GSUmathRealImagToXY( real, imag, width ) {
const arr = [];
const fn = GSUmathRealImagToXY_sub.bind( null, real, imag );
for ( let x = 0; x < width; ++x ) {
arr.push( fn( x / width ) );
}
return arr;
}
// .............................................................................
function GSUmathLineFindY_sub( ptA, ptB, x ) {
const w = ptB.x - ptA.x;
const h = ptB.y - ptA.y;
const xx = x - ptA.x;
return xx / w * h + ptA.y;
}
function GSUmathLineFindY( ptA, ptB, x ) {
return ptA.x < ptB.x
? GSUmathLineFindY_sub( ptA, ptB, x )
: GSUmathLineFindY_sub( ptB, ptA, x );
}
// .............................................................................
function GSUmathSampleDotLine( dots, nb, xstart, xend ) {
const dataDots = Object.values( dots ).sort( ( a, b ) => a.x - b.x );
const dataFloat = GSUnewArray( nb, 0 );
if ( dataDots.length > 1 ) {
const xa = xstart ?? dataDots[ 0 ].x;
const xb = xend ?? dataDots.at( -1 ).x;
const stepX = ( xb - xa ) / ( nb - 1 );
let currX = xa;
let fn = null;
let dot = null;
let type = null;
let prevDot = null;
let dotI = 0;
let dotW = 0;
let dotH = 0;
let i2 = 0;
for ( let i = 0; i < nb; ++i ) {
while ( dotI < dataDots.length - 1 && ( !dotI || currX >= dataDots[ dotI ].x ) ) {
i2 = 0;
prevDot = dataDots[ dotI ];
dot = dataDots[ ++dotI ];
type = dot.type in GSUmathSampleDotLine.$fns ? dot.type : "line";
dotW = dot.x - prevDot.x;
dotH = dot.y - prevDot.y;
fn = GSUmathSampleDotLine.$fns[ type ].bind( null, GSUmathSampleDotLine.$calcDotVal( type, dot.val ) );
}
const p = GSUmathClamp( ( currX - prevDot.x ) / dotW, 0, 1 );
const y = GSUmathClamp( fn( p, i2++ ), 0, 1 );
dataFloat[ i ] = [ currX, prevDot.y + dotH * y ];
currX += stepX;
}
}
return dataFloat;
}
GSUmathSampleDotLine.$calcDotVal = ( type, val ) => {
const dotVal = !type || type.endsWith( "urve" ) ? val : Math.round( val );
const dotVal2 = !dotVal && (
type === "stair" ||
type === "sineWave" ||
type === "triangleWave" ||
type === "squareWave"
) ? 1 : dotVal;
return dotVal2;
};
GSUmathSampleDotLine.$fns = Object.freeze( {
hold: () => 0,
line: ( _, p ) => p,
curve( val, p ) {
return val > 0
? 1 - ( ( 1 - p ) ** ( val + 1 ) )
: p ** -( val - 1 );
},
doubleCurve( val, p ) {
return p > .5
? GSUmathSampleDotLine.$fns.curve( val, ( p - .5 ) * 2 ) / 2 + .5
: GSUmathSampleDotLine.$fns.curve( -val, p * 2 ) / 2;
},
stair( val, p, i ) {
const nbStairsAbs = Math.abs( val );
const inv = val > 0;
const y = ( Math.floor( p / ( 1 / ( nbStairsAbs + inv ) ) ) + !inv ) * ( 1 / ( nbStairsAbs + !inv ) );
return i ? y : 0;
},
sineWave( val, p ) {
const val2 = Math.abs( val );
const val3 = val2 * ( ( val2 - .5 ) / val2 );
const p2 = val < 0 ? p + 1 : p;
return .5 + Math.sin( Math.PI * 1.5 + p2 * val3 * Math.PI * 2 ) / 2;
},
triangleWave( val, p ) {
const val2 = Math.abs( val );
const val3 = val2 * ( ( val2 - .5 ) / val2 );
const p2 = val < 0 ? p + 1 : p;
return -Math.abs( 2 * val3 * p2 % 2 - 1 ) + 1;
},
squareWave( val, p ) {
const val2 = Math.floor( p / ( 1 / ( val * 2 ) ) ) % 2 === 0;
return val2 && p < 1 ? 0 : 1;
},
} );
// .............................................................................
function GSUmathDotLineGetYFromDot( type, val, p ) {
const val2 = GSUmathSampleDotLine.$calcDotVal( type, val );
return GSUmathClamp( GSUmathSampleDotLine.$fns[ type || "curve" ]( val2, p, 1 ), 0, 1 );
}
function GSUmathDotLineGetYFromX( dots, x ) {
let a = null;
let b = null;
GSUforEach( dots, d => {
if ( GSUmathInRange( d.x, a?.x ?? -Infinity, x ) ) {
a = d;
}
} );
GSUforEach( dots, d => {
if ( GSUmathInRange( d.x, x, b?.x ?? Infinity ) ) {
b = d;
}
} );
if ( a && b ) {
const p = GSUmathClamp( ( x - a.x ) / ( b.x - a.x ), 0, 1 );
const y = GSUmathDotLineGetYFromDot( b.type, b.val, p );
const y2 = a.y < b.y ? y : 1 - y;
const yStart = Math.min( a.y, b.y );
const h = Math.abs( a.y - b.y );
return yStart + y2 * h;
}
return 0;
}