Skip to content

Commit 5150843

Browse files
committed
- gtx Easing,
- String.parseFloat, String.parseDouble
1 parent 8f566ae commit 5150843

File tree

6 files changed

+441
-3
lines changed

6 files changed

+441
-3
lines changed

src/main/kotlin/glm_/func/common/func_vector4_common.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ interface func_vector4_common {
304304
}
305305

306306

307-
307+
// TODO openBounded closeBounded
308308

309309

310310
fun min(a: Vec4, b: Float) = min(a, b, Vec4())

src/main/kotlin/glm_/glm.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ object glm :
7373

7474
gtcMatrixInverse,
7575

76+
gtxEasing,
7677
gtxEulerAngles,
7778
gtxFastTrigonometry,
7879
gtxInteger,
Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
package glm_.gtx
2+
3+
import glm_.glm
4+
import kotlin.math.cos
5+
import kotlin.math.sin
6+
import kotlin.math.sqrt
7+
8+
interface gtxEasing {
9+
10+
/** Modelled after the line y = x
11+
* @see gtx_easing */
12+
fun linearInterpolation(a: Float): Float {
13+
// Only defined in [0, 1]
14+
assert(a in 0f..1f)
15+
16+
return a
17+
}
18+
19+
/** Modelled after the parabola y = x^2
20+
* @see gtx_easing */
21+
fun quadraticEaseIn(a: Float): Float {
22+
// Only defined in [0, 1]
23+
assert(a in 0f..1f)
24+
25+
return a * a
26+
}
27+
28+
/** Modelled after the parabola y = -x^2 + 2x
29+
* @see gtx_easing */
30+
fun quadraticEaseOut(a: Float): Float {
31+
// Only defined in [0, 1]
32+
assert(a in 0f..1f)
33+
34+
return -(a * (a - 2f))
35+
}
36+
37+
/** Modelled after the piecewise quadratic
38+
* y = (1/2)((2x)^2) ; [0, 0.5)
39+
* y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
40+
* @see gtx_easing */
41+
fun quadraticEaseInOut(a: Float): Float {
42+
// Only defined in [0, 1]
43+
assert(a in 0f..1f)
44+
45+
return when {
46+
a < 0.5f -> 2f * a * a
47+
else -> -2f * a * a + 4 * a - 1f
48+
}
49+
}
50+
51+
/** Modelled after the cubic y = x^3 */
52+
fun cubicEaseIn(a: Float): Float {
53+
// Only defined in [0, 1]
54+
assert(a in 0f..1f)
55+
56+
return a * a * a
57+
}
58+
59+
/** Modelled after the cubic y = (x - 1)^3 + 1
60+
* @see gtx_easing */
61+
fun cubicEaseOut(a: Float): Float {
62+
// Only defined in [0, 1]
63+
assert(a in 0f..1f)
64+
65+
val f = a - 1f
66+
return f * f * f + 1f
67+
}
68+
69+
/** Modelled after the piecewise cubic
70+
* y = (1/2)((2x)^3) ; [0, 0.5)
71+
* y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
72+
* @see gtx_easing */
73+
fun cubicEaseInOut(a: Float): Float {
74+
// Only defined in [0, 1]
75+
assert(a in 0f..1f)
76+
77+
return when {
78+
a < 0.5f -> 4f * a * a * a
79+
else -> {
80+
val f = 2f * a - 2f
81+
0.5f * f * f * f + 1f
82+
}
83+
}
84+
}
85+
86+
/** Modelled after the quartic x^4
87+
* @see gtx_easing */
88+
fun quarticEaseIn(a: Float): Float {
89+
// Only defined in [0, 1]
90+
assert(a in 0f..1f)
91+
92+
return a * a * a * a
93+
}
94+
95+
/** Modelled after the quartic y = 1 - (x - 1)^4
96+
* @see gtx_easing */
97+
fun quarticEaseOut(a: Float): Float {
98+
// Only defined in [0, 1]
99+
assert(a in 0f..1f)
100+
101+
val f = a - 1f
102+
return f * f * f * (1f - a) + 1f
103+
}
104+
105+
/** Modelled after the piecewise quartic
106+
* y = (1/2)((2x)^4) ; [0, 0.5)
107+
* y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
108+
* @see gtx_easing */
109+
fun quarticEaseInOut(a: Float): Float {
110+
// Only defined in [0, 1]
111+
assert(a in 0f..1f)
112+
113+
return when {
114+
a < 0.5f -> 8f * a * a * a * a
115+
else -> {
116+
val f = a - 1f
117+
-8f * f * f * f * f + 1f
118+
}
119+
}
120+
}
121+
122+
/** Modelled after the quintic y = x^5
123+
* @see gtx_easing */
124+
fun quinticEaseIn(a: Float): Float {
125+
// Only defined in [0, 1]
126+
assert(a in 0f..1f)
127+
128+
return a * a * a * a * a
129+
}
130+
131+
/** Modelled after the quintic y = (x - 1)^5 + 1
132+
* @see gtx_easing */
133+
fun quinticEaseOut(a: Float): Float {
134+
// Only defined in [0, 1]
135+
assert(a in 0f..1f)
136+
137+
val f = a - 1f
138+
return f * f * f * f * f + 1f
139+
}
140+
141+
/** Modelled after the piecewise quintic
142+
* y = (1/2)((2x)^5) ; [0, 0.5)
143+
* y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
144+
* @see gtx_easing */
145+
fun quinticEaseInOut(a: Float): Float {
146+
// Only defined in [0, 1]
147+
assert(a in 0f..1f)
148+
149+
return when {
150+
a < 0.5f -> 16f * a * a * a * a * a
151+
else -> {
152+
val f = 2f * a - 2f
153+
0.5f * f * f * f * f * f + 1f
154+
}
155+
}
156+
}
157+
158+
/** Modelled after quarter-cycle of sine wave
159+
* @see gtx_easing */
160+
fun sineEaseIn(a: Float): Float {
161+
// Only defined in [0, 1]
162+
assert(a in 0f..1f)
163+
164+
return sin((a - 1f) * glm.HPIf) + 1f
165+
}
166+
167+
/** Modelled after quarter-cycle of sine wave (different phase)
168+
* @see gtx_easing */
169+
fun sineEaseOut(a: Float): Float {
170+
// Only defined in [0, 1]
171+
assert(a in 0f..1f)
172+
173+
return sin(a * glm.HPIf)
174+
}
175+
176+
/** Modelled after half sine wave
177+
* @see gtx_easing */
178+
fun sineEaseInOut(a: Float): Float {
179+
// Only defined in [0, 1]
180+
assert(a in 0f..1f)
181+
182+
return 0.5f * (1f - cos(a * glm.PIf))
183+
}
184+
185+
/** Modelled after shifted quadrant IV of unit circle
186+
* @see gtx_easing */
187+
fun circularEaseIn(a: Float): Float {
188+
// Only defined in [0, 1]
189+
assert(a in 0f..1f)
190+
191+
return 1f - sqrt(1f - a * a)
192+
}
193+
194+
/** Modelled after shifted quadrant II of unit circle
195+
* @see gtx_easing */
196+
fun circularEaseOut(a: Float): Float {
197+
// Only defined in [0, 1]
198+
assert(a in 0f..1f)
199+
200+
return sqrt((2f - a) * a)
201+
}
202+
203+
/** Modelled after the piecewise circular function
204+
* y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
205+
* y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
206+
* @see gtx_easing */
207+
fun circularEaseInOut(a: Float): Float {
208+
// Only defined in [0, 1]
209+
assert(a in 0f..1f)
210+
211+
return when {
212+
a < 0.5f -> 0.5f * (1f - sqrt(1f - 4f * (a * a)))
213+
else -> 0.5f * (sqrt(-(2f * a - 3f) * (2f * a - 1f)) + 1f)
214+
}
215+
}
216+
217+
/** Modelled after the exponential function y = 2^(10(x - 1))
218+
* @see gtx_easing */
219+
fun exponentialEaseIn(a: Float): Float {
220+
// Only defined in [0, 1]
221+
assert(a in 0f..1f)
222+
223+
return when {
224+
a <= 0f -> a
225+
else -> {
226+
val complementary = a - 1f
227+
val two = 2f
228+
229+
glm.pow(two, complementary * 10f)
230+
}
231+
}
232+
}
233+
234+
/** Modelled after the exponential function y = -2^(-10x) + 1
235+
* @see gtx_easing */
236+
fun exponentialEaseOut(a: Float): Float {
237+
// Only defined in [0, 1]
238+
assert(a in 0f..1f)
239+
240+
return when {
241+
a >= 1f -> a
242+
else -> 1f - glm.pow(2f, -10f * a)
243+
}
244+
}
245+
246+
/** Modelled after the piecewise exponential
247+
* y = (1/2)2^(10(2x - 1)) ; [0,0.5)
248+
* y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
249+
* @see gtx_easing */
250+
fun exponentialEaseInOut(a: Float): Float {
251+
// Only defined in [0, 1]
252+
assert(a in 0f..1f)
253+
254+
return when {
255+
a < 0.5f -> 0.5f * glm.pow(2f, 20f * a - 10f)
256+
else -> -0.5f * glm.pow(2f, -20f * a + 10f) + 1f
257+
}
258+
}
259+
260+
/** Modelled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
261+
* @see gtx_easing */
262+
fun elasticEaseIn(a: Float): Float {
263+
// Only defined in [0, 1]
264+
assert(a in 0f..1f)
265+
266+
return sin(13f * glm.HPIf * a) * glm.pow(2f, 10f * (a - 1f))
267+
}
268+
269+
/** Modelled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1
270+
* @see gtx_easing */
271+
fun elasticEaseOut(a: Float): Float {
272+
// Only defined in [0, 1]
273+
assert(a in 0f..1f)
274+
275+
return sin(-13f * glm.HPIf * (a + 1f)) * glm.pow(2f, -10f * a) + 1f
276+
}
277+
278+
/** Modelled after the piecewise exponentially-damped sine wave:
279+
* y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
280+
* y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
281+
* @see gtx_easing */
282+
fun elasticEaseInOut(a: Float): Float {
283+
// Only defined in [0, 1]
284+
assert(a in 0f..1f)
285+
286+
return when {
287+
a < 0.5f -> 0.5f * sin(13f * glm.HPIf * (2f * a)) * glm.pow(2f, 10f * (2f * a - 1f))
288+
else -> 0.5f * (sin(-13f * glm.HPIf * (2f * a)) * glm.pow(2f, -10f * (2f * a - 1f)) + 2f)
289+
}
290+
}
291+
292+
/** @see gtx_easing */
293+
fun backEaseIn(a: Float): Float = backEaseIn(a, 1.70158f)
294+
295+
/** @see gtx_easing */
296+
fun backEaseOut(a: Float): Float = backEaseOut(a, 1.70158f)
297+
298+
/** @see gtx_easing */
299+
fun backEaseInOut(a: Float): Float = backEaseInOut(a, 1.70158f)
300+
301+
/** @param a parameter
302+
* @param o Optional overshoot modifier
303+
* @see gtx_easing */
304+
fun backEaseIn(a: Float, o: Float): Float {
305+
// Only defined in [0, 1]
306+
assert(a in 0f..1f)
307+
308+
val z = ((o + 1f) * a) - o
309+
return a * a * z
310+
}
311+
312+
/** @param a parameter
313+
* @param o Optional overshoot modifier
314+
* @see gtx_easing */
315+
fun backEaseOut(a: Float, o: Float): Float {
316+
// Only defined in [0, 1]
317+
assert(a in 0f..1f)
318+
319+
val n = a - 1f
320+
val z = ((o + 1f) * n) + o
321+
return n * n * z + 1f
322+
}
323+
324+
/** @param a parameter
325+
* @param o Optional overshoot modifier
326+
* @see gtx_easing */
327+
fun backEaseInOut(a: Float, o: Float): Float {
328+
// Only defined in [0, 1]
329+
assert(a in 0f..1f)
330+
331+
val s = o * 1.525f
332+
val x = 0.5f
333+
var n = a / 0.5f
334+
335+
return when {
336+
n < 1f -> {
337+
val z = ((s + 1f) * n) - s
338+
val m = n * n * z
339+
x * m
340+
}
341+
else -> {
342+
n -= 2
343+
val z = (s + 1f) * n + s
344+
val m = n * n * z + 2f
345+
x * m
346+
}
347+
}
348+
}
349+
350+
/** @see gtx_easing */
351+
fun bounceEaseIn(a: Float): Float {
352+
// Only defined in [0, 1]
353+
assert(a in 0f..1f)
354+
355+
return 1f - bounceEaseOut(1 - a)
356+
}
357+
358+
/** @see gtx_easing */
359+
fun bounceEaseOut(a: Float): Float {
360+
// Only defined in [0, 1]
361+
assert(a in 0f..1f)
362+
363+
return when {
364+
a < 4f / 11f -> (121f * a * a) / 16f
365+
a < 8f / 11f -> (363f / 40f) * a * a - (99f / 10f) * a + 17f / 5f
366+
a < 9f / 10f -> (4356f / 361f) * a * a - (35442f / 1805f) * a + 16061f / 1805f
367+
else -> (54f / 5f) * a * a - (513f / 25f) * a + 268f / 25f
368+
}
369+
}
370+
371+
/** @see gtx_easing */
372+
fun bounceEaseInOut(a: Float): Float {
373+
// Only defined in [0, 1]
374+
assert(a in 0f..1f)
375+
376+
return when {
377+
a < 0.5f -> 0.5f * (1f - bounceEaseOut(a * 2f))
378+
else -> 0.5f * bounceEaseOut(a * 2f - 1f) + 0.5f
379+
}
380+
}
381+
}

0 commit comments

Comments
 (0)