-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
96 lines (85 loc) · 1.99 KB
/
index.js
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
/*
* react-native-animated-math
* https://github.com/rastapasta/react-native-animated-math
*
* Supplies mathematic logic and approximations by combining
* Animated.add
* Animated.multiply
* Animated.divide
* Animated.modulo
*
* @flow
*/
import { Animated } from 'react-native';
const
sin = (
node: Animated | number
): Animated => {
// Normalize the angle into the 'precise' range
let angle = __normalize(node);
// Taylor series approximation of sine curve near origin:
// sin x = x − x^3/3! + x^5/5! − x^7/7! + x^9/9! − x^11/11!
return Animated.add(
Animated.add(
angle,
Animated.divide(pow(angle, 3), -__factorial(3))
),
Animated.add(
Animated.add(
Animated.divide(pow(angle, 5), __factorial(5)),
Animated.divide(pow(angle, 7), -__factorial(7))
),
Animated.add(
Animated.divide(pow(angle, 9), __factorial(9)),
Animated.divide(pow(angle, 11), -__factorial(11))
)
)
);
},
cos = (
node: Animated | number
): Animated =>
sin(Animated.add(Math.PI/2, node)),
tan = (
node: Animated | number
): Animated =>
Animated.divide(sin(node), cos(node)),
pow = (
node: Animated | number,
times: Animated | number
): Animated | number =>
times == 0 ? 1 :
times == 1 ? node :
Animated.multiply(node, pow(node, Math.floor(times)-1)),
subtract = (
a: Animated | number,
b: Animated | number
): Animated =>
Animated.add(a, Animated.multiply(b, -1)),
__normalize = (
node: Animated | number
): Animated =>
Animated.add(
Animated.modulo(Animated.add(node, Math.PI), 2*Math.PI),
-Math.PI
),
__factorial = (
n: number
) : number => {
let sum = n;
while(n>2)
sum *= --n;
return sum;
};
export default {
sin,
cos,
tan,
pow,
subtract,
// Backward compatibility for previous bad namings
sinus: sin,
cosinus: cos,
tangens: tan,
substract: subtract
};