forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigonometry.js
356 lines (342 loc) · 8.7 KB
/
trigonometry.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* @module Math
* @submodule Trigonometry
* @for p5
* @requires core
* @requires polargeometry
* @requires constants
*/
'use strict';
var p5 = require('../core/core');
var polarGeometry = require('./polargeometry');
var constants = require('../core/constants');
p5.prototype._angleMode = constants.RADIANS;
/**
* The inverse of cos(), returns the arc cosine of a value. This function
* expects the values in the range of -1 to 1 and values are returned in
* the range 0 to PI (3.1415927).
*
* @method acos
* @param {Number} value the value whose arc cosine is to be returned
* @return {Number} the arc cosine of the given value
*
* @example
* <div class= “norender">
* <code>
* var a = PI;
* var c = cos(a);
* var ac = acos(c);
* // Prints: "3.1415927 : -1.0 : 3.1415927"
* print(a + " : " + c + " : " + ac);
* </code>
* </div>
*
* <div class= “norender">
* <code>
* var a = PI + PI/4.0;
* var c = cos(a);
* var ac = acos(c);
* // Prints: "3.926991 : -0.70710665 : 2.3561943"
* print(a + " : " + c + " : " + ac);
* </code>
* </div>
*/
p5.prototype.acos = function(ratio) {
if (this._angleMode === constants.RADIANS) {
return Math.acos(ratio);
} else {
return polarGeometry.radiansToDegrees(Math.acos(ratio));
}
};
/**
* The inverse of sin(), returns the arc sine of a value. This function
* expects the values in the range of -1 to 1 and values are returned
* in the range -PI/2 to PI/2.
*
* @method asin
* @param {Number} value the value whose arc sine is to be returned
* @return {Number} the arc sine of the given value
*
* @example
* <div class= “norender">
* <code>
* var a = PI + PI/3;
* var s = sin(a);
* var as = asin(s);
* // Prints: "1.0471976 : 0.86602545 : 1.0471976"
* print(a + " : " + s + " : " + as);
* </code>
* </div>
*
* <div class= “norender">
* <code>
* var a = PI + PI/3.0;
* var s = sin(a);
* var as = asin(s);
* // Prints: "4.1887903 : -0.86602545 : -1.0471976"
* print(a + " : " + s + " : " + as);
* </code>
* </div>
*
*/
p5.prototype.asin = function(ratio) {
if (this._angleMode === constants.RADIANS) {
return Math.asin(ratio);
} else {
return polarGeometry.radiansToDegrees(Math.asin(ratio));
}
};
/**
* The inverse of tan(), returns the arc tangent of a value. This function
* expects the values in the range of -Infinity to Infinity (exclusive) and
* values are returned in the range -PI/2 to PI/2.
*
* @method atan
* @param {Number} value the value whose arc tangent is to be returned
* @return {Number} the arc tangent of the given value
*
* @example
* <div class= “norender">
* <code>
* var a = PI + PI/3;
* var t = tan(a);
* var at = atan(t);
* // Prints: "1.0471976 : 1.7320509 : 1.0471976"
* print(a + " : " + t + " : " + at);
* </code>
* </div>
*
* <div class= “norender">
* <code>
* var a = PI + PI/3.0;
* var t = tan(a);
* var at = atan(t);
* // Prints: "4.1887903 : 1.7320513 : 1.0471977"
* print(a + " : " + t + " : " + at);
* </code>
* </div>
*
*/
p5.prototype.atan = function(ratio) {
if (this._angleMode === constants.RADIANS) {
return Math.atan(ratio);
} else {
return polarGeometry.radiansToDegrees(Math.atan(ratio));
}
};
/**
* Calculates the angle (in radians) from a specified point to the coordinate
* origin as measured from the positive x-axis. Values are returned as a
* float in the range from PI to -PI. The atan2() function is most often used
* for orienting geometry to the position of the cursor.
* <br><br>
* Note: The y-coordinate of the point is the first parameter, and the
* x-coordinate is the second parameter, due the the structure of calculating
* the tangent.
*
* @method atan2
* @param {Number} y y-coordinate of the point
* @param {Number} x x-coordinate of the point
* @return {Number} the arc tangent of the given point
*
* @example
* <div>
* <code>
* function draw() {
* background(204);
* translate(width/2, height/2);
* var a = atan2(mouseY-height/2, mouseX-width/2);
* rotate(a);
* rect(-30, -5, 60, 10);
* }
* </code>
* </div>
*
* @alt
* 60 by 10 rect at center of canvas rotates with mouse movements
*
*/
p5.prototype.atan2 = function (y, x) {
if (this._angleMode === constants.RADIANS) {
return Math.atan2(y, x);
} else {
return polarGeometry.radiansToDegrees(Math.atan2(y, x));
}
};
/**
* Calculates the cosine of an angle. This function takes into account the
* current angleMode. Values are returned in the range -1 to 1.
*
* @method cos
* @param {Number} angle the angle
* @return {Number} the cosine of the angle
*
* @example
* <div>
* <code>
* var a = 0.0;
* var inc = TWO_PI/25.0;
* for (var i = 0; i < 25; i++) {
* line(i*4, 50, i*4, 50+cos(a)*40.0);
* a = a + inc;
* }
* </code>
* </div>
*
* @alt
* vertical black lines form wave patterns, extend-down on left and right side
*
*/
p5.prototype.cos = function(angle) {
if (this._angleMode === constants.RADIANS) {
return Math.cos(angle);
} else {
return Math.cos(this.radians(angle));
}
};
/**
* Calculates the sine of an angle. This function takes into account the
* current angleMode. Values are returned in the range -1 to 1.
*
* @method sin
* @param {Number} angle the angle
* @return {Number} the sine of the angle
*
* @example
* <div>
* <code>
* var a = 0.0;
* var inc = TWO_PI/25.0;
* for (var i = 0; i < 25; i++) {
* line(i*4, 50, i*4, 50+sin(a)*40.0);
* a = a + inc;
* }
* </code>
* </div>
*
* @alt
* vertical black lines extend down and up from center to form wave pattern
*
*/
p5.prototype.sin = function(angle) {
if (this._angleMode === constants.RADIANS) {
return Math.sin(angle);
} else {
return Math.sin(this.radians(angle));
}
};
/**
* Calculates the tangent of an angle. This function takes into account
* the current angleMode. Values are returned in the range -1 to 1.
*
* @method tan
* @param {Number} angle the angle
* @return {Number} the tangent of the angle
*
* @example
* <div>
* <code>
* var a = 0.0;
* var inc = TWO_PI/50.0;
* for (var i = 0; i < 100; i = i+2) {
* line(i, 50, i, 50+tan(a)*2.0);
* a = a + inc;
* }
* </code>
*
*
* @alt
* vertical black lines end down and up from center to form spike pattern
*
*/
p5.prototype.tan = function(angle) {
if (this._angleMode === constants.RADIANS) {
return Math.tan(angle);
} else {
return Math.tan(this.radians(angle));
}
};
/**
* Converts a radian measurement to its corresponding value in degrees.
* Radians and degrees are two ways of measuring the same thing. There are
* 360 degrees in a circle and 2*PI radians in a circle. For example,
* 90° = PI/2 = 1.5707964.
*
* @method degrees
* @param {Number} radians the radians value to convert to degrees
* @return {Number} the converted angle
*
*
* @example
* <div class= “norender">
* <code>
* var rad = PI/4;
* var deg = degrees(rad);
* print(rad + " radians is " + deg + " degrees");
* // Prints: 0.7853981633974483 radians is 45 degrees
* </code>
* </div>
*
*/
p5.prototype.degrees = function(angle) {
return polarGeometry.radiansToDegrees(angle);
};
/**
* Converts a degree measurement to its corresponding value in radians.
* Radians and degrees are two ways of measuring the same thing. There are
* 360 degrees in a circle and 2*PI radians in a circle. For example,
* 90° = PI/2 = 1.5707964.
*
* @method radians
* @param {Number} degrees the degree value to convert to radians
* @return {Number} the converted angle
*
* @example
* <div class= “norender">
* <code>
* var deg = 45.0;
* var rad = radians(deg);
* print(deg + " degrees is " + rad + " radians");
* // Prints: 45 degrees is 0.7853981633974483 radians
* </code>
* </div>
*/
p5.prototype.radians = function(angle) {
return polarGeometry.degreesToRadians(angle);
};
/**
* Sets the current mode of p5 to given mode. Default mode is RADIANS.
*
* @method angleMode
* @param {Constant} mode either RADIANS or DEGREES
*
* @example
* <div>
* <code>
* function draw(){
* background(204);
* angleMode(DEGREES); // Change the mode to DEGREES
* var a = atan2(mouseY-height/2, mouseX-width/2);
* translate(width/2, height/2);
* push();
* rotate(a);
* rect(-20, -5, 40, 10); // Larger rectangle is rotating in degrees
* pop();
* angleMode(RADIANS); // Change the mode to RADIANS
* rotate(a); // var a stays the same
* rect(-40, -5, 20, 10); // Smaller rectangle is rotating in radians
* }
* </code>
* </div>
*
* @alt
* 40 by 10 rect in center rotates with mouse moves. 20 by 10 rect moves faster.
*
*
*/
p5.prototype.angleMode = function(mode) {
if (mode === constants.DEGREES || mode === constants.RADIANS) {
this._angleMode = mode;
}
};
module.exports = p5;