Skip to content

Commit 3bd42ea

Browse files
authored
feat(🧮): Add asin() (#60)
1 parent 666edcd commit 3bd42ea

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ atan(rad: Node) => Node
164164

165165
### `atan2(node)`
166166

167-
Returns the angle in the plane (in radians) between the positive x-axis and the ray from (0,0) to the point (x,y), `atan2(y,x)`. Beware that this function is not as precise at `Math.atan2()`.
167+
Returns the angle in the plane (in radians) between the positive x-axis and the ray from (0,0) to the point (x,y), `atan2(y,x)`.
168168

169169
```js
170170
atan2(y: Node, x Node) => Node
@@ -179,6 +179,15 @@ We provide this function in case you are using a version of reanimated that does
179179
acos(y: Node, x Node) => Node
180180
```
181181

182+
### `asin(node)`
183+
184+
Returns a arc-sinus of the value in radians of the given node.
185+
We provide this function in case you are using a version of reanimated that doesn't ship `cos`.
186+
187+
```js
188+
asin(y: Node, x Node) => Node
189+
```
190+
182191
### `cubicBezier(t, p0, p1, p2, p3)`
183192

184193
Returns the coordinate of a cubic bezier curve.

src/Math.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const {
1414
sub,
1515
min: min2,
1616
max: max2,
17-
greaterOrEq,
17+
greaterThan,
1818
pow
1919
} = Animated;
2020

@@ -36,28 +36,40 @@ export const toRad = (deg: Animated.Adaptable<number>): Animated.Node<number> =>
3636
export const toDeg = (rad: Animated.Adaptable<number>): Animated.Node<number> =>
3737
multiply(rad, 180 / Math.PI);
3838

39-
export const atan = (rad: Animated.Adaptable<number>): Animated.Node<number> =>
40-
sub(
41-
multiply(Math.PI / 4, rad),
42-
multiply(
43-
multiply(rad, sub(abs(rad), 1)),
44-
add(0.2447, multiply(0.0663, abs(rad)))
45-
)
46-
);
39+
// https://developer.download.nvidia.com/cg/atan.html
40+
export const atan = (x: Animated.Adaptable<number>): Animated.Node<number> =>
41+
atan2(x, 1);
4742

43+
// https://developer.download.nvidia.com/cg/atan2.html
4844
export const atan2 = (
4945
y: Animated.Adaptable<number>,
5046
x: Animated.Adaptable<number>
5147
): Animated.Node<number> => {
52-
const coeff1 = Math.PI / 4;
53-
const coeff2 = 3 * coeff1;
54-
const absY = abs(y);
55-
const angle = cond(
56-
greaterOrEq(x, 0),
57-
[sub(coeff1, multiply(coeff1, divide(sub(x, absY), add(x, absY))))],
58-
[sub(coeff2, multiply(coeff1, divide(add(x, absY), sub(absY, x))))]
59-
);
60-
return cond(lessThan(y, 0), multiply(angle, -1), angle);
48+
const t0: Animated.Value<number> = new Value();
49+
const t1: Animated.Value<number> = new Value();
50+
// const t2: Animated.Value<number> = new Value();
51+
const t3: Animated.Value<number> = new Value();
52+
const t4: Animated.Value<number> = new Value();
53+
return block([
54+
set(t3, abs(x)),
55+
set(t1, abs(y)),
56+
set(t0, max(t3, t1)),
57+
set(t1, min(t3, t1)),
58+
set(t3, divide(1, t0)),
59+
set(t3, multiply(t1, t3)),
60+
set(t4, multiply(t3, t3)),
61+
set(t0, -0.01348047),
62+
set(t0, add(multiply(t0, t4), 0.057477314)),
63+
set(t0, sub(multiply(t0, t4), 0.121239071)),
64+
set(t0, add(multiply(t0, t4), 0.195635925)),
65+
set(t0, sub(multiply(t0, t4), 0.332994597)),
66+
set(t0, add(multiply(t0, t4), 0.99999563)),
67+
set(t3, multiply(t0, t3)),
68+
set(t3, cond(greaterThan(abs(y), abs(x)), sub(1.570796327, t3), t3)),
69+
set(t3, cond(lessThan(x, 0), sub(Math.PI, t3), t3)),
70+
set(t3, cond(lessThan(y, 0), multiply(t3, -1), t3)),
71+
t3
72+
]);
6173
};
6274

6375
// https://developer.download.nvidia.com/cg/acos.html
@@ -81,6 +93,26 @@ export const acos = (x1: Animated.Adaptable<number>) => {
8193
]);
8294
};
8395

96+
// https://developer.download.nvidia.com/cg/asin.html
97+
export const asin = (x1: Animated.Adaptable<number>) => {
98+
const negate: Animated.Value<number> = new Value();
99+
const x: Animated.Value<number> = new Value();
100+
const ret: Animated.Value<number> = new Value();
101+
return block([
102+
set(negate, lessThan(x, 0)),
103+
set(x, abs(x1)),
104+
set(ret, -0.0187293),
105+
set(ret, multiply(ret, x)),
106+
set(ret, add(ret, 0.074261)),
107+
set(ret, multiply(ret, x)),
108+
set(ret, sub(ret, 0.2121144)),
109+
set(ret, multiply(ret, x)),
110+
set(ret, add(ret, 1.5707288)),
111+
set(ret, sub(Math.PI / 2, multiply(sqrt(sub(1, x)), ret))),
112+
sub(ret, multiply(2, negate, ret))
113+
]);
114+
};
115+
84116
export const cubicBezier = (
85117
t: Animated.Node<number>,
86118
p0: Animated.Node<number>,

0 commit comments

Comments
 (0)