Repository navigation
Expand file tree
/
Copy pathmath.js
More file actions
283 lines (228 loc) · 7.52 KB
/
Copy pathmath.js
File metadata and controls
283 lines (228 loc) · 7.52 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/** Provides a fast approximation of math functions using a Pade approximant
continued fraction, calculated sample by sample.
Note: This is an approximation which works on a limited range. You are
advised to use input values only between -5 and +5 for limiting the error.
Copied with kind permission of Jules Storer. https://forum.juce.com/t/math-functions-in-littlefoot/33415
This file is part of the JUCE library. Copyright (c) 2019 - ROLI Ltd.
*/
function cosh (x)
{
const x2 = x * x;
const numerator = -(39251520 + x2 * (18471600 + x2 * (1075032 + 14615 * x2)));
const denominator = -39251520 + x2 * (1154160 + x2 * (-16632 + 127 * x2));
return numerator / denominator;
}
function sinh (x)
{
const x2 = x * x;
const numerator = -x * (11511339840 + x2 * (1640635920 + x2 * (52785432 + x2 * 479249)));
const denominator = -11511339840 + x2 * (277920720 + x2 * (-3177720 + x2 * 18361));
return numerator / denominator;
}
function tanh (x)
{
const x2 = x * x;
const numerator = x * (135135 + x2 * (17325 + x2 * (378 + x2)));
const denominator = 135135 + x2 * (62370 + x2 * (3150 + 28 * x2));
return numerator / denominator;
}
function cos (x)
{
const x2 = x * x;
const numerator = -(-39251520 + x2 * (18471600 + x2 * (-1075032 + 14615 * x2)));
const denominator = 39251520 + x2 * (1154160 + x2 * (16632 + x2 * 127));
return numerator / denominator;
}
function sin (x)
{
const x2 = x * x;
const numerator = -x * (-11511339840 + x2 * (1640635920 + x2 * (-52785432 + x2 * 479249)));
const denominator = 11511339840 + x2 * (277920720 + x2 * (3177720 + x2 * 18361));
return numerator / denominator;
}
function tan (x)
{
const x2 = x * x;
const numerator = x * (-135135 + x2 * (17325 + x2 * (-378 + x2)));
const denominator = -135135 + x2 * (62370 + x2 * (-3150 + 28 * x2));
return numerator / denominator;
}
function exp (x)
{
const numerator = 1680 + x * (840 + x * (180 + x * (20 + x)));
const denominator = 1680 + x *(-840 + x * (180 + x * (-20 + x)));
return numerator / denominator;
}
// Polynomial approximating arctangenet on the range -1,1.
// Max error < 0.005 (or 0.29 degrees)
function atan(z)
{
const n1 = 0.97239411;
const n2 = -0.19194795;
return (n1 + n2 * z * z) * z;
}
/*function atan2(y,x)
{
const PI = 3.14159265358979323846264338327950288;
const PI_2 = 6.28318530717958647692528676655900576;
if (x != 0.0) {
if (Math.abs(x) > Math.abs(y)) {
const z = y / x;
if (x > 0.0) {
// atan2(y,x) = atan(y/x) if x > 0
return atan(z);
} else if (y >= 0.0) {
// atan2(y,x) = atan(y/x) + PI if x < 0, y >= 0
return atan(z) + PI;
} else {
// atan2(y,x) = atan(y/x) - PI if x < 0, y < 0
return atan(z) - PI;
}
} else { // Use property atan(y/x) = PI/2 - atan(x/y) if |y/x| > 1.
const z = x / y;
if (y > 0.0) {
// atan2(y,x) = PI/2 - atan(x/y) if |y/x| > 1, y > 0
return -atan(z) + PI_2;
} else {
// atan2(y,x) = -PI/2 - atan(x/y) if |y/x| > 1, y < 0
return -atan(z) - PI_2;
}
}
} else {
if (y > 0.0) { // x = 0, y > 0
return PI_2;
} else if (y < 0.0) { // x = 0, y < 0
return -PI_2;
}
}
return 0.0; // x,y = 0. Could return NaN instead.
}
function atan2_approximation1(y,x)
{
//http://pubs.opengroup.org/onlinepubs/009695399/functions/atan2.html
//Volkan SALMA
const PI = 3.14159265358979323846264338327950288;
const ONEQTR_PI = PI / 4.0;
const THRQTR_PI = 3.0 * PI / 4.0;
let r, angle;
const abs_y = Math.abs(y) + 1e-10; // kludge to prevent 0/0 condition
if ( x < 0.0 ) {
r = (x + abs_y) / (abs_y - x);
angle = THRQTR_PI;
} else {
r = (x - abs_y) / (x + abs_y);
angle = ONEQTR_PI;
}
angle += (0.1963 * r * r - 0.9817) * r;
if ( y < 0.0 )
return -angle; // negate if in quad III or IV
else
return angle;
}
*/
function atan2(y, x ) //_approximation2
{
const PI = 3.14159265358979323846264338327950288;
const PIBY2 = 1.5707963;
if ( x == 0.0 ) {
if ( y > 0.0 ) return PIBY2;
if ( y == 0.0 ) return 0.0;
return -PIBY2;
}
let atan;
const z = y/x;
if ( Math.abs( z ) < 1.0 ) {
atan = z/(1.0 + 0.28*z*z);
if ( x < 0.0 ) {
if ( y < 0.0 ) return atan - PI;
return atan + PI;
}
} else {
atan = PIBY2 - z/(z*z + 0.28);
if ( y < 0.0 ) return atan - PI;
}
return atan;
}
function DegreesToX(degrees, radius, origin)
{
const radians = degrees * Math.PI / 180.0;
return cos(radians) * radius + origin;
}
function DegreesToY(degrees, radius, origin)
{
const radians = degrees * Math.PI / 180.0;
return sin(radians) * radius + origin;
}
function XYToDegrees(x,y,originX,originY)
{
const deltaX = originX - x;
const deltaY = originY - y;
/* const radAngle = atan2(deltaY, deltaX);*/
const radAngle = atan2(y, x);
console.log(`radAngle ${radAngle}`)
const degreeAngle = radAngle * (180.0 / Math.PI);
console.log(`degreeAngle ${degreeAngle}`)
return (180.0 - degreeAngle);
}
const rads = 6.2831853072 / 2
for (let x = 0; x <= rads +0.1; x += 0.1) {
console.log(`cos(${x}) = ${cos(x)} vs ${Math.cos(x)} delta ${Math.abs(Math.cos(x)-cos(x)).toFixed(7)}`)
}
for (let x = 0; x <= rads +0.1; x += 0.1) {
console.log(`sin(${x}) = ${sin(x)} vs ${Math.sin(x)} delta ${Math.abs(Math.sin(x)-sin(x)).toFixed(7)}`)
}
/*for (let x = -5; x <= 5; x++) {
for (let y = -5; y <= 5; y++) {
*/
for (let x = -1.0; x <= 1.0; x += 0.1) {
for (let y = -1.0; y <= 1.0; y += 0.1) {
console.log(`atan2(${y},${x}) = ${atan2(y,x)} vs ${Math.atan2(y,x)} delta ${Math.abs(Math.atan2(y,x)-atan2(y,x)).toFixed(7)}`)
// console.log(`atan2_approximation1(${y},${x}) = ${atan2_approximation1(y,x)} vs ${Math.atan2(y,x)} delta ${Math.abs(Math.atan2(y,x)-atan2_approximation1(y,x)).toFixed(7)}`)
/* console.log(`atan2_approximation2(${y},${x}) = ${atan2_approximation2(y,x)} vs ${Math.atan2(y,x)} delta ${Math.abs(Math.atan2(y,x)-atan2_approximation2(y,x)).toFixed(7)}`)*/
const degrees = XYToDegrees(x,y,0,0)
const nx = DegreesToX(degrees, 1.0, 0)
const ny = DegreesToY(degrees, 1.0, 0)
const dx = Math.abs(x - nx)
const dy = Math.abs(y - ny)
console.log(`X = ${x} degrees = ${degrees} NX = ${nx} DX = ${dx} `)
console.log(`Y = ${y} degrees = ${degrees} NY = ${ny} DY = ${dy} `)
console.log(Math.PI)
/* process.exit()*/
}
}
function sqrt(n) {
if (!n) return 0;
let t;
let squareRoot = n / 2;
do {
t = squareRoot;
squareRoot = (t + (n / t)) / 2;
} while ((t - squareRoot) != 0);
return squareRoot;
}
console.log(Math.sin(90 * Math.PI / 180))
console.log(sin(90 * Math.PI / 180))
function calcAngleDegrees(x, y) {
/* return Math.atan2(y, x) * (180 / Math.PI);*/
return atan2(y, x) * (180 / Math.PI);
}
function radius(x,y)
{
/* return Math.sqrt((x*x) + (y*y))*/
return sqrt((x*x) + (y*y))
}
for (let x = 0; x < 15; x += 1) {
for (let y = 0; y < 15; y += 1) {
const deg = calcAngleDegrees(x, y)
const rad = radius(x,y)
const X = DegreesToX(deg,rad,0)
const Y = DegreesToY(deg,rad,0)}
console.log(`JJR: x:${x} y:${y} deg:${deg.toFixed(2)} radius:${rad.toFixed(2)} X:${X.toFixed(2)} Y:${Y.toFixed(2)}`);
}
}
console.log(calcAngleDegrees(5, 5));
//expected output: 45
console.log(calcAngleDegrees(10, 10));
//expected output: 45
console.log(calcAngleDegrees(0, 10));
//expected output: 90