p5 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{src → dist}/accessibility/color_namer.js +48 -3
- package/{src → dist}/accessibility/describe.js +12 -2
- package/{src → dist}/accessibility/gridOutput.js +2 -2
- package/dist/accessibility/index.js +60 -0
- package/{src → dist}/accessibility/outputs.js +23 -2
- package/{src → dist}/accessibility/textOutput.js +2 -2
- package/dist/app.js +120 -0
- package/{src → dist}/color/color_conversion.js +48 -10
- package/{src → dist}/color/color_spaces/hsb.js +3 -1
- package/dist/color/creating_reading.js +3 -0
- package/dist/color/index.js +13 -0
- package/dist/color/p5.Color.culori.js +1 -0
- package/dist/color/p5.Color.js +3 -0
- package/{src → dist}/color/setting.js +9 -6
- package/{src/core/constants.js → dist/constants-tYr0tCl8.js} +284 -132
- package/{src → dist}/core/States.js +3 -1
- package/dist/core/constants.js +1 -0
- package/{src → dist}/core/environment.js +12 -10
- package/{src → dist}/core/friendly_errors/browser_errors.js +1 -1
- package/{src → dist}/core/friendly_errors/fes_core.js +14 -44
- package/{src → dist}/core/friendly_errors/file_errors.js +6 -3
- package/dist/core/friendly_errors/index.js +23 -0
- package/dist/core/friendly_errors/param_validator.js +5421 -0
- package/{src → dist}/core/friendly_errors/sketch_reader.js +50 -4
- package/{src → dist}/core/friendly_errors/sketch_verifier.js +6 -6
- package/{src → dist}/core/friendly_errors/stacktrace.js +3 -5
- package/{src → dist}/core/friendly_errors/validate_params.js +50 -41
- package/{src → dist}/core/helpers.js +9 -6
- package/dist/core/init.js +105 -0
- package/dist/core/internationalization.js +302 -0
- package/dist/core/legacy.js +73 -0
- package/dist/core/main.js +44 -0
- package/dist/core/noop.js +3 -0
- package/dist/core/p5.Graphics.js +40 -0
- package/dist/core/p5.Renderer.js +11 -0
- package/dist/core/p5.Renderer2D.js +44 -0
- package/dist/core/reference.js +1 -0
- package/dist/core/rendering.js +40 -0
- package/{src → dist}/core/structure.js +3 -3
- package/{src → dist}/core/transform.js +2 -2
- package/{src/color/creating_reading.js → dist/creating_reading-Cr8L2Jnm.js} +841 -13
- package/{src → dist}/data/index.js +3 -1
- package/{src → dist}/data/local_storage.js +2 -8
- package/{src → dist}/dom/dom.js +19 -13
- package/dist/dom/index.js +18 -0
- package/{src → dist}/dom/p5.Element.js +14 -12
- package/{src → dist}/dom/p5.File.js +4 -4
- package/{src → dist}/dom/p5.MediaElement.js +10 -4
- package/{src → dist}/events/acceleration.js +26 -26
- package/{src → dist}/events/index.js +3 -1
- package/{src → dist}/events/keyboard.js +14 -12
- package/{src → dist}/events/pointer.js +16 -17
- package/dist/image/const.js +9 -0
- package/{src → dist}/image/filterRenderer2D.js +57 -37
- package/{src → dist}/image/filters.js +1 -3
- package/dist/image/image.js +40 -0
- package/dist/image/index.js +51 -0
- package/dist/image/loading_displaying.js +40 -0
- package/dist/image/p5.Image.js +11 -0
- package/{src → dist}/image/pixels.js +5 -3
- package/{src → dist}/io/csv.js +72 -70
- package/dist/io/files.js +40 -0
- package/dist/io/index.js +51 -0
- package/{src → dist}/io/p5.Table.js +6 -6
- package/{src → dist}/io/p5.TableRow.js +5 -6
- package/{src → dist}/io/p5.XML.js +2 -5
- package/{src → dist}/io/utilities.js +1 -1
- package/{src/core/p5.Renderer2D.js → dist/main-CAxvgiOV.js} +738 -57
- package/{src → dist}/math/Matrices/Matrix.js +10 -8
- package/{src → dist}/math/Matrices/MatrixInterface.js +5 -3
- package/{src → dist}/math/Matrices/MatrixNumjs.js +12 -26
- package/{src → dist}/math/calculation.js +2 -2
- package/{src → dist}/math/index.js +6 -3
- package/{src → dist}/math/math.js +3 -3
- package/{src → dist}/math/noise.js +2 -2
- package/{src → dist}/math/p5.Matrix.js +7 -4
- package/{src → dist}/math/p5.Vector.js +6 -6
- package/{src → dist}/math/random.js +2 -2
- package/{src → dist}/math/trigonometry.js +16 -15
- package/{src/image/p5.Image.js → dist/p5.Renderer-Swjl9HQO.js} +393 -22
- package/dist/rendering-B5TRR7aY.js +24960 -0
- package/{src → dist}/shape/2d_primitives.js +18 -17
- package/{src → dist}/shape/attributes.js +18 -17
- package/{src → dist}/shape/curves.js +2 -2
- package/{src → dist}/shape/custom_shapes.js +44 -64
- package/{src → dist}/shape/index.js +10 -2
- package/{src → dist}/shape/vertex.js +2 -3
- package/dist/type/index.js +25 -0
- package/{src → dist}/type/lib/Typr.js +76 -94
- package/{src → dist}/type/p5.Font.js +37 -63
- package/{src → dist}/type/textCore.js +35 -58
- package/{src → dist}/type/unicodeRanges.js +3 -1
- package/{src → dist}/utilities/conversion.js +2 -2
- package/{src → dist}/utilities/index.js +3 -1
- package/{src → dist}/utilities/time_date.js +6 -7
- package/{src → dist}/utilities/utility_functions.js +2 -2
- package/dist/webgl/3d_primitives.js +40 -0
- package/{src → dist}/webgl/GeometryBufferCache.js +3 -1
- package/{src → dist}/webgl/GeometryBuilder.js +12 -8
- package/{src → dist}/webgl/ShaderGenerator.js +79 -82
- package/{src → dist}/webgl/ShapeBuilder.js +26 -23
- package/dist/webgl/index.js +76 -0
- package/{src → dist}/webgl/interaction.js +7 -6
- package/dist/webgl/light.js +40 -0
- package/{src → dist}/webgl/loading.js +45 -12
- package/dist/webgl/material.js +40 -0
- package/dist/webgl/p5.Camera.js +40 -0
- package/{src → dist}/webgl/p5.DataArray.js +3 -5
- package/dist/webgl/p5.Framebuffer.js +40 -0
- package/{src → dist}/webgl/p5.Geometry.js +12 -15
- package/{src → dist}/webgl/p5.Quat.js +5 -4
- package/{src → dist}/webgl/p5.RenderBuffer.js +2 -3
- package/dist/webgl/p5.RendererGL.js +40 -0
- package/dist/webgl/p5.Shader.js +40 -0
- package/dist/webgl/p5.Texture.js +40 -0
- package/{src → dist}/webgl/text.js +78 -38
- package/lib/p5.esm.js +296 -194
- package/lib/p5.js +296 -194
- package/lib/p5.min.js +1 -1
- package/package.json +17 -17
- package/translations/dev.js +6 -6
- package/translations/index.js +1 -1
- package/types/accessibility/color_namer.d.ts +8 -0
- package/types/accessibility/describe.d.ts +184 -0
- package/types/accessibility/gridOutput.d.ts +8 -0
- package/types/accessibility/outputs.d.ts +235 -0
- package/types/accessibility/textOutput.d.ts +8 -0
- package/types/color/color_conversion.d.ts +47 -0
- package/types/color/creating_reading.d.ts +1348 -0
- package/types/color/p5.Color.d.ts +1070 -0
- package/types/color/setting.d.ts +2085 -0
- package/types/core/constants.d.ts +341 -0
- package/types/core/environment.d.ts +668 -0
- package/types/core/friendly_errors/fes_core.d.ts +8 -0
- package/types/core/friendly_errors/file_errors.d.ts +8 -0
- package/types/core/friendly_errors/param_validator.d.ts +30 -0
- package/types/core/friendly_errors/sketch_reader.d.ts +8 -0
- package/types/core/friendly_errors/stacktrace.d.ts +11 -0
- package/types/core/friendly_errors/validate_params.d.ts +8 -0
- package/types/core/helpers.d.ts +8 -0
- package/types/core/legacy.d.ts +8 -0
- package/types/core/main.d.ts +5996 -0
- package/types/core/p5.Graphics.d.ts +484 -0
- package/types/core/p5.Renderer.d.ts +14 -0
- package/types/core/reference.d.ts +8 -0
- package/types/core/rendering.d.ts +481 -0
- package/types/core/structure.d.ts +492 -0
- package/types/core/transform.d.ts +1638 -0
- package/types/data/local_storage.d.ts +323 -0
- package/types/dom/dom.d.ts +1295 -0
- package/types/dom/p5.Element.d.ts +2011 -0
- package/types/dom/p5.File.d.ts +13 -0
- package/types/dom/p5.MediaElement.d.ts +1249 -0
- package/types/events/acceleration.d.ts +193 -0
- package/types/events/keyboard.d.ts +499 -0
- package/types/events/pointer.d.ts +782 -0
- package/types/global.d.ts +5542 -0
- package/types/image/filterRenderer2D.d.ts +54 -0
- package/types/image/image.d.ts +326 -0
- package/types/image/loading_displaying.d.ts +580 -0
- package/types/image/p5.Image.d.ts +5882 -0
- package/types/image/pixels.d.ts +832 -0
- package/types/io/files.d.ts +1447 -0
- package/types/io/p5.Table.d.ts +1247 -0
- package/types/io/p5.TableRow.d.ts +343 -0
- package/types/io/p5.XML.d.ts +1188 -0
- package/types/math/Matrices/Matrix.d.ts +1029 -0
- package/types/math/Matrices/MatrixNumjs.d.ts +8 -0
- package/types/math/calculation.d.ts +923 -0
- package/types/math/math.d.ts +90 -0
- package/types/math/noise.d.ts +311 -0
- package/types/math/p5.Matrix.d.ts +8 -0
- package/types/math/p5.Vector.d.ts +3416 -0
- package/types/math/random.d.ts +267 -0
- package/types/math/trigonometry.d.ts +663 -0
- package/types/p5.d.ts +6663 -0
- package/types/shape/2d_primitives.d.ts +1033 -0
- package/types/shape/attributes.d.ts +466 -0
- package/types/shape/curves.d.ts +740 -0
- package/types/shape/custom_shapes.d.ts +888 -0
- package/types/shape/vertex.d.ts +1141 -0
- package/types/type/p5.Font.d.ts +575 -0
- package/types/type/textCore.d.ts +1198 -0
- package/types/utilities/conversion.d.ts +894 -0
- package/types/utilities/time_date.d.ts +295 -0
- package/types/utilities/utility_functions.d.ts +587 -0
- package/types/webgl/3d_primitives.d.ts +1432 -0
- package/types/webgl/ShaderGenerator.d.ts +8 -0
- package/types/webgl/interaction.d.ts +371 -0
- package/types/webgl/light.d.ts +1184 -0
- package/types/webgl/loading.d.ts +481 -0
- package/types/webgl/material.d.ts +2656 -0
- package/types/webgl/p5.Camera.d.ts +3023 -0
- package/types/webgl/p5.DataArray.d.ts +61 -0
- package/types/webgl/p5.Framebuffer.d.ts +760 -0
- package/types/webgl/p5.Geometry.d.ts +1191 -0
- package/types/webgl/p5.Quat.d.ts +45 -0
- package/types/webgl/p5.RendererGL.d.ts +234 -0
- package/types/webgl/p5.Shader.d.ts +660 -0
- package/types/webgl/p5.Texture.d.ts +61 -0
- package/types/webgl/text.d.ts +74 -0
- package/src/README.md +0 -27
- package/src/accessibility/index.js +0 -13
- package/src/app.js +0 -61
- package/src/color/index.js +0 -9
- package/src/color/p5.Color.culori.js +0 -66
- package/src/color/p5.Color.js +0 -851
- package/src/core/README.md +0 -91
- package/src/core/friendly_errors/index.js +0 -13
- package/src/core/friendly_errors/param_validator.js +0 -561
- package/src/core/init.js +0 -58
- package/src/core/internationalization.js +0 -195
- package/src/core/legacy.js +0 -29
- package/src/core/main.js +0 -689
- package/src/core/noop.js +0 -1
- package/src/core/p5.Graphics.js +0 -696
- package/src/core/p5.Renderer.js +0 -408
- package/src/core/reference.js +0 -2060
- package/src/core/rendering.js +0 -697
- package/src/dom/index.js +0 -11
- package/src/image/const.js +0 -6
- package/src/image/image.js +0 -731
- package/src/image/index.js +0 -15
- package/src/image/loading_displaying.js +0 -1431
- package/src/io/files.js +0 -2210
- package/src/io/index.js +0 -11
- package/src/math/README.md +0 -40
- package/src/type/index.js +0 -9
- package/src/webgl/3d_primitives.js +0 -2741
- package/src/webgl/index.js +0 -37
- package/src/webgl/light.js +0 -1851
- package/src/webgl/material.js +0 -3854
- package/src/webgl/p5.Camera.js +0 -4010
- package/src/webgl/p5.Framebuffer.js +0 -1865
- package/src/webgl/p5.RendererGL.js +0 -2867
- package/src/webgl/p5.Shader.js +0 -1505
- package/src/webgl/p5.Texture.js +0 -541
- package/src/webgl/shaders/basic.frag +0 -6
- package/src/webgl/shaders/filters/base.frag +0 -22
- package/src/webgl/shaders/filters/base.vert +0 -19
- package/src/webgl/shaders/filters/blur.frag +0 -60
- package/src/webgl/shaders/filters/default.vert +0 -18
- package/src/webgl/shaders/filters/dilate.frag +0 -39
- package/src/webgl/shaders/filters/erode.frag +0 -39
- package/src/webgl/shaders/filters/gray.frag +0 -16
- package/src/webgl/shaders/filters/invert.frag +0 -15
- package/src/webgl/shaders/filters/opaque.frag +0 -12
- package/src/webgl/shaders/filters/posterize.frag +0 -29
- package/src/webgl/shaders/filters/threshold.frag +0 -23
- package/src/webgl/shaders/font.frag +0 -216
- package/src/webgl/shaders/font.vert +0 -44
- package/src/webgl/shaders/imageLight.vert +0 -33
- package/src/webgl/shaders/imageLightDiffused.frag +0 -82
- package/src/webgl/shaders/imageLightSpecular.frag +0 -134
- package/src/webgl/shaders/light.vert +0 -37
- package/src/webgl/shaders/light_texture.frag +0 -26
- package/src/webgl/shaders/lighting.glsl +0 -227
- package/src/webgl/shaders/line.frag +0 -74
- package/src/webgl/shaders/line.vert +0 -294
- package/src/webgl/shaders/normal.frag +0 -6
- package/src/webgl/shaders/normal.vert +0 -72
- package/src/webgl/shaders/phong.frag +0 -84
- package/src/webgl/shaders/phong.vert +0 -87
- package/src/webgl/shaders/point.frag +0 -29
- package/src/webgl/shaders/point.vert +0 -19
- package/src/webgl/shaders/sphereMapping.frag +0 -26
- package/src/webgl/shaders/webgl2Compatibility.glsl +0 -34
|
@@ -0,0 +1,3416 @@
|
|
|
1
|
+
// This file is auto-generated from JSDoc documentation
|
|
2
|
+
|
|
3
|
+
import p5 from 'p5';
|
|
4
|
+
import { Vector } from '../math/p5.Vector';
|
|
5
|
+
|
|
6
|
+
declare module 'p5' {
|
|
7
|
+
class Vector {
|
|
8
|
+
constructor(x?: number, y?: number, z?: number);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new 2D vector from an angle.
|
|
12
|
+
*
|
|
13
|
+
* @param
|
|
14
|
+
* @param
|
|
15
|
+
*/
|
|
16
|
+
static fromAngle(angle: number, length: number): p5.Vector;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new 3D vector from a pair of ISO spherical angles.
|
|
20
|
+
*
|
|
21
|
+
* @param
|
|
22
|
+
* @param
|
|
23
|
+
* @param
|
|
24
|
+
*/
|
|
25
|
+
static fromAngles(theta: number, phi: number, length: number): p5.Vector;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new 2D unit vector with a random heading.
|
|
29
|
+
*/
|
|
30
|
+
static random2D(): p5.Vector;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new 3D unit vector with a random heading.
|
|
34
|
+
*/
|
|
35
|
+
static random3D(): p5.Vector;
|
|
36
|
+
|
|
37
|
+
static copy(v: p5.Vector): p5.Vector;
|
|
38
|
+
|
|
39
|
+
static add(v1: p5.Vector, v2: p5.Vector, target: p5.Vector): p5.Vector;
|
|
40
|
+
|
|
41
|
+
static rem(v1: p5.Vector, v2: p5.Vector): void;
|
|
42
|
+
|
|
43
|
+
static rem(v1: p5.Vector, v2: p5.Vector): p5.Vector;
|
|
44
|
+
|
|
45
|
+
static sub(v1: p5.Vector, v2: p5.Vector, target: p5.Vector): p5.Vector;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Multiplies a vector by a scalar and returns a new vector.
|
|
49
|
+
*
|
|
50
|
+
* @param
|
|
51
|
+
* @param
|
|
52
|
+
* @param
|
|
53
|
+
*/
|
|
54
|
+
static mult(v: any, n: any, target: any): void;
|
|
55
|
+
|
|
56
|
+
static mult(v: any, n: any, target: any, x: number, y: number, z: number): p5.Vector;
|
|
57
|
+
|
|
58
|
+
static mult(v: p5.Vector, n: number, target: p5.Vector): void;
|
|
59
|
+
|
|
60
|
+
static mult(v: any, n: any, target: p5.Vector, v0: p5.Vector, v1: p5.Vector): void;
|
|
61
|
+
|
|
62
|
+
static mult(v: any, n: any, target: p5.Vector, v0: p5.Vector, arr: number[]): void;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Rotates the vector (only 2D vectors) by the given angle; magnitude remains the same. Returns a new vector.
|
|
66
|
+
*
|
|
67
|
+
* @param
|
|
68
|
+
* @param
|
|
69
|
+
* @param
|
|
70
|
+
*/
|
|
71
|
+
static rotate(v: any, a: any, target: any): void;
|
|
72
|
+
|
|
73
|
+
static rotate(v: p5.Vector, a: any, target: p5.Vector, angle: number): void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Divides a vector by a scalar and returns a new vector.
|
|
77
|
+
*
|
|
78
|
+
* @param
|
|
79
|
+
* @param
|
|
80
|
+
* @param
|
|
81
|
+
*/
|
|
82
|
+
static div(v: any, n: any, target: any): void;
|
|
83
|
+
|
|
84
|
+
static div(v: any, n: any, target: any, x: number, y: number, z: number): p5.Vector;
|
|
85
|
+
|
|
86
|
+
static div(v: p5.Vector, n: number, target: p5.Vector): void;
|
|
87
|
+
|
|
88
|
+
static div(v: any, n: any, target: p5.Vector, v0: p5.Vector, v1: p5.Vector): void;
|
|
89
|
+
|
|
90
|
+
static div(v: any, n: any, target: p5.Vector, v0: p5.Vector, arr: number[]): void;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Calculates the dot product of two vectors.
|
|
94
|
+
*
|
|
95
|
+
* @param
|
|
96
|
+
* @param
|
|
97
|
+
*/
|
|
98
|
+
static dot(v1: any, v2: any): void;
|
|
99
|
+
|
|
100
|
+
static dot(v1: p5.Vector, v2: p5.Vector): number;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Calculates the cross product of two vectors.
|
|
104
|
+
*
|
|
105
|
+
* @param
|
|
106
|
+
* @param
|
|
107
|
+
*/
|
|
108
|
+
static cross(v1: any, v2: any): void;
|
|
109
|
+
|
|
110
|
+
static cross(v1: p5.Vector, v2: p5.Vector): number;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Calculates the Euclidean distance between two points (considering a
|
|
114
|
+
* point as a vector object).
|
|
115
|
+
*
|
|
116
|
+
* @param
|
|
117
|
+
* @param
|
|
118
|
+
*/
|
|
119
|
+
static dist(v1: any, v2: any): void;
|
|
120
|
+
|
|
121
|
+
static dist(v1: p5.Vector, v2: p5.Vector): number;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Linear interpolate a vector to another vector and return the result as a
|
|
125
|
+
* new vector.
|
|
126
|
+
*
|
|
127
|
+
* @param
|
|
128
|
+
* @param
|
|
129
|
+
* @param
|
|
130
|
+
* @param
|
|
131
|
+
*/
|
|
132
|
+
static lerp(v1: any, v2: any, amt: any, target: any): void;
|
|
133
|
+
|
|
134
|
+
static lerp(v1: p5.Vector, v2: p5.Vector, amt: number, target: p5.Vector): p5.Vector;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Performs spherical linear interpolation with the other vector
|
|
138
|
+
* and returns the resulting vector.
|
|
139
|
+
* This works in both 3D and 2D. As for 2D, the result of slerping
|
|
140
|
+
* between 2D vectors is always a 2D vector.
|
|
141
|
+
*
|
|
142
|
+
* @param
|
|
143
|
+
* @param
|
|
144
|
+
* @param
|
|
145
|
+
* @param
|
|
146
|
+
*/
|
|
147
|
+
static slerp(v1: any, v2: any, amt: any, target: any): void;
|
|
148
|
+
|
|
149
|
+
static slerp(v1: p5.Vector, v2: p5.Vector, amt: number, target: p5.Vector): p5.Vector;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Calculates the magnitude (length) of the vector and returns the result as
|
|
153
|
+
* a float (this is simply the equation `sqrt(x*x + y*y + z*z)`.)
|
|
154
|
+
*
|
|
155
|
+
* @param
|
|
156
|
+
*/
|
|
157
|
+
static mag(vecT: any): void;
|
|
158
|
+
|
|
159
|
+
static mag(vecT: p5.Vector): number;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Calculates the squared magnitude of the vector and returns the result
|
|
163
|
+
* as a float (this is simply the equation (x*x + y*y + z*z).)
|
|
164
|
+
* Faster if the real length is not required in the
|
|
165
|
+
* case of comparing vectors, etc.
|
|
166
|
+
*
|
|
167
|
+
* @param
|
|
168
|
+
*/
|
|
169
|
+
static magSq(vecT: any): void;
|
|
170
|
+
|
|
171
|
+
static magSq(vecT: p5.Vector): number;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Normalize the vector to length 1 (make it a unit vector).
|
|
175
|
+
*
|
|
176
|
+
* @param
|
|
177
|
+
* @param
|
|
178
|
+
*/
|
|
179
|
+
static normalize(v: any, target: any): void;
|
|
180
|
+
|
|
181
|
+
static normalize(v: p5.Vector, target: p5.Vector): p5.Vector;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Limit the magnitude of the vector to the value used for the max
|
|
185
|
+
* parameter.
|
|
186
|
+
*
|
|
187
|
+
* @param
|
|
188
|
+
* @param
|
|
189
|
+
* @param
|
|
190
|
+
*/
|
|
191
|
+
static limit(v: any, max: any, target: any): void;
|
|
192
|
+
|
|
193
|
+
static limit(v: p5.Vector, max: number, target: p5.Vector): p5.Vector;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Set the magnitude of the vector to the value used for the len
|
|
197
|
+
* parameter.
|
|
198
|
+
*
|
|
199
|
+
* @param
|
|
200
|
+
* @param
|
|
201
|
+
* @param
|
|
202
|
+
*/
|
|
203
|
+
static setMag(v: any, len: any, target: any): void;
|
|
204
|
+
|
|
205
|
+
static setMag(v: p5.Vector, len: number, target: p5.Vector): p5.Vector;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Calculate the angle of rotation for this vector (only 2D vectors).
|
|
209
|
+
* p5.Vectors created using createVector()
|
|
210
|
+
* will take the current angleMode into
|
|
211
|
+
* consideration, and give the angle in radians or degrees accordingly.
|
|
212
|
+
*
|
|
213
|
+
* @param
|
|
214
|
+
*/
|
|
215
|
+
static heading(v: any): void;
|
|
216
|
+
|
|
217
|
+
static heading(v: p5.Vector): number;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Calculates and returns the angle between two vectors. This function will take
|
|
221
|
+
* the angleMode on v1 into consideration, and
|
|
222
|
+
* give the angle in radians or degrees accordingly.
|
|
223
|
+
*
|
|
224
|
+
* @param
|
|
225
|
+
* @param
|
|
226
|
+
*/
|
|
227
|
+
static angleBetween(v1: any, v2: any): void;
|
|
228
|
+
|
|
229
|
+
static angleBetween(v1: p5.Vector, v2: p5.Vector): number;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Reflect a vector about a normal to a line in 2D, or about a normal to a
|
|
233
|
+
* plane in 3D.
|
|
234
|
+
*
|
|
235
|
+
* @param
|
|
236
|
+
* @param
|
|
237
|
+
* @param
|
|
238
|
+
*/
|
|
239
|
+
static reflect(incidentVector: any, surfaceNormal: any, target: any): void;
|
|
240
|
+
|
|
241
|
+
static reflect(incidentVector: p5.Vector, surfaceNormal: p5.Vector, target: p5.Vector): p5.Vector;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Return a representation of this vector as a float array. This is only
|
|
245
|
+
* for temporary use. If used in any other fashion, the contents should be
|
|
246
|
+
* copied by using the p5.Vector.copy()
|
|
247
|
+
* method to copy into your own vector.
|
|
248
|
+
*
|
|
249
|
+
* @param
|
|
250
|
+
*/
|
|
251
|
+
static array(v: any): void;
|
|
252
|
+
|
|
253
|
+
static array(v: p5.Vector): number[];
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Equality check against a p5.Vector
|
|
257
|
+
*
|
|
258
|
+
* @param
|
|
259
|
+
* @param
|
|
260
|
+
*/
|
|
261
|
+
static equals(v1: any, v2: any): void;
|
|
262
|
+
|
|
263
|
+
static equals(v1: p5.Vector | Array, v2: p5.Vector | Array): boolean;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Retrieves the value at the specified index from the vector.This method allows you to get the value of a specific component of the vector
|
|
267
|
+
* by providing its index. Think of the vector as a list of numbers, where each
|
|
268
|
+
* number represents a different direction (like x, y, or z). The index is just
|
|
269
|
+
* the position of the number in that list.For example, if you have a vector with values 10, 20, 30 the index 0 would
|
|
270
|
+
* give you the first value 10, index 1 would give you the second value 20,
|
|
271
|
+
* and so on.
|
|
272
|
+
*
|
|
273
|
+
* @param
|
|
274
|
+
*/
|
|
275
|
+
getValue(index: number): number;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Sets the value at the specified index of the vector.This method allows you to change a specific component of the vector by providing its index and the new value you want to set.
|
|
279
|
+
* Think of the vector as a list of numbers, where each number represents a different direction (like x, y, or z).
|
|
280
|
+
* The index is just the position of the number in that list.For example, if you have a vector with values [0, 20, 30], and you want to change the second value (20) to 50,
|
|
281
|
+
* you would use this method with index 1 (since indexes start at 0) and value 50.
|
|
282
|
+
*
|
|
283
|
+
* @param
|
|
284
|
+
* @param
|
|
285
|
+
*/
|
|
286
|
+
setValue(index: number, value: number): void;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Returns a string representation of a vector.Calling `toString()` is useful for printing vectors to the console while
|
|
290
|
+
* debugging.
|
|
291
|
+
*/
|
|
292
|
+
toString(): string;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Sets the vector's `x`, `y`, and `z` components.`set()` can use separate numbers, as in `v.set(1, 2, 3)`, a
|
|
296
|
+
* p5.Vector object, as in `v.set(v2)`, or an
|
|
297
|
+
* array of numbers, as in `v.set([1, 2, 3])`.If a value isn't provided for a component, it will be set to 0. For
|
|
298
|
+
* example, `v.set(4, 5)` sets `v.x` to 4, `v.y` to 5, and `v.z` to 0.
|
|
299
|
+
* Calling `set()` with no arguments, as in `v.set()`, sets all the vector's
|
|
300
|
+
* components to 0.
|
|
301
|
+
*
|
|
302
|
+
* @param
|
|
303
|
+
* @param
|
|
304
|
+
* @param
|
|
305
|
+
* @param
|
|
306
|
+
*/
|
|
307
|
+
set(args: any[], x: number, y: number, z: number): void;
|
|
308
|
+
|
|
309
|
+
set(args: any[], value: p5.Vector | number[]): void;
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Returns a copy of the p5.Vector object.
|
|
313
|
+
*/
|
|
314
|
+
copy(): p5.Vector;
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Adds to a vector's components.`add()` can use separate numbers, as in `v.add(1, 2, 3)`,
|
|
318
|
+
* another p5.Vector object, as in `v.add(v2)`, or
|
|
319
|
+
* an array of numbers, as in `v.add([1, 2, 3])`.If a value isn't provided for a component, it won't change. For
|
|
320
|
+
* example, `v.add(4, 5)` adds 4 to `v.x`, 5 to `v.y`, and 0 to `v.z`.
|
|
321
|
+
* Calling `add()` with no arguments, as in `v.add()`, has no effect.This method supports N-dimensional vectors.The static version of `add()`, as in `p5.Vector.add(v2, v1)`, returns a new
|
|
322
|
+
* p5.Vector object and doesn't change the
|
|
323
|
+
* originals.
|
|
324
|
+
*
|
|
325
|
+
* @param
|
|
326
|
+
* @param
|
|
327
|
+
* @param
|
|
328
|
+
* @param
|
|
329
|
+
*/
|
|
330
|
+
add(args: any[], x: number | Array, y: number, z: number): void;
|
|
331
|
+
|
|
332
|
+
add(args: any[], value: p5.Vector | number[]): void;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Performs modulo (remainder) division with a vector's `x`, `y`, and `z`
|
|
336
|
+
* components.`rem()` can use separate numbers, as in `v.rem(1, 2, 3)`,
|
|
337
|
+
* another p5.Vector object, as in `v.rem(v2)`, or
|
|
338
|
+
* an array of numbers, as in `v.rem([1, 2, 3])`.If only one value is provided, as in `v.rem(2)`, then all the components
|
|
339
|
+
* will be set to their values modulo 2. If two values are provided, as in
|
|
340
|
+
* `v.rem(2, 3)`, then `v.z` won't change. Calling `rem()` with no
|
|
341
|
+
* arguments, as in `v.rem()`, has no effect.The static version of `rem()`, as in `p5.Vector.rem(v2, v1)`, returns a
|
|
342
|
+
* new p5.Vector object and doesn't change the
|
|
343
|
+
* originals.
|
|
344
|
+
*
|
|
345
|
+
* @param
|
|
346
|
+
* @param
|
|
347
|
+
* @param
|
|
348
|
+
*/
|
|
349
|
+
rem(x: number, y: number, z: number): void;
|
|
350
|
+
|
|
351
|
+
rem(x: any, y: any, z: any, value: p5.Vector | number[]): void;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Subtracts from a vector's `x`, `y`, and `z` components.`sub()` can use separate numbers, as in `v.sub(1, 2, 3)`, another
|
|
355
|
+
* p5.Vector object, as in `v.sub(v2)`, or an array
|
|
356
|
+
* of numbers, as in `v.sub([1, 2, 3])`.If a value isn't provided for a component, it won't change. For
|
|
357
|
+
* example, `v.sub(4, 5)` subtracts 4 from `v.x`, 5 from `v.y`, and 0 from `v.z`.
|
|
358
|
+
* Calling `sub()` with no arguments, as in `v.sub()`, has no effect.The static version of `sub()`, as in `p5.Vector.sub(v2, v1)`, returns a new
|
|
359
|
+
* p5.Vector object and doesn't change the
|
|
360
|
+
* originals.
|
|
361
|
+
*
|
|
362
|
+
* @param
|
|
363
|
+
* @param
|
|
364
|
+
* @param
|
|
365
|
+
* @param
|
|
366
|
+
*/
|
|
367
|
+
sub(args: any[], x: number, y: number, z: number): void;
|
|
368
|
+
|
|
369
|
+
sub(args: any[], value: p5.Vector | number[]): void;
|
|
370
|
+
|
|
371
|
+
mult(args: any[], x: number, y: number, z: number): void;
|
|
372
|
+
|
|
373
|
+
mult(args: any[], arr: number[]): void;
|
|
374
|
+
|
|
375
|
+
mult(args: any[], v: p5.Vector): void;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Divides a vector's `x`, `y`, and `z` components.`div()` can use separate numbers, as in `v.div(1, 2, 3)`, another
|
|
379
|
+
* p5.Vector object, as in `v.div(v2)`, or an array
|
|
380
|
+
* of numbers, as in `v.div([1, 2, 3])`.If only one value is provided, as in `v.div(2)`, then all the components
|
|
381
|
+
* will be divided by 2. If a value isn't provided for a component, it
|
|
382
|
+
* won't change. For example, `v.div(4, 5)` divides `v.x` by, `v.y` by 5,
|
|
383
|
+
* and `v.z` by 1. Calling `div()` with no arguments, as in `v.div()`, has
|
|
384
|
+
* no effect.The static version of `div()`, as in `p5.Vector.div(v, 2)`, returns a new
|
|
385
|
+
* p5.Vector object and doesn't change the
|
|
386
|
+
* originals.
|
|
387
|
+
*
|
|
388
|
+
* @param
|
|
389
|
+
* @param
|
|
390
|
+
*/
|
|
391
|
+
div(args: any[], n: number): void;
|
|
392
|
+
|
|
393
|
+
div(args: any[], x: number, y: number, z: number): void;
|
|
394
|
+
|
|
395
|
+
div(args: any[], arr: number[]): void;
|
|
396
|
+
|
|
397
|
+
div(args: any[], v: p5.Vector): void;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Calculates the magnitude (length) of the vector.Use mag() to calculate the magnitude of a 2D vector
|
|
401
|
+
* using components as in `mag(x, y)`.
|
|
402
|
+
*/
|
|
403
|
+
mag(): number;
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Calculates the magnitude (length) of the vector squared.
|
|
407
|
+
*/
|
|
408
|
+
magSq(): number;
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Calculates the dot product of two vectors.The dot product is a number that describes the overlap between two vectors.
|
|
412
|
+
* Visually, the dot product can be thought of as the "shadow" one vector
|
|
413
|
+
* casts on another. The dot product's magnitude is largest when two vectors
|
|
414
|
+
* point in the same or opposite directions. Its magnitude is 0 when two
|
|
415
|
+
* vectors form a right angle.The version of `dot()` with one parameter interprets it as another
|
|
416
|
+
* p5.Vector object.The version of `dot()` with multiple parameters interprets them as the
|
|
417
|
+
* `x`, `y`, and `z` components of another vector.The static version of `dot()`, as in `p5.Vector.dot(v1, v2)`, is the same
|
|
418
|
+
* as calling `v1.dot(v2)`.
|
|
419
|
+
*
|
|
420
|
+
* @param
|
|
421
|
+
* @param
|
|
422
|
+
* @param
|
|
423
|
+
* @param
|
|
424
|
+
*/
|
|
425
|
+
dot(args: any[], x: number, y: number, z: number): number;
|
|
426
|
+
|
|
427
|
+
dot(args: any[], v: p5.Vector): number;
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Calculates the cross product of two vectors.The cross product is a vector that points straight out of the plane created
|
|
431
|
+
* by two vectors. The cross product's magnitude is the area of the parallelogram
|
|
432
|
+
* formed by the original two vectors.The static version of `cross()`, as in `p5.Vector.cross(v1, v2)`, is the same
|
|
433
|
+
* as calling `v1.cross(v2)`.
|
|
434
|
+
*
|
|
435
|
+
* @param
|
|
436
|
+
*/
|
|
437
|
+
cross(v: p5.Vector): p5.Vector;
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Scales the components of a p5.Vector object so
|
|
441
|
+
* that its magnitude is 1.The static version of `normalize()`, as in `p5.Vector.normalize(v)`,
|
|
442
|
+
* returns a new p5.Vector object and doesn't change
|
|
443
|
+
* the original.
|
|
444
|
+
*/
|
|
445
|
+
normalize(): p5.Vector;
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Limits a vector's magnitude to a maximum value.The static version of `limit()`, as in `p5.Vector.limit(v, 5)`, returns a
|
|
449
|
+
* new p5.Vector object and doesn't change the
|
|
450
|
+
* original.
|
|
451
|
+
*
|
|
452
|
+
* @param
|
|
453
|
+
*/
|
|
454
|
+
limit(max: number): void;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Sets a vector's magnitude to a given value.The static version of `setMag()`, as in `p5.Vector.setMag(v, 10)`, returns
|
|
458
|
+
* a new p5.Vector object and doesn't change the
|
|
459
|
+
* original.
|
|
460
|
+
*
|
|
461
|
+
* @param
|
|
462
|
+
* @param
|
|
463
|
+
*/
|
|
464
|
+
setMag(n: any, len: number): void;
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Calculates the angle a 2D vector makes with the positive x-axis.By convention, the positive x-axis has an angle of 0. Angles increase in
|
|
468
|
+
* the clockwise direction.If the vector was created with
|
|
469
|
+
* createVector(), `heading()` returns angles
|
|
470
|
+
* in the units of the current angleMode().The static version of `heading()`, as in `p5.Vector.heading(v)`, works the
|
|
471
|
+
* same way.
|
|
472
|
+
*/
|
|
473
|
+
heading(): number;
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Rotates a 2D vector to a specific angle without changing its magnitude.By convention, the positive x-axis has an angle of 0. Angles increase in
|
|
477
|
+
* the clockwise direction.If the vector was created with
|
|
478
|
+
* createVector(), `setHeading()` uses
|
|
479
|
+
* the units of the current angleMode().
|
|
480
|
+
*
|
|
481
|
+
* @param
|
|
482
|
+
* @param
|
|
483
|
+
*/
|
|
484
|
+
setHeading(a: any, angle: number): void;
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Rotates a 2D vector by an angle without changing its magnitude.By convention, the positive x-axis has an angle of 0. Angles increase in
|
|
488
|
+
* the clockwise direction.If the vector was created with
|
|
489
|
+
* createVector(), `rotate()` uses
|
|
490
|
+
* the units of the current angleMode().The static version of `rotate()`, as in `p5.Vector.rotate(v, PI)`,
|
|
491
|
+
* returns a new p5.Vector object and doesn't change
|
|
492
|
+
* the original.
|
|
493
|
+
*
|
|
494
|
+
* @param
|
|
495
|
+
* @param
|
|
496
|
+
*/
|
|
497
|
+
rotate(a: any, angle: number): void;
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Calculates the angle between two vectors.The angles returned are signed, which means that
|
|
501
|
+
* `v1.angleBetween(v2) === -v2.angleBetween(v1)`.If the vector was created with
|
|
502
|
+
* createVector(), `angleBetween()` returns
|
|
503
|
+
* angles in the units of the current
|
|
504
|
+
* angleMode().
|
|
505
|
+
*
|
|
506
|
+
* @param
|
|
507
|
+
* @param
|
|
508
|
+
*/
|
|
509
|
+
angleBetween(v: any, value: p5.Vector): number;
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Calculates new `x`, `y`, and `z` components that are proportionally the
|
|
513
|
+
* same distance between two vectors.The `amt` parameter is the amount to interpolate between the old vector and
|
|
514
|
+
* the new vector. 0.0 keeps all components equal to the old vector's, 0.5 is
|
|
515
|
+
* halfway between, and 1.0 sets all components equal to the new vector's.The static version of `lerp()`, as in `p5.Vector.lerp(v0, v1, 0.5)`,
|
|
516
|
+
* returns a new p5.Vector object and doesn't change
|
|
517
|
+
* the original.
|
|
518
|
+
*
|
|
519
|
+
* @param
|
|
520
|
+
* @param
|
|
521
|
+
* @param
|
|
522
|
+
* @param
|
|
523
|
+
*/
|
|
524
|
+
lerp(x: number, y: number, z: number, amt: number): void;
|
|
525
|
+
|
|
526
|
+
lerp(x: any, y: any, z: any, amt: number, v: p5.Vector): void;
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Calculates a new heading and magnitude that are between two vectors.The `amt` parameter is the amount to interpolate between the old vector and
|
|
530
|
+
* the new vector. 0.0 keeps the heading and magnitude equal to the old
|
|
531
|
+
* vector's, 0.5 sets them halfway between, and 1.0 sets the heading and
|
|
532
|
+
* magnitude equal to the new vector's.`slerp()` differs from lerp() because
|
|
533
|
+
* it interpolates magnitude. Calling `v0.slerp(v1, 0.5)` sets `v0`'s
|
|
534
|
+
* magnitude to a value halfway between its original magnitude and `v1`'s.
|
|
535
|
+
* Calling `v0.lerp(v1, 0.5)` makes no such guarantee.The static version of `slerp()`, as in `p5.Vector.slerp(v0, v1, 0.5)`,
|
|
536
|
+
* returns a new p5.Vector object and doesn't change
|
|
537
|
+
* the original.
|
|
538
|
+
*
|
|
539
|
+
* @param
|
|
540
|
+
* @param
|
|
541
|
+
*/
|
|
542
|
+
slerp(v: p5.Vector, amt: number): p5.Vector;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Reflects a vector about a line in 2D or a plane in 3D.The orientation of the line or plane is described by a normal vector that
|
|
546
|
+
* points away from the shape.The static version of `reflect()`, as in `p5.Vector.reflect(v, n)`,
|
|
547
|
+
* returns a new p5.Vector object and doesn't change
|
|
548
|
+
* the original.
|
|
549
|
+
*
|
|
550
|
+
* @param
|
|
551
|
+
*/
|
|
552
|
+
reflect(surfaceNormal: p5.Vector): void;
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Returns the vector's components as an array of numbers.
|
|
556
|
+
*/
|
|
557
|
+
array(): number[];
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Checks whether all the vector's components are equal to another vector's.`equals()` returns `true` if the vector's components are all the same as another
|
|
561
|
+
* vector's and `false` if not.The version of `equals()` with one parameter interprets it as another
|
|
562
|
+
* p5.Vector object.The version of `equals()` with multiple parameters interprets them as the
|
|
563
|
+
* components of another vector. Any missing parameters are assigned the value
|
|
564
|
+
* 0.The static version of `equals()`, as in `p5.Vector.equals(v0, v1)`,
|
|
565
|
+
* interprets both parameters as p5.Vector objects.
|
|
566
|
+
*
|
|
567
|
+
* @param
|
|
568
|
+
* @param
|
|
569
|
+
* @param
|
|
570
|
+
* @param
|
|
571
|
+
*/
|
|
572
|
+
equals(args: any[], x: number, y: number, z: number): boolean;
|
|
573
|
+
|
|
574
|
+
equals(args: any[], value: p5.Vector | Array): boolean;
|
|
575
|
+
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Calculates the distance between two points represented by vectors.A point's coordinates can be represented by the components of a vector
|
|
580
|
+
* that extends from the origin to the point.The static version of `dist()`, as in `p5.Vector.dist(v1, v2)`, is the same
|
|
581
|
+
* as calling `v1.dist(v2)`.Use dist() to calculate the distance between points
|
|
582
|
+
* using coordinates as in `dist(x1, y1, x2, y2)`.
|
|
583
|
+
*
|
|
584
|
+
* @submodule p5.Vector
|
|
585
|
+
* @param x, y, and z coordinates of a <a href="#/p5.Vector">p5.Vector</a>.
|
|
586
|
+
* @return distance.
|
|
587
|
+
* @example <div class="norender">
|
|
588
|
+
* <code>
|
|
589
|
+
* function setup() {
|
|
590
|
+
* createCanvas(100, 100);
|
|
591
|
+
*
|
|
592
|
+
* background(200);
|
|
593
|
+
*
|
|
594
|
+
* // Create p5.Vector objects.
|
|
595
|
+
* let v1 = createVector(1, 0);
|
|
596
|
+
* let v2 = createVector(0, 1);
|
|
597
|
+
*
|
|
598
|
+
* // Calculate the distance between them.
|
|
599
|
+
* let d = v1.dist(v2);
|
|
600
|
+
*
|
|
601
|
+
* // Prints "1.414..." to the console.
|
|
602
|
+
* print(d);
|
|
603
|
+
* }
|
|
604
|
+
* </code>
|
|
605
|
+
* </div>
|
|
606
|
+
*
|
|
607
|
+
* <div class="norender">
|
|
608
|
+
* <code>
|
|
609
|
+
* function setup() {
|
|
610
|
+
* createCanvas(100, 100);
|
|
611
|
+
*
|
|
612
|
+
* background(200);
|
|
613
|
+
*
|
|
614
|
+
* // Create p5.Vector objects.
|
|
615
|
+
* let v1 = createVector(1, 0);
|
|
616
|
+
* let v2 = createVector(0, 1);
|
|
617
|
+
*
|
|
618
|
+
* // Calculate the distance between them.
|
|
619
|
+
* let d = p5.Vector.dist(v1, v2);
|
|
620
|
+
*
|
|
621
|
+
* // Prints "1.414..." to the console.
|
|
622
|
+
* print(d);
|
|
623
|
+
* }
|
|
624
|
+
* </code>
|
|
625
|
+
* </div>
|
|
626
|
+
*
|
|
627
|
+
* <div>
|
|
628
|
+
* <code>
|
|
629
|
+
* function setup() {
|
|
630
|
+
* createCanvas(100, 100);
|
|
631
|
+
*
|
|
632
|
+
* describe('Three arrows drawn on a gray square. A red and a blue arrow extend from the top left. A purple arrow extends from the tip of the red arrow to the tip of the blue arrow. The number 36 is written in black near the purple arrow.');
|
|
633
|
+
* }
|
|
634
|
+
*
|
|
635
|
+
* function draw() {
|
|
636
|
+
* background(200);
|
|
637
|
+
*
|
|
638
|
+
* let origin = createVector(0, 0);
|
|
639
|
+
*
|
|
640
|
+
* // Draw the red arrow.
|
|
641
|
+
* let v1 = createVector(50, 50);
|
|
642
|
+
* drawArrow(origin, v1, 'red');
|
|
643
|
+
*
|
|
644
|
+
* // Draw the blue arrow.
|
|
645
|
+
* let v2 = createVector(20, 70);
|
|
646
|
+
* drawArrow(origin, v2, 'blue');
|
|
647
|
+
*
|
|
648
|
+
* // Purple arrow.
|
|
649
|
+
* let v3 = p5.Vector.sub(v2, v1);
|
|
650
|
+
* drawArrow(v1, v3, 'purple');
|
|
651
|
+
*
|
|
652
|
+
* // Style the text.
|
|
653
|
+
* textAlign(CENTER);
|
|
654
|
+
*
|
|
655
|
+
* // Display the magnitude. The same as floor(v3.mag());
|
|
656
|
+
* let m = floor(p5.Vector.dist(v1, v2));
|
|
657
|
+
* text(m, 50, 75);
|
|
658
|
+
* }
|
|
659
|
+
*
|
|
660
|
+
* // Draws an arrow between two vectors.
|
|
661
|
+
* function drawArrow(base, vec, myColor) {
|
|
662
|
+
* push();
|
|
663
|
+
* stroke(myColor);
|
|
664
|
+
* strokeWeight(3);
|
|
665
|
+
* fill(myColor);
|
|
666
|
+
* translate(base.x, base.y);
|
|
667
|
+
* line(0, 0, vec.x, vec.y);
|
|
668
|
+
* rotate(vec.heading());
|
|
669
|
+
* let arrowSize = 7;
|
|
670
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
671
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
672
|
+
* pop();
|
|
673
|
+
* }
|
|
674
|
+
* </code>
|
|
675
|
+
* </div>
|
|
676
|
+
*/
|
|
677
|
+
function dist(v: p5.Vector): number;
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Retrieves the value at the specified index from the vector.This method allows you to get the value of a specific component of the vector
|
|
681
|
+
* by providing its index. Think of the vector as a list of numbers, where each
|
|
682
|
+
* number represents a different direction (like x, y, or z). The index is just
|
|
683
|
+
* the position of the number in that list.For example, if you have a vector with values 10, 20, 30 the index 0 would
|
|
684
|
+
* give you the first value 10, index 1 would give you the second value 20,
|
|
685
|
+
* and so on.
|
|
686
|
+
*
|
|
687
|
+
* @param The position of the value you want to get from the vector.
|
|
688
|
+
* @returns The value at the specified position in the vector.
|
|
689
|
+
* @throws Will throw an error if the index is out of bounds, meaning if you try to
|
|
690
|
+
* get a value from a position that doesn't exist in the vector.
|
|
691
|
+
*/
|
|
692
|
+
function getValue(index: number): number;
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* Sets the value at the specified index of the vector.This method allows you to change a specific component of the vector by providing its index and the new value you want to set.
|
|
696
|
+
* Think of the vector as a list of numbers, where each number represents a different direction (like x, y, or z).
|
|
697
|
+
* The index is just the position of the number in that list.For example, if you have a vector with values [0, 20, 30], and you want to change the second value (20) to 50,
|
|
698
|
+
* you would use this method with index 1 (since indexes start at 0) and value 50.
|
|
699
|
+
*
|
|
700
|
+
* @param The position in the vector where you want to set the new value.
|
|
701
|
+
* @param The new value you want to set at the specified position.
|
|
702
|
+
* @throws Will throw an error if the index is outside the bounds of the vector, meaning if you try to set a value at a position that doesn't exist in the vector.
|
|
703
|
+
*/
|
|
704
|
+
function setValue(index: number, value: number): void;
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Returns a string representation of a vector.Calling `toString()` is useful for printing vectors to the console while
|
|
708
|
+
* debugging.
|
|
709
|
+
*
|
|
710
|
+
* @return string representation of the vector.
|
|
711
|
+
* @example <div class = "norender">
|
|
712
|
+
* <code>
|
|
713
|
+
* function setup() {
|
|
714
|
+
* let v = createVector(20, 30);
|
|
715
|
+
*
|
|
716
|
+
* // Prints 'p5.Vector Object : [20, 30, 0]'.
|
|
717
|
+
* print(v.toString());
|
|
718
|
+
* }
|
|
719
|
+
* </code>
|
|
720
|
+
* </div>
|
|
721
|
+
*/
|
|
722
|
+
function toString(): string;
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Sets the vector's `x`, `y`, and `z` components.`set()` can use separate numbers, as in `v.set(1, 2, 3)`, a
|
|
726
|
+
* p5.Vector object, as in `v.set(v2)`, or an
|
|
727
|
+
* array of numbers, as in `v.set([1, 2, 3])`.If a value isn't provided for a component, it will be set to 0. For
|
|
728
|
+
* example, `v.set(4, 5)` sets `v.x` to 4, `v.y` to 5, and `v.z` to 0.
|
|
729
|
+
* Calling `set()` with no arguments, as in `v.set()`, sets all the vector's
|
|
730
|
+
* components to 0.
|
|
731
|
+
*
|
|
732
|
+
* @param x component of the vector.
|
|
733
|
+
* @param y component of the vector.
|
|
734
|
+
* @param z component of the vector.
|
|
735
|
+
* @example <div>
|
|
736
|
+
* <code>
|
|
737
|
+
* function setup() {
|
|
738
|
+
* createCanvas(100, 100);
|
|
739
|
+
*
|
|
740
|
+
* background(200);
|
|
741
|
+
*
|
|
742
|
+
* // Style the points.
|
|
743
|
+
* strokeWeight(5);
|
|
744
|
+
*
|
|
745
|
+
* // Top left.
|
|
746
|
+
* let pos = createVector(25, 25);
|
|
747
|
+
* point(pos);
|
|
748
|
+
*
|
|
749
|
+
* // Top right.
|
|
750
|
+
* // set() with numbers.
|
|
751
|
+
* pos.set(75, 25);
|
|
752
|
+
* point(pos);
|
|
753
|
+
*
|
|
754
|
+
* // Bottom right.
|
|
755
|
+
* // set() with a p5.Vector.
|
|
756
|
+
* let p2 = createVector(75, 75);
|
|
757
|
+
* pos.set(p2);
|
|
758
|
+
* point(pos);
|
|
759
|
+
*
|
|
760
|
+
* // Bottom left.
|
|
761
|
+
* // set() with an array.
|
|
762
|
+
* let arr = [25, 75];
|
|
763
|
+
* pos.set(arr);
|
|
764
|
+
* point(pos);
|
|
765
|
+
*
|
|
766
|
+
* describe('Four black dots arranged in a square on a gray background.');
|
|
767
|
+
* }
|
|
768
|
+
* </code>
|
|
769
|
+
* </div>
|
|
770
|
+
*/
|
|
771
|
+
function set(...args: any[], x?: number, y?: number, z?: number): void;
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* @param vector to set.
|
|
775
|
+
*/
|
|
776
|
+
function set(...args: any[], value: p5.Vector | number[]): void;
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Returns a copy of the p5.Vector object.
|
|
780
|
+
*
|
|
781
|
+
* @return copy of the <a href="#/p5.Vector">p5.Vector</a> object.
|
|
782
|
+
* @example <div>
|
|
783
|
+
* <code>
|
|
784
|
+
* function setup() {
|
|
785
|
+
* createCanvas(100 ,100);
|
|
786
|
+
*
|
|
787
|
+
* background(200);
|
|
788
|
+
*
|
|
789
|
+
* // Create a p5.Vector object.
|
|
790
|
+
* let pos = createVector(50, 50);
|
|
791
|
+
*
|
|
792
|
+
* // Make a copy.
|
|
793
|
+
* let pc = pos.copy();
|
|
794
|
+
*
|
|
795
|
+
* // Draw the point.
|
|
796
|
+
* strokeWeight(5);
|
|
797
|
+
* point(pc);
|
|
798
|
+
*
|
|
799
|
+
* describe('A black point drawn in the middle of a gray square.');
|
|
800
|
+
* }
|
|
801
|
+
* </code>
|
|
802
|
+
* </div>
|
|
803
|
+
*/
|
|
804
|
+
function copy(): p5.Vector;
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Adds to a vector's components.`add()` can use separate numbers, as in `v.add(1, 2, 3)`,
|
|
808
|
+
* another p5.Vector object, as in `v.add(v2)`, or
|
|
809
|
+
* an array of numbers, as in `v.add([1, 2, 3])`.If a value isn't provided for a component, it won't change. For
|
|
810
|
+
* example, `v.add(4, 5)` adds 4 to `v.x`, 5 to `v.y`, and 0 to `v.z`.
|
|
811
|
+
* Calling `add()` with no arguments, as in `v.add()`, has no effect.This method supports N-dimensional vectors.The static version of `add()`, as in `p5.Vector.add(v2, v1)`, returns a new
|
|
812
|
+
* p5.Vector object and doesn't change the
|
|
813
|
+
* originals.
|
|
814
|
+
*
|
|
815
|
+
* @param x component of the vector to be added or an array of components.
|
|
816
|
+
* @param y component of the vector to be added.
|
|
817
|
+
* @param z component of the vector to be added.
|
|
818
|
+
* @example <div>
|
|
819
|
+
* <code>
|
|
820
|
+
* function setup() {
|
|
821
|
+
* createCanvas(100, 100);
|
|
822
|
+
*
|
|
823
|
+
* background(200);
|
|
824
|
+
*
|
|
825
|
+
* // Style the points.
|
|
826
|
+
* strokeWeight(5);
|
|
827
|
+
*
|
|
828
|
+
* // Top left.
|
|
829
|
+
* let pos = createVector(25, 25);
|
|
830
|
+
* point(pos);
|
|
831
|
+
*
|
|
832
|
+
* // Top right.
|
|
833
|
+
* // Add numbers.
|
|
834
|
+
* pos.add(50, 0);
|
|
835
|
+
* point(pos);
|
|
836
|
+
*
|
|
837
|
+
* // Bottom right.
|
|
838
|
+
* // Add a p5.Vector.
|
|
839
|
+
* let p2 = createVector(0, 50);
|
|
840
|
+
* pos.add(p2);
|
|
841
|
+
* point(pos);
|
|
842
|
+
*
|
|
843
|
+
* // Bottom left.
|
|
844
|
+
* // Add an array.
|
|
845
|
+
* let arr = [-50, 0];
|
|
846
|
+
* pos.add(arr);
|
|
847
|
+
* point(pos);
|
|
848
|
+
*
|
|
849
|
+
* describe('Four black dots arranged in a square on a gray background.');
|
|
850
|
+
* }
|
|
851
|
+
* </code>
|
|
852
|
+
* </div>
|
|
853
|
+
*
|
|
854
|
+
* <div>
|
|
855
|
+
* <code>
|
|
856
|
+
* function setup() {
|
|
857
|
+
* createCanvas(100, 100);
|
|
858
|
+
*
|
|
859
|
+
* background(200);
|
|
860
|
+
*
|
|
861
|
+
* // Top left.
|
|
862
|
+
* let p1 = createVector(25, 25);
|
|
863
|
+
*
|
|
864
|
+
* // Center.
|
|
865
|
+
* let p2 = createVector(50, 50);
|
|
866
|
+
*
|
|
867
|
+
* // Bottom right.
|
|
868
|
+
* // Add p1 and p2.
|
|
869
|
+
* let p3 = p5.Vector.add(p1, p2);
|
|
870
|
+
*
|
|
871
|
+
* // Draw the points.
|
|
872
|
+
* strokeWeight(5);
|
|
873
|
+
* point(p1);
|
|
874
|
+
* point(p2);
|
|
875
|
+
* point(p3);
|
|
876
|
+
*
|
|
877
|
+
* describe('Three black dots in a diagonal line from top left to bottom right.');
|
|
878
|
+
* }
|
|
879
|
+
* </code>
|
|
880
|
+
* </div>
|
|
881
|
+
*
|
|
882
|
+
* <div>
|
|
883
|
+
* <code>
|
|
884
|
+
* function setup() {
|
|
885
|
+
* createCanvas(100, 100);
|
|
886
|
+
*
|
|
887
|
+
* describe('Three arrows drawn on a gray square. A red arrow extends from the top left corner to the center. A blue arrow extends from the tip of the red arrow. A purple arrow extends from the origin to the tip of the blue arrow.');
|
|
888
|
+
* }
|
|
889
|
+
*
|
|
890
|
+
* function draw() {
|
|
891
|
+
* background(200);
|
|
892
|
+
*
|
|
893
|
+
* let origin = createVector(0, 0);
|
|
894
|
+
*
|
|
895
|
+
* // Draw the red arrow.
|
|
896
|
+
* let v1 = createVector(50, 50);
|
|
897
|
+
* drawArrow(origin, v1, 'red');
|
|
898
|
+
*
|
|
899
|
+
* // Draw the blue arrow.
|
|
900
|
+
* let v2 = createVector(-30, 20);
|
|
901
|
+
* drawArrow(v1, v2, 'blue');
|
|
902
|
+
*
|
|
903
|
+
* // Purple arrow.
|
|
904
|
+
* let v3 = p5.Vector.add(v1, v2);
|
|
905
|
+
* drawArrow(origin, v3, 'purple');
|
|
906
|
+
* }
|
|
907
|
+
*
|
|
908
|
+
* // Draws an arrow between two vectors.
|
|
909
|
+
* function drawArrow(base, vec, myColor) {
|
|
910
|
+
* push();
|
|
911
|
+
* stroke(myColor);
|
|
912
|
+
* strokeWeight(3);
|
|
913
|
+
* fill(myColor);
|
|
914
|
+
* translate(base.x, base.y);
|
|
915
|
+
* line(0, 0, vec.x, vec.y);
|
|
916
|
+
* rotate(vec.heading());
|
|
917
|
+
* let arrowSize = 7;
|
|
918
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
919
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
920
|
+
* pop();
|
|
921
|
+
* }
|
|
922
|
+
* </code>
|
|
923
|
+
* </div>
|
|
924
|
+
*/
|
|
925
|
+
function add(...args: any[], x: number | Array, y?: number, z?: number): void;
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* @param The vector to add
|
|
929
|
+
*/
|
|
930
|
+
function add(...args: any[], value: p5.Vector | number[]): void;
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* Performs modulo (remainder) division with a vector's `x`, `y`, and `z`
|
|
934
|
+
* components.`rem()` can use separate numbers, as in `v.rem(1, 2, 3)`,
|
|
935
|
+
* another p5.Vector object, as in `v.rem(v2)`, or
|
|
936
|
+
* an array of numbers, as in `v.rem([1, 2, 3])`.If only one value is provided, as in `v.rem(2)`, then all the components
|
|
937
|
+
* will be set to their values modulo 2. If two values are provided, as in
|
|
938
|
+
* `v.rem(2, 3)`, then `v.z` won't change. Calling `rem()` with no
|
|
939
|
+
* arguments, as in `v.rem()`, has no effect.The static version of `rem()`, as in `p5.Vector.rem(v2, v1)`, returns a
|
|
940
|
+
* new p5.Vector object and doesn't change the
|
|
941
|
+
* originals.
|
|
942
|
+
*
|
|
943
|
+
* @param x component of divisor vector.
|
|
944
|
+
* @param y component of divisor vector.
|
|
945
|
+
* @param z component of divisor vector.
|
|
946
|
+
* @example <div class='norender'>
|
|
947
|
+
* <code>
|
|
948
|
+
* function setup() {
|
|
949
|
+
* // Create a p5.Vector object.
|
|
950
|
+
* let v = createVector(3, 4, 5);
|
|
951
|
+
*
|
|
952
|
+
* // Divide numbers.
|
|
953
|
+
* v.rem(2);
|
|
954
|
+
*
|
|
955
|
+
* // Prints 'p5.Vector Object : [1, 0, 1]'.
|
|
956
|
+
* print(v.toString());
|
|
957
|
+
* }
|
|
958
|
+
* </code>
|
|
959
|
+
* </div>
|
|
960
|
+
*
|
|
961
|
+
* <div class='norender'>
|
|
962
|
+
* <code>
|
|
963
|
+
* function setup() {
|
|
964
|
+
* // Create a p5.Vector object.
|
|
965
|
+
* let v = createVector(3, 4, 5);
|
|
966
|
+
*
|
|
967
|
+
* // Divide numbers.
|
|
968
|
+
* v.rem(2, 3);
|
|
969
|
+
*
|
|
970
|
+
* // Prints 'p5.Vector Object : [1, 1, 5]'.
|
|
971
|
+
* print(v.toString());
|
|
972
|
+
* }
|
|
973
|
+
* </code>
|
|
974
|
+
* </div>
|
|
975
|
+
*
|
|
976
|
+
* <div class='norender'>
|
|
977
|
+
* <code>
|
|
978
|
+
* function setup() {
|
|
979
|
+
* // Create a p5.Vector object.
|
|
980
|
+
* let v = createVector(3, 4, 5);
|
|
981
|
+
*
|
|
982
|
+
* // Divide numbers.
|
|
983
|
+
* v.rem(2, 3, 4);
|
|
984
|
+
*
|
|
985
|
+
* // Prints 'p5.Vector Object : [1, 1, 1]'.
|
|
986
|
+
* print(v.toString());
|
|
987
|
+
* }
|
|
988
|
+
* </code>
|
|
989
|
+
* </div>
|
|
990
|
+
*
|
|
991
|
+
* <div class='norender'>
|
|
992
|
+
* <code>
|
|
993
|
+
* function setup() {
|
|
994
|
+
* // Create p5.Vector objects.
|
|
995
|
+
* let v1 = createVector(3, 4, 5);
|
|
996
|
+
* let v2 = createVector(2, 3, 4);
|
|
997
|
+
*
|
|
998
|
+
* // Divide a p5.Vector.
|
|
999
|
+
* v1.rem(v2);
|
|
1000
|
+
*
|
|
1001
|
+
* // Prints 'p5.Vector Object : [1, 1, 1]'.
|
|
1002
|
+
* print(v1.toString());
|
|
1003
|
+
* }
|
|
1004
|
+
* </code>
|
|
1005
|
+
* </div>
|
|
1006
|
+
*
|
|
1007
|
+
* <div class='norender'>
|
|
1008
|
+
* <code>
|
|
1009
|
+
* function setup() {
|
|
1010
|
+
* // Create a p5.Vector object.
|
|
1011
|
+
* let v = createVector(3, 4, 5);
|
|
1012
|
+
*
|
|
1013
|
+
* // Divide an array.
|
|
1014
|
+
* let arr = [2, 3, 4];
|
|
1015
|
+
* v.rem(arr);
|
|
1016
|
+
*
|
|
1017
|
+
* // Prints 'p5.Vector Object : [1, 1, 1]'.
|
|
1018
|
+
* print(v.toString());
|
|
1019
|
+
* }
|
|
1020
|
+
* </code>
|
|
1021
|
+
* </div>
|
|
1022
|
+
*
|
|
1023
|
+
* <div class="norender">
|
|
1024
|
+
* <code>
|
|
1025
|
+
* function setup() {
|
|
1026
|
+
* // Create p5.Vector objects.
|
|
1027
|
+
* let v1 = createVector(3, 4, 5);
|
|
1028
|
+
* let v2 = createVector(2, 3, 4);
|
|
1029
|
+
*
|
|
1030
|
+
* // Divide without modifying the original vectors.
|
|
1031
|
+
* let v3 = p5.Vector.rem(v1, v2);
|
|
1032
|
+
*
|
|
1033
|
+
* // Prints 'p5.Vector Object : [1, 1, 1]'.
|
|
1034
|
+
* print(v3.toString());
|
|
1035
|
+
* }
|
|
1036
|
+
* </code>
|
|
1037
|
+
* </div>
|
|
1038
|
+
*/
|
|
1039
|
+
function rem(x: number, y: number, z: number): void;
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* @param divisor vector.
|
|
1043
|
+
*/
|
|
1044
|
+
function rem(x: any, y: any, z: any, value: p5.Vector | number[]): void;
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Subtracts from a vector's `x`, `y`, and `z` components.`sub()` can use separate numbers, as in `v.sub(1, 2, 3)`, another
|
|
1048
|
+
* p5.Vector object, as in `v.sub(v2)`, or an array
|
|
1049
|
+
* of numbers, as in `v.sub([1, 2, 3])`.If a value isn't provided for a component, it won't change. For
|
|
1050
|
+
* example, `v.sub(4, 5)` subtracts 4 from `v.x`, 5 from `v.y`, and 0 from `v.z`.
|
|
1051
|
+
* Calling `sub()` with no arguments, as in `v.sub()`, has no effect.The static version of `sub()`, as in `p5.Vector.sub(v2, v1)`, returns a new
|
|
1052
|
+
* p5.Vector object and doesn't change the
|
|
1053
|
+
* originals.
|
|
1054
|
+
*
|
|
1055
|
+
* @param x component of the vector to subtract.
|
|
1056
|
+
* @param y component of the vector to subtract.
|
|
1057
|
+
* @param z component of the vector to subtract.
|
|
1058
|
+
* @example <div>
|
|
1059
|
+
* <code>
|
|
1060
|
+
* function setup() {
|
|
1061
|
+
* createCanvas(100, 100);
|
|
1062
|
+
*
|
|
1063
|
+
* background(200);
|
|
1064
|
+
*
|
|
1065
|
+
* // Style the points.
|
|
1066
|
+
* strokeWeight(5);
|
|
1067
|
+
*
|
|
1068
|
+
* // Bottom right.
|
|
1069
|
+
* let pos = createVector(75, 75);
|
|
1070
|
+
* point(pos);
|
|
1071
|
+
*
|
|
1072
|
+
* // Top right.
|
|
1073
|
+
* // Subtract numbers.
|
|
1074
|
+
* pos.sub(0, 50);
|
|
1075
|
+
* point(pos);
|
|
1076
|
+
*
|
|
1077
|
+
* // Top left.
|
|
1078
|
+
* // Subtract a p5.Vector.
|
|
1079
|
+
* let p2 = createVector(50, 0);
|
|
1080
|
+
* pos.sub(p2);
|
|
1081
|
+
* point(pos);
|
|
1082
|
+
*
|
|
1083
|
+
* // Bottom left.
|
|
1084
|
+
* // Subtract an array.
|
|
1085
|
+
* let arr = [0, -50];
|
|
1086
|
+
* pos.sub(arr);
|
|
1087
|
+
* point(pos);
|
|
1088
|
+
*
|
|
1089
|
+
* describe('Four black dots arranged in a square on a gray background.');
|
|
1090
|
+
* }
|
|
1091
|
+
* </code>
|
|
1092
|
+
* </div>
|
|
1093
|
+
*
|
|
1094
|
+
* <div>
|
|
1095
|
+
* <code>
|
|
1096
|
+
* function setup() {
|
|
1097
|
+
* createCanvas(100, 100);
|
|
1098
|
+
*
|
|
1099
|
+
* background(200);
|
|
1100
|
+
*
|
|
1101
|
+
* // Create p5.Vector objects.
|
|
1102
|
+
* let p1 = createVector(75, 75);
|
|
1103
|
+
* let p2 = createVector(50, 50);
|
|
1104
|
+
*
|
|
1105
|
+
* // Subtract with modifying the original vectors.
|
|
1106
|
+
* let p3 = p5.Vector.sub(p1, p2);
|
|
1107
|
+
*
|
|
1108
|
+
* // Draw the points.
|
|
1109
|
+
* strokeWeight(5);
|
|
1110
|
+
* point(p1);
|
|
1111
|
+
* point(p2);
|
|
1112
|
+
* point(p3);
|
|
1113
|
+
*
|
|
1114
|
+
* describe('Three black dots in a diagonal line from top left to bottom right.');
|
|
1115
|
+
* }
|
|
1116
|
+
* </code>
|
|
1117
|
+
* </div>
|
|
1118
|
+
*
|
|
1119
|
+
* <div>
|
|
1120
|
+
* <code>
|
|
1121
|
+
* function setup() {
|
|
1122
|
+
* createCanvas(100, 100);
|
|
1123
|
+
*
|
|
1124
|
+
* describe('Three arrows drawn on a gray square. A red and a blue arrow extend from the top left. A purple arrow extends from the tip of the red arrow to the tip of the blue arrow.');
|
|
1125
|
+
* }
|
|
1126
|
+
*
|
|
1127
|
+
* function draw() {
|
|
1128
|
+
* background(200);
|
|
1129
|
+
*
|
|
1130
|
+
* let origin = createVector(0, 0);
|
|
1131
|
+
*
|
|
1132
|
+
* // Draw the red arrow.
|
|
1133
|
+
* let v1 = createVector(50, 50);
|
|
1134
|
+
* drawArrow(origin, v1, 'red');
|
|
1135
|
+
*
|
|
1136
|
+
* // Draw the blue arrow.
|
|
1137
|
+
* let v2 = createVector(20, 70);
|
|
1138
|
+
* drawArrow(origin, v2, 'blue');
|
|
1139
|
+
*
|
|
1140
|
+
* // Purple arrow.
|
|
1141
|
+
* let v3 = p5.Vector.sub(v2, v1);
|
|
1142
|
+
* drawArrow(v1, v3, 'purple');
|
|
1143
|
+
* }
|
|
1144
|
+
*
|
|
1145
|
+
* // Draws an arrow between two vectors.
|
|
1146
|
+
* function drawArrow(base, vec, myColor) {
|
|
1147
|
+
* push();
|
|
1148
|
+
* stroke(myColor);
|
|
1149
|
+
* strokeWeight(3);
|
|
1150
|
+
* fill(myColor);
|
|
1151
|
+
* translate(base.x, base.y);
|
|
1152
|
+
* line(0, 0, vec.x, vec.y);
|
|
1153
|
+
* rotate(vec.heading());
|
|
1154
|
+
* let arrowSize = 7;
|
|
1155
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1156
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1157
|
+
* pop();
|
|
1158
|
+
* }
|
|
1159
|
+
* </code>
|
|
1160
|
+
* </div>
|
|
1161
|
+
*/
|
|
1162
|
+
function sub(...args: any[], x: number, y?: number, z?: number): void;
|
|
1163
|
+
|
|
1164
|
+
/**
|
|
1165
|
+
* @param the vector to subtract
|
|
1166
|
+
*/
|
|
1167
|
+
function sub(...args: any[], value: p5.Vector | number[]): void;
|
|
1168
|
+
|
|
1169
|
+
/**
|
|
1170
|
+
* @param number to multiply with the x component of the vector.
|
|
1171
|
+
* @param number to multiply with the y component of the vector.
|
|
1172
|
+
* @param number to multiply with the z component of the vector.
|
|
1173
|
+
*/
|
|
1174
|
+
function mult(...args: any[], x: number, y: number, z?: number): void;
|
|
1175
|
+
|
|
1176
|
+
/**
|
|
1177
|
+
* @param array to multiply with the components of the vector.
|
|
1178
|
+
*/
|
|
1179
|
+
function mult(...args: any[], arr: number[]): void;
|
|
1180
|
+
|
|
1181
|
+
/**
|
|
1182
|
+
* @param vector to multiply with the components of the original vector.
|
|
1183
|
+
*/
|
|
1184
|
+
function mult(...args: any[], v: p5.Vector): void;
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* Divides a vector's `x`, `y`, and `z` components.`div()` can use separate numbers, as in `v.div(1, 2, 3)`, another
|
|
1188
|
+
* p5.Vector object, as in `v.div(v2)`, or an array
|
|
1189
|
+
* of numbers, as in `v.div([1, 2, 3])`.If only one value is provided, as in `v.div(2)`, then all the components
|
|
1190
|
+
* will be divided by 2. If a value isn't provided for a component, it
|
|
1191
|
+
* won't change. For example, `v.div(4, 5)` divides `v.x` by, `v.y` by 5,
|
|
1192
|
+
* and `v.z` by 1. Calling `div()` with no arguments, as in `v.div()`, has
|
|
1193
|
+
* no effect.The static version of `div()`, as in `p5.Vector.div(v, 2)`, returns a new
|
|
1194
|
+
* p5.Vector object and doesn't change the
|
|
1195
|
+
* originals.
|
|
1196
|
+
*
|
|
1197
|
+
* @param The number to divide the vector by
|
|
1198
|
+
* @example <div>
|
|
1199
|
+
* <code>
|
|
1200
|
+
* function setup() {
|
|
1201
|
+
* createCanvas(100, 100);
|
|
1202
|
+
*
|
|
1203
|
+
* background(200);
|
|
1204
|
+
*
|
|
1205
|
+
* // Style the points.
|
|
1206
|
+
* strokeWeight(5);
|
|
1207
|
+
*
|
|
1208
|
+
* // Center.
|
|
1209
|
+
* let p = createVector(50, 50);
|
|
1210
|
+
* point(p);
|
|
1211
|
+
*
|
|
1212
|
+
* // Top-left.
|
|
1213
|
+
* // Divide p.x / 2 and p.y / 2
|
|
1214
|
+
* p.div(2);
|
|
1215
|
+
* point(p);
|
|
1216
|
+
*
|
|
1217
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.');
|
|
1218
|
+
* }
|
|
1219
|
+
* </code>
|
|
1220
|
+
* </div>
|
|
1221
|
+
*
|
|
1222
|
+
* <div>
|
|
1223
|
+
* <code>
|
|
1224
|
+
* function setup() {
|
|
1225
|
+
* createCanvas(100, 100);
|
|
1226
|
+
*
|
|
1227
|
+
* background(200);
|
|
1228
|
+
*
|
|
1229
|
+
* // Style the points.
|
|
1230
|
+
* strokeWeight(5);
|
|
1231
|
+
*
|
|
1232
|
+
* // Bottom-right.
|
|
1233
|
+
* let p = createVector(50, 75);
|
|
1234
|
+
* point(p);
|
|
1235
|
+
*
|
|
1236
|
+
* // Top-left.
|
|
1237
|
+
* // Divide p.x / 2 and p.y / 3
|
|
1238
|
+
* p.div(2, 3);
|
|
1239
|
+
* point(p);
|
|
1240
|
+
*
|
|
1241
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
1242
|
+
* }
|
|
1243
|
+
* </code>
|
|
1244
|
+
* </div>
|
|
1245
|
+
*
|
|
1246
|
+
* <div>
|
|
1247
|
+
* <code>
|
|
1248
|
+
* function setup() {
|
|
1249
|
+
* createCanvas(100, 100);
|
|
1250
|
+
*
|
|
1251
|
+
* background(200);
|
|
1252
|
+
*
|
|
1253
|
+
* // Style the points.
|
|
1254
|
+
* strokeWeight(5);
|
|
1255
|
+
*
|
|
1256
|
+
* // Bottom-right.
|
|
1257
|
+
* let p = createVector(50, 75);
|
|
1258
|
+
* point(p);
|
|
1259
|
+
*
|
|
1260
|
+
* // Top-left.
|
|
1261
|
+
* // Divide p.x / 2 and p.y / 3
|
|
1262
|
+
* let arr = [2, 3];
|
|
1263
|
+
* p.div(arr);
|
|
1264
|
+
* point(p);
|
|
1265
|
+
*
|
|
1266
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
1267
|
+
* }
|
|
1268
|
+
* </code>
|
|
1269
|
+
* </div>
|
|
1270
|
+
*
|
|
1271
|
+
* <div>
|
|
1272
|
+
* <code>
|
|
1273
|
+
* function setup() {
|
|
1274
|
+
* createCanvas(100, 100);
|
|
1275
|
+
*
|
|
1276
|
+
* background(200);
|
|
1277
|
+
*
|
|
1278
|
+
* // Style the points.
|
|
1279
|
+
* strokeWeight(5);
|
|
1280
|
+
*
|
|
1281
|
+
* // Bottom-right.
|
|
1282
|
+
* let p = createVector(50, 75);
|
|
1283
|
+
* point(p);
|
|
1284
|
+
*
|
|
1285
|
+
* // Top-left.
|
|
1286
|
+
* // Divide p.x / 2 and p.y / 3
|
|
1287
|
+
* let p2 = createVector(2, 3);
|
|
1288
|
+
* p.div(p2);
|
|
1289
|
+
* point(p);
|
|
1290
|
+
*
|
|
1291
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
1292
|
+
* }
|
|
1293
|
+
* </code>
|
|
1294
|
+
* </div>
|
|
1295
|
+
*
|
|
1296
|
+
* <div>
|
|
1297
|
+
* <code>
|
|
1298
|
+
* function setup() {
|
|
1299
|
+
* createCanvas(100, 100);
|
|
1300
|
+
*
|
|
1301
|
+
* background(200);
|
|
1302
|
+
*
|
|
1303
|
+
* // Style the points.
|
|
1304
|
+
* strokeWeight(5);
|
|
1305
|
+
*
|
|
1306
|
+
* // Bottom-right.
|
|
1307
|
+
* let p = createVector(50, 75);
|
|
1308
|
+
* point(p);
|
|
1309
|
+
*
|
|
1310
|
+
* // Top-left.
|
|
1311
|
+
* // Create a new p5.Vector with
|
|
1312
|
+
* // p3.x = p.x / p2.x
|
|
1313
|
+
* // p3.y = p.y / p2.y
|
|
1314
|
+
* let p2 = createVector(2, 3);
|
|
1315
|
+
* let p3 = p5.Vector.div(p, p2);
|
|
1316
|
+
* point(p3);
|
|
1317
|
+
*
|
|
1318
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
1319
|
+
* }
|
|
1320
|
+
* </code>
|
|
1321
|
+
* </div>
|
|
1322
|
+
*
|
|
1323
|
+
* <div>
|
|
1324
|
+
* <code>
|
|
1325
|
+
* function draw() {
|
|
1326
|
+
* background(200);
|
|
1327
|
+
*
|
|
1328
|
+
* let origin = createVector(0, 0);
|
|
1329
|
+
*
|
|
1330
|
+
* // Draw the red arrow.
|
|
1331
|
+
* let v1 = createVector(50, 50);
|
|
1332
|
+
* drawArrow(origin, v1, 'red');
|
|
1333
|
+
*
|
|
1334
|
+
* // Draw the blue arrow.
|
|
1335
|
+
* let v2 = p5.Vector.div(v1, 2);
|
|
1336
|
+
* drawArrow(origin, v2, 'blue');
|
|
1337
|
+
*
|
|
1338
|
+
* describe('Two arrows extending from the top left corner. The blue arrow is half the length of the red arrow.');
|
|
1339
|
+
* }
|
|
1340
|
+
*
|
|
1341
|
+
* // Draws an arrow between two vectors.
|
|
1342
|
+
* function drawArrow(base, vec, myColor) {
|
|
1343
|
+
* push();
|
|
1344
|
+
* stroke(myColor);
|
|
1345
|
+
* strokeWeight(3);
|
|
1346
|
+
* fill(myColor);
|
|
1347
|
+
* translate(base.x, base.y);
|
|
1348
|
+
* line(0, 0, vec.x, vec.y);
|
|
1349
|
+
* rotate(vec.heading());
|
|
1350
|
+
* let arrowSize = 7;
|
|
1351
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1352
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1353
|
+
* pop();
|
|
1354
|
+
* }
|
|
1355
|
+
* </code>
|
|
1356
|
+
* </div>
|
|
1357
|
+
*/
|
|
1358
|
+
function div(...args: any[], n: number): void;
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* @param number to divide with the x component of the vector.
|
|
1362
|
+
* @param number to divide with the y component of the vector.
|
|
1363
|
+
* @param number to divide with the z component of the vector.
|
|
1364
|
+
*/
|
|
1365
|
+
function div(...args: any[], x: number, y: number, z?: number): void;
|
|
1366
|
+
|
|
1367
|
+
/**
|
|
1368
|
+
* @param array to divide the components of the vector by.
|
|
1369
|
+
*/
|
|
1370
|
+
function div(...args: any[], arr: number[]): void;
|
|
1371
|
+
|
|
1372
|
+
/**
|
|
1373
|
+
* @param vector to divide the components of the original vector by.
|
|
1374
|
+
*/
|
|
1375
|
+
function div(...args: any[], v: p5.Vector): void;
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* Calculates the magnitude (length) of the vector.Use mag() to calculate the magnitude of a 2D vector
|
|
1379
|
+
* using components as in `mag(x, y)`.
|
|
1380
|
+
*
|
|
1381
|
+
* @return magnitude of the vector.
|
|
1382
|
+
* @example <div>
|
|
1383
|
+
* <code>
|
|
1384
|
+
* function setup() {
|
|
1385
|
+
* createCanvas(100, 100);
|
|
1386
|
+
*
|
|
1387
|
+
* background(200);
|
|
1388
|
+
*
|
|
1389
|
+
* // Create a p5.Vector object.
|
|
1390
|
+
* let p = createVector(30, 40);
|
|
1391
|
+
*
|
|
1392
|
+
* // Draw a line from the origin.
|
|
1393
|
+
* line(0, 0, p.x, p.y);
|
|
1394
|
+
*
|
|
1395
|
+
* // Style the text.
|
|
1396
|
+
* textAlign(CENTER);
|
|
1397
|
+
* textSize(16);
|
|
1398
|
+
*
|
|
1399
|
+
* // Display the vector's magnitude.
|
|
1400
|
+
* let m = p.mag();
|
|
1401
|
+
* text(m, p.x, p.y);
|
|
1402
|
+
*
|
|
1403
|
+
* describe('A diagonal black line extends from the top left corner of a gray square. The number 50 is written at the end of the line.');
|
|
1404
|
+
* }
|
|
1405
|
+
* </code>
|
|
1406
|
+
* </div>
|
|
1407
|
+
*/
|
|
1408
|
+
function mag(): number;
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Calculates the magnitude (length) of the vector squared.
|
|
1412
|
+
*
|
|
1413
|
+
* @return squared magnitude of the vector.
|
|
1414
|
+
* @example <div>
|
|
1415
|
+
* <code>
|
|
1416
|
+
* function setup() {
|
|
1417
|
+
* createCanvas(100, 100);
|
|
1418
|
+
*
|
|
1419
|
+
* background(200);
|
|
1420
|
+
*
|
|
1421
|
+
* // Create a p5.Vector object.
|
|
1422
|
+
* let p = createVector(30, 40);
|
|
1423
|
+
*
|
|
1424
|
+
* // Draw a line from the origin.
|
|
1425
|
+
* line(0, 0, p.x, p.y);
|
|
1426
|
+
*
|
|
1427
|
+
* // Style the text.
|
|
1428
|
+
* textAlign(CENTER);
|
|
1429
|
+
* textSize(16);
|
|
1430
|
+
*
|
|
1431
|
+
* // Display the vector's magnitude squared.
|
|
1432
|
+
* let m = p.magSq();
|
|
1433
|
+
* text(m, p.x, p.y);
|
|
1434
|
+
*
|
|
1435
|
+
* describe('A diagonal black line extends from the top left corner of a gray square. The number 2500 is written at the end of the line.');
|
|
1436
|
+
* }
|
|
1437
|
+
* </code>
|
|
1438
|
+
* </div>
|
|
1439
|
+
*/
|
|
1440
|
+
function magSq(): number;
|
|
1441
|
+
|
|
1442
|
+
/**
|
|
1443
|
+
* Calculates the dot product of two vectors.The dot product is a number that describes the overlap between two vectors.
|
|
1444
|
+
* Visually, the dot product can be thought of as the "shadow" one vector
|
|
1445
|
+
* casts on another. The dot product's magnitude is largest when two vectors
|
|
1446
|
+
* point in the same or opposite directions. Its magnitude is 0 when two
|
|
1447
|
+
* vectors form a right angle.The version of `dot()` with one parameter interprets it as another
|
|
1448
|
+
* p5.Vector object.The version of `dot()` with multiple parameters interprets them as the
|
|
1449
|
+
* `x`, `y`, and `z` components of another vector.The static version of `dot()`, as in `p5.Vector.dot(v1, v2)`, is the same
|
|
1450
|
+
* as calling `v1.dot(v2)`.
|
|
1451
|
+
*
|
|
1452
|
+
* @param x component of the vector.
|
|
1453
|
+
* @param y component of the vector.
|
|
1454
|
+
* @param z component of the vector.
|
|
1455
|
+
* @return dot product.
|
|
1456
|
+
* @example <div class="norender">
|
|
1457
|
+
* <code>
|
|
1458
|
+
* function setup() {
|
|
1459
|
+
* // Create p5.Vector objects.
|
|
1460
|
+
* let v1 = createVector(3, 4);
|
|
1461
|
+
* let v2 = createVector(3, 0);
|
|
1462
|
+
*
|
|
1463
|
+
* // Calculate the dot product.
|
|
1464
|
+
* let dp = v1.dot(v2);
|
|
1465
|
+
*
|
|
1466
|
+
* // Prints "9" to the console.
|
|
1467
|
+
* print(dp);
|
|
1468
|
+
* }
|
|
1469
|
+
* </code>
|
|
1470
|
+
* </div>
|
|
1471
|
+
*
|
|
1472
|
+
* <div class="norender">
|
|
1473
|
+
* <code>
|
|
1474
|
+
* function setup() {
|
|
1475
|
+
* // Create p5.Vector objects.
|
|
1476
|
+
* let v1 = createVector(1, 0);
|
|
1477
|
+
* let v2 = createVector(0, 1);
|
|
1478
|
+
*
|
|
1479
|
+
* // Calculate the dot product.
|
|
1480
|
+
* let dp = p5.Vector.dot(v1, v2);
|
|
1481
|
+
*
|
|
1482
|
+
* // Prints "0" to the console.
|
|
1483
|
+
* print(dp);
|
|
1484
|
+
* }
|
|
1485
|
+
* </code>
|
|
1486
|
+
* </div>
|
|
1487
|
+
*
|
|
1488
|
+
* <div>
|
|
1489
|
+
* <code>
|
|
1490
|
+
* function setup() {
|
|
1491
|
+
* createCanvas(100, 100);
|
|
1492
|
+
*
|
|
1493
|
+
* describe('Two arrows drawn on a gray square. A black arrow points to the right and a red arrow follows the mouse. The text "v1 • v2 = something" changes as the mouse moves.');
|
|
1494
|
+
* }
|
|
1495
|
+
*
|
|
1496
|
+
* function draw() {
|
|
1497
|
+
* background(200);
|
|
1498
|
+
*
|
|
1499
|
+
* // Center.
|
|
1500
|
+
* let v0 = createVector(50, 50);
|
|
1501
|
+
*
|
|
1502
|
+
* // Draw the black arrow.
|
|
1503
|
+
* let v1 = createVector(30, 0);
|
|
1504
|
+
* drawArrow(v0, v1, 'black');
|
|
1505
|
+
*
|
|
1506
|
+
* // Draw the red arrow.
|
|
1507
|
+
* let v2 = createVector(mouseX - 50, mouseY - 50);
|
|
1508
|
+
* drawArrow(v0, v2, 'red');
|
|
1509
|
+
*
|
|
1510
|
+
* // Display the dot product.
|
|
1511
|
+
* let dp = v2.dot(v1);
|
|
1512
|
+
* text(`v2 • v1 = ${dp}`, 10, 20);
|
|
1513
|
+
* }
|
|
1514
|
+
*
|
|
1515
|
+
* // Draws an arrow between two vectors.
|
|
1516
|
+
* function drawArrow(base, vec, myColor) {
|
|
1517
|
+
* push();
|
|
1518
|
+
* stroke(myColor);
|
|
1519
|
+
* strokeWeight(3);
|
|
1520
|
+
* fill(myColor);
|
|
1521
|
+
* translate(base.x, base.y);
|
|
1522
|
+
* line(0, 0, vec.x, vec.y);
|
|
1523
|
+
* rotate(vec.heading());
|
|
1524
|
+
* let arrowSize = 7;
|
|
1525
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1526
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1527
|
+
* pop();
|
|
1528
|
+
* }
|
|
1529
|
+
* </code>
|
|
1530
|
+
* </div>
|
|
1531
|
+
*/
|
|
1532
|
+
function dot(...args: any[], x: number, y?: number, z?: number): number;
|
|
1533
|
+
|
|
1534
|
+
/**
|
|
1535
|
+
* @param <a href="#/p5.Vector">p5.Vector</a> to be dotted.
|
|
1536
|
+
*/
|
|
1537
|
+
function dot(...args: any[], v: p5.Vector): number;
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* Calculates the cross product of two vectors.The cross product is a vector that points straight out of the plane created
|
|
1541
|
+
* by two vectors. The cross product's magnitude is the area of the parallelogram
|
|
1542
|
+
* formed by the original two vectors.The static version of `cross()`, as in `p5.Vector.cross(v1, v2)`, is the same
|
|
1543
|
+
* as calling `v1.cross(v2)`.
|
|
1544
|
+
*
|
|
1545
|
+
* @param <a href="#/p5.Vector">p5.Vector</a> to be crossed.
|
|
1546
|
+
* @return cross product as a <a href="#/p5.Vector">p5.Vector</a>.
|
|
1547
|
+
* @example <div class="norender">
|
|
1548
|
+
* <code>
|
|
1549
|
+
* function setup() {
|
|
1550
|
+
* // Create p5.Vector objects.
|
|
1551
|
+
* let v1 = createVector(1, 0);
|
|
1552
|
+
* let v2 = createVector(3, 4);
|
|
1553
|
+
*
|
|
1554
|
+
* // Calculate the cross product.
|
|
1555
|
+
* let cp = v1.cross(v2);
|
|
1556
|
+
*
|
|
1557
|
+
* // Prints "p5.Vector Object : [0, 0, 4]" to the console.
|
|
1558
|
+
* print(cp.toString());
|
|
1559
|
+
* }
|
|
1560
|
+
* </code>
|
|
1561
|
+
* </div>
|
|
1562
|
+
*
|
|
1563
|
+
* <div class="norender">
|
|
1564
|
+
* <code>
|
|
1565
|
+
* function setup() {
|
|
1566
|
+
* // Create p5.Vector objects.
|
|
1567
|
+
* let v1 = createVector(1, 0);
|
|
1568
|
+
* let v2 = createVector(3, 4);
|
|
1569
|
+
*
|
|
1570
|
+
* // Calculate the cross product.
|
|
1571
|
+
* let cp = p5.Vector.cross(v1, v2);
|
|
1572
|
+
*
|
|
1573
|
+
* // Prints "p5.Vector Object : [0, 0, 4]" to the console.
|
|
1574
|
+
* print(cp.toString());
|
|
1575
|
+
* }
|
|
1576
|
+
* </code>
|
|
1577
|
+
* </div>
|
|
1578
|
+
*/
|
|
1579
|
+
function cross(v: p5.Vector): p5.Vector;
|
|
1580
|
+
|
|
1581
|
+
/**
|
|
1582
|
+
* Scales the components of a p5.Vector object so
|
|
1583
|
+
* that its magnitude is 1.The static version of `normalize()`, as in `p5.Vector.normalize(v)`,
|
|
1584
|
+
* returns a new p5.Vector object and doesn't change
|
|
1585
|
+
* the original.
|
|
1586
|
+
*
|
|
1587
|
+
* @return normalized <a href="#/p5.Vector">p5.Vector</a>.
|
|
1588
|
+
* @example <div class="norender">
|
|
1589
|
+
* <code>
|
|
1590
|
+
* function setup() {
|
|
1591
|
+
* createCanvas(100, 100);
|
|
1592
|
+
*
|
|
1593
|
+
* background(200);
|
|
1594
|
+
*
|
|
1595
|
+
* // Create a p5.Vector.
|
|
1596
|
+
* let v = createVector(10, 20, 2);
|
|
1597
|
+
*
|
|
1598
|
+
* // Normalize.
|
|
1599
|
+
* v.normalize();
|
|
1600
|
+
*
|
|
1601
|
+
* // Prints "p5.Vector Object : [0.445..., 0.890..., 0.089...]" to the console.
|
|
1602
|
+
* print(v.toString());
|
|
1603
|
+
* }
|
|
1604
|
+
* </code>
|
|
1605
|
+
* </div>
|
|
1606
|
+
*
|
|
1607
|
+
* <div class="norender">
|
|
1608
|
+
* <code>
|
|
1609
|
+
* function setup() {
|
|
1610
|
+
* createCanvas(100, 100);
|
|
1611
|
+
*
|
|
1612
|
+
* background(200);
|
|
1613
|
+
*
|
|
1614
|
+
* // Create a p5.Vector.
|
|
1615
|
+
* let v0 = createVector(10, 20, 2);
|
|
1616
|
+
*
|
|
1617
|
+
* // Create a normalized copy.
|
|
1618
|
+
* let v1 = p5.Vector.normalize(v0);
|
|
1619
|
+
*
|
|
1620
|
+
* // Prints "p5.Vector Object : [10, 20, 2]" to the console.
|
|
1621
|
+
* print(v0.toString());
|
|
1622
|
+
* // Prints "p5.Vector Object : [0.445..., 0.890..., 0.089...]" to the console.
|
|
1623
|
+
* print(v1.toString());
|
|
1624
|
+
* }
|
|
1625
|
+
* </code>
|
|
1626
|
+
* </div>
|
|
1627
|
+
*
|
|
1628
|
+
* <div>
|
|
1629
|
+
* <code>
|
|
1630
|
+
* function setup() {
|
|
1631
|
+
* createCanvas(100, 100);
|
|
1632
|
+
*
|
|
1633
|
+
* describe("A red and blue arrow extend from the center of a circle. Both arrows follow the mouse, but the blue arrow's length is fixed to the circle's radius.");
|
|
1634
|
+
* }
|
|
1635
|
+
*
|
|
1636
|
+
* function draw() {
|
|
1637
|
+
* background(240);
|
|
1638
|
+
*
|
|
1639
|
+
* // Vector to the center.
|
|
1640
|
+
* let v0 = createVector(50, 50);
|
|
1641
|
+
*
|
|
1642
|
+
* // Vector from the center to the mouse.
|
|
1643
|
+
* let v1 = createVector(mouseX - 50, mouseY - 50);
|
|
1644
|
+
*
|
|
1645
|
+
* // Circle's radius.
|
|
1646
|
+
* let r = 25;
|
|
1647
|
+
*
|
|
1648
|
+
* // Draw the red arrow.
|
|
1649
|
+
* drawArrow(v0, v1, 'red');
|
|
1650
|
+
*
|
|
1651
|
+
* // Draw the blue arrow.
|
|
1652
|
+
* v1.normalize();
|
|
1653
|
+
* drawArrow(v0, v1.mult(r), 'blue');
|
|
1654
|
+
*
|
|
1655
|
+
* // Draw the circle.
|
|
1656
|
+
* noFill();
|
|
1657
|
+
* circle(50, 50, r * 2);
|
|
1658
|
+
* }
|
|
1659
|
+
*
|
|
1660
|
+
* // Draws an arrow between two vectors.
|
|
1661
|
+
* function drawArrow(base, vec, myColor) {
|
|
1662
|
+
* push();
|
|
1663
|
+
* stroke(myColor);
|
|
1664
|
+
* strokeWeight(3);
|
|
1665
|
+
* fill(myColor);
|
|
1666
|
+
* translate(base.x, base.y);
|
|
1667
|
+
* line(0, 0, vec.x, vec.y);
|
|
1668
|
+
* rotate(vec.heading());
|
|
1669
|
+
* let arrowSize = 7;
|
|
1670
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1671
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1672
|
+
* pop();
|
|
1673
|
+
* }
|
|
1674
|
+
* </code>
|
|
1675
|
+
* </div>
|
|
1676
|
+
*/
|
|
1677
|
+
function normalize(): p5.Vector;
|
|
1678
|
+
|
|
1679
|
+
/**
|
|
1680
|
+
* Limits a vector's magnitude to a maximum value.The static version of `limit()`, as in `p5.Vector.limit(v, 5)`, returns a
|
|
1681
|
+
* new p5.Vector object and doesn't change the
|
|
1682
|
+
* original.
|
|
1683
|
+
*
|
|
1684
|
+
* @param maximum magnitude for the vector.
|
|
1685
|
+
* @example <div class="norender">
|
|
1686
|
+
* <code>
|
|
1687
|
+
* function setup() {
|
|
1688
|
+
* // Create a p5.Vector object.
|
|
1689
|
+
* let v = createVector(10, 20, 2);
|
|
1690
|
+
*
|
|
1691
|
+
* // Limit its magnitude.
|
|
1692
|
+
* v.limit(5);
|
|
1693
|
+
*
|
|
1694
|
+
* // Prints "p5.Vector Object : [2.227..., 4.454..., 0.445...]" to the console.
|
|
1695
|
+
* print(v.toString());
|
|
1696
|
+
* }
|
|
1697
|
+
* </code>
|
|
1698
|
+
* </div>
|
|
1699
|
+
*
|
|
1700
|
+
* <div class="norender">
|
|
1701
|
+
* <code>
|
|
1702
|
+
* function setup() {
|
|
1703
|
+
* // Create a p5.Vector object.
|
|
1704
|
+
* let v0 = createVector(10, 20, 2);
|
|
1705
|
+
*
|
|
1706
|
+
* // Create a copy an limit its magintude.
|
|
1707
|
+
* let v1 = p5.Vector.limit(v0, 5);
|
|
1708
|
+
*
|
|
1709
|
+
* // Prints "p5.Vector Object : [2.227..., 4.454..., 0.445...]" to the console.
|
|
1710
|
+
* print(v1.toString());
|
|
1711
|
+
* }
|
|
1712
|
+
* </code>
|
|
1713
|
+
* </div>
|
|
1714
|
+
*
|
|
1715
|
+
* <div>
|
|
1716
|
+
* <code>
|
|
1717
|
+
* function setup() {
|
|
1718
|
+
* createCanvas(100, 100);
|
|
1719
|
+
*
|
|
1720
|
+
* describe("A red and blue arrow extend from the center of a circle. Both arrows follow the mouse, but the blue arrow never crosses the circle's edge.");
|
|
1721
|
+
* }
|
|
1722
|
+
* function draw() {
|
|
1723
|
+
* background(240);
|
|
1724
|
+
*
|
|
1725
|
+
* // Vector to the center.
|
|
1726
|
+
* let v0 = createVector(50, 50);
|
|
1727
|
+
*
|
|
1728
|
+
* // Vector from the center to the mouse.
|
|
1729
|
+
* let v1 = createVector(mouseX - 50, mouseY - 50);
|
|
1730
|
+
*
|
|
1731
|
+
* // Circle's radius.
|
|
1732
|
+
* let r = 25;
|
|
1733
|
+
*
|
|
1734
|
+
* // Draw the red arrow.
|
|
1735
|
+
* drawArrow(v0, v1, 'red');
|
|
1736
|
+
*
|
|
1737
|
+
* // Draw the blue arrow.
|
|
1738
|
+
* drawArrow(v0, v1.limit(r), 'blue');
|
|
1739
|
+
*
|
|
1740
|
+
* // Draw the circle.
|
|
1741
|
+
* noFill();
|
|
1742
|
+
* circle(50, 50, r * 2);
|
|
1743
|
+
* }
|
|
1744
|
+
*
|
|
1745
|
+
* // Draws an arrow between two vectors.
|
|
1746
|
+
* function drawArrow(base, vec, myColor) {
|
|
1747
|
+
* push();
|
|
1748
|
+
* stroke(myColor);
|
|
1749
|
+
* strokeWeight(3);
|
|
1750
|
+
* fill(myColor);
|
|
1751
|
+
* translate(base.x, base.y);
|
|
1752
|
+
* line(0, 0, vec.x, vec.y);
|
|
1753
|
+
* rotate(vec.heading());
|
|
1754
|
+
* let arrowSize = 7;
|
|
1755
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1756
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1757
|
+
* pop();
|
|
1758
|
+
* }
|
|
1759
|
+
* </code>
|
|
1760
|
+
* </div>
|
|
1761
|
+
*/
|
|
1762
|
+
function limit(max: number): void;
|
|
1763
|
+
|
|
1764
|
+
/**
|
|
1765
|
+
* Sets a vector's magnitude to a given value.The static version of `setMag()`, as in `p5.Vector.setMag(v, 10)`, returns
|
|
1766
|
+
* a new p5.Vector object and doesn't change the
|
|
1767
|
+
* original.
|
|
1768
|
+
*
|
|
1769
|
+
* @param new length for this vector.
|
|
1770
|
+
* @example <div class="norender">
|
|
1771
|
+
* <code>
|
|
1772
|
+
* function setup() {
|
|
1773
|
+
* // Create a p5.Vector object.
|
|
1774
|
+
* let v = createVector(3, 4, 0);
|
|
1775
|
+
*
|
|
1776
|
+
* // Prints "5" to the console.
|
|
1777
|
+
* print(v.mag());
|
|
1778
|
+
*
|
|
1779
|
+
* // Set its magnitude to 10.
|
|
1780
|
+
* v.setMag(10);
|
|
1781
|
+
*
|
|
1782
|
+
* // Prints "p5.Vector Object : [6, 8, 0]" to the console.
|
|
1783
|
+
* print(v.toString());
|
|
1784
|
+
* }
|
|
1785
|
+
* </code>
|
|
1786
|
+
* </div>
|
|
1787
|
+
*
|
|
1788
|
+
* <div class="norender">
|
|
1789
|
+
* <code>
|
|
1790
|
+
* function setup() {
|
|
1791
|
+
* // Create a p5.Vector object.
|
|
1792
|
+
* let v0 = createVector(3, 4, 0);
|
|
1793
|
+
*
|
|
1794
|
+
* // Create a copy with a magnitude of 10.
|
|
1795
|
+
* let v1 = p5.Vector.setMag(v0, 10);
|
|
1796
|
+
*
|
|
1797
|
+
* // Prints "5" to the console.
|
|
1798
|
+
* print(v0.mag());
|
|
1799
|
+
*
|
|
1800
|
+
* // Prints "p5.Vector Object : [6, 8, 0]" to the console.
|
|
1801
|
+
* print(v1.toString());
|
|
1802
|
+
* }
|
|
1803
|
+
* </code>
|
|
1804
|
+
* </div>
|
|
1805
|
+
*
|
|
1806
|
+
* <div>
|
|
1807
|
+
* <code>
|
|
1808
|
+
* function setup() {
|
|
1809
|
+
* createCanvas(100, 100);
|
|
1810
|
+
*
|
|
1811
|
+
* describe('Two arrows extend from the top left corner of a square toward its center. The red arrow reaches the center and the blue arrow only extends part of the way.');
|
|
1812
|
+
* }
|
|
1813
|
+
*
|
|
1814
|
+
* function draw() {
|
|
1815
|
+
* background(240);
|
|
1816
|
+
*
|
|
1817
|
+
* let origin = createVector(0, 0);
|
|
1818
|
+
* let v = createVector(50, 50);
|
|
1819
|
+
*
|
|
1820
|
+
* // Draw the red arrow.
|
|
1821
|
+
* drawArrow(origin, v, 'red');
|
|
1822
|
+
*
|
|
1823
|
+
* // Set v's magnitude to 30.
|
|
1824
|
+
* v.setMag(30);
|
|
1825
|
+
*
|
|
1826
|
+
* // Draw the blue arrow.
|
|
1827
|
+
* drawArrow(origin, v, 'blue');
|
|
1828
|
+
* }
|
|
1829
|
+
*
|
|
1830
|
+
* // Draws an arrow between two vectors.
|
|
1831
|
+
* function drawArrow(base, vec, myColor) {
|
|
1832
|
+
* push();
|
|
1833
|
+
* stroke(myColor);
|
|
1834
|
+
* strokeWeight(3);
|
|
1835
|
+
* fill(myColor);
|
|
1836
|
+
* translate(base.x, base.y);
|
|
1837
|
+
* line(0, 0, vec.x, vec.y);
|
|
1838
|
+
* rotate(vec.heading());
|
|
1839
|
+
* let arrowSize = 7;
|
|
1840
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1841
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1842
|
+
* pop();
|
|
1843
|
+
* }
|
|
1844
|
+
* </code>
|
|
1845
|
+
* </div>
|
|
1846
|
+
*/
|
|
1847
|
+
function setMag(n: any, len: number): void;
|
|
1848
|
+
|
|
1849
|
+
/**
|
|
1850
|
+
* Calculates the angle a 2D vector makes with the positive x-axis.By convention, the positive x-axis has an angle of 0. Angles increase in
|
|
1851
|
+
* the clockwise direction.If the vector was created with
|
|
1852
|
+
* createVector(), `heading()` returns angles
|
|
1853
|
+
* in the units of the current angleMode().The static version of `heading()`, as in `p5.Vector.heading(v)`, works the
|
|
1854
|
+
* same way.
|
|
1855
|
+
*
|
|
1856
|
+
* @return angle of rotation.
|
|
1857
|
+
* @example <div class = "norender">
|
|
1858
|
+
* <code>
|
|
1859
|
+
* function setup() {
|
|
1860
|
+
* // Create a p5.Vector object.
|
|
1861
|
+
* let v = createVector(1, 1);
|
|
1862
|
+
*
|
|
1863
|
+
* // Prints "0.785..." to the console.
|
|
1864
|
+
* print(v.heading());
|
|
1865
|
+
*
|
|
1866
|
+
* // Use degrees.
|
|
1867
|
+
* angleMode(DEGREES);
|
|
1868
|
+
*
|
|
1869
|
+
* // Prints "45" to the console.
|
|
1870
|
+
* print(v.heading());
|
|
1871
|
+
* }
|
|
1872
|
+
* </code>
|
|
1873
|
+
* </div>
|
|
1874
|
+
*
|
|
1875
|
+
* <div class = "norender">
|
|
1876
|
+
* <code>
|
|
1877
|
+
* function setup() {
|
|
1878
|
+
* // Create a p5.Vector object.
|
|
1879
|
+
* let v = createVector(1, 1);
|
|
1880
|
+
*
|
|
1881
|
+
* // Prints "0.785..." to the console.
|
|
1882
|
+
* print(p5.Vector.heading(v));
|
|
1883
|
+
*
|
|
1884
|
+
* // Use degrees.
|
|
1885
|
+
* angleMode(DEGREES);
|
|
1886
|
+
*
|
|
1887
|
+
* // Prints "45" to the console.
|
|
1888
|
+
* print(p5.Vector.heading(v));
|
|
1889
|
+
* }
|
|
1890
|
+
* </code>
|
|
1891
|
+
* </div>
|
|
1892
|
+
*
|
|
1893
|
+
* <div>
|
|
1894
|
+
* <code>
|
|
1895
|
+
* function setup() {
|
|
1896
|
+
* createCanvas(100, 100);
|
|
1897
|
+
*
|
|
1898
|
+
* describe('A black arrow extends from the top left of a square to its center. The text "Radians: 0.79" and "Degrees: 45" is written near the tip of the arrow.');
|
|
1899
|
+
* }
|
|
1900
|
+
*
|
|
1901
|
+
* function draw() {
|
|
1902
|
+
* background(200);
|
|
1903
|
+
*
|
|
1904
|
+
* let origin = createVector(0, 0);
|
|
1905
|
+
* let v = createVector(50, 50);
|
|
1906
|
+
*
|
|
1907
|
+
* // Draw the black arrow.
|
|
1908
|
+
* drawArrow(origin, v, 'black');
|
|
1909
|
+
*
|
|
1910
|
+
* // Use radians.
|
|
1911
|
+
* angleMode(RADIANS);
|
|
1912
|
+
*
|
|
1913
|
+
* // Display the heading in radians.
|
|
1914
|
+
* let h = round(v.heading(), 2);
|
|
1915
|
+
* text(`Radians: ${h}`, 20, 70);
|
|
1916
|
+
*
|
|
1917
|
+
* // Use degrees.
|
|
1918
|
+
* angleMode(DEGREES);
|
|
1919
|
+
*
|
|
1920
|
+
* // Display the heading in degrees.
|
|
1921
|
+
* h = v.heading();
|
|
1922
|
+
* text(`Degrees: ${h}`, 20, 85);
|
|
1923
|
+
* }
|
|
1924
|
+
*
|
|
1925
|
+
* // Draws an arrow between two vectors.
|
|
1926
|
+
* function drawArrow(base, vec, myColor) {
|
|
1927
|
+
* push();
|
|
1928
|
+
* stroke(myColor);
|
|
1929
|
+
* strokeWeight(3);
|
|
1930
|
+
* fill(myColor);
|
|
1931
|
+
* translate(base.x, base.y);
|
|
1932
|
+
* line(0, 0, vec.x, vec.y);
|
|
1933
|
+
* rotate(vec.heading());
|
|
1934
|
+
* let arrowSize = 7;
|
|
1935
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
1936
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
1937
|
+
* pop();
|
|
1938
|
+
* }
|
|
1939
|
+
* </code>
|
|
1940
|
+
* </div>
|
|
1941
|
+
*/
|
|
1942
|
+
function heading(): number;
|
|
1943
|
+
|
|
1944
|
+
/**
|
|
1945
|
+
* Rotates a 2D vector to a specific angle without changing its magnitude.By convention, the positive x-axis has an angle of 0. Angles increase in
|
|
1946
|
+
* the clockwise direction.If the vector was created with
|
|
1947
|
+
* createVector(), `setHeading()` uses
|
|
1948
|
+
* the units of the current angleMode().
|
|
1949
|
+
*
|
|
1950
|
+
* @param angle of rotation.
|
|
1951
|
+
* @example <div class="norender">
|
|
1952
|
+
* <code>
|
|
1953
|
+
* function setup() {
|
|
1954
|
+
* // Create a p5.Vector object.
|
|
1955
|
+
* let v = createVector(0, 1);
|
|
1956
|
+
*
|
|
1957
|
+
* // Prints "1.570..." to the console.
|
|
1958
|
+
* print(v.heading());
|
|
1959
|
+
*
|
|
1960
|
+
* // Point to the left.
|
|
1961
|
+
* v.setHeading(PI);
|
|
1962
|
+
*
|
|
1963
|
+
* // Prints "3.141..." to the console.
|
|
1964
|
+
* print(v.heading());
|
|
1965
|
+
* }
|
|
1966
|
+
* </code>
|
|
1967
|
+
* </div>
|
|
1968
|
+
*
|
|
1969
|
+
* <div class="norender">
|
|
1970
|
+
* <code>
|
|
1971
|
+
* function setup() {
|
|
1972
|
+
* // Use degrees.
|
|
1973
|
+
* angleMode(DEGREES);
|
|
1974
|
+
*
|
|
1975
|
+
* // Create a p5.Vector object.
|
|
1976
|
+
* let v = createVector(0, 1);
|
|
1977
|
+
*
|
|
1978
|
+
* // Prints "90" to the console.
|
|
1979
|
+
* print(v.heading());
|
|
1980
|
+
*
|
|
1981
|
+
* // Point to the left.
|
|
1982
|
+
* v.setHeading(180);
|
|
1983
|
+
*
|
|
1984
|
+
* // Prints "180" to the console.
|
|
1985
|
+
* print(v.heading());
|
|
1986
|
+
* }
|
|
1987
|
+
* </code>
|
|
1988
|
+
* </div>
|
|
1989
|
+
*
|
|
1990
|
+
* <div>
|
|
1991
|
+
* <code>
|
|
1992
|
+
* function setup() {
|
|
1993
|
+
* createCanvas(100, 100);
|
|
1994
|
+
*
|
|
1995
|
+
* describe('Two arrows extend from the center of a gray square. The red arrow points to the right and the blue arrow points down.');
|
|
1996
|
+
* }
|
|
1997
|
+
*
|
|
1998
|
+
* function draw() {
|
|
1999
|
+
* background(200);
|
|
2000
|
+
*
|
|
2001
|
+
* // Create p5.Vector objects.
|
|
2002
|
+
* let v0 = createVector(50, 50);
|
|
2003
|
+
* let v1 = createVector(30, 0);
|
|
2004
|
+
*
|
|
2005
|
+
* // Draw the red arrow.
|
|
2006
|
+
* drawArrow(v0, v1, 'red');
|
|
2007
|
+
*
|
|
2008
|
+
* // Point down.
|
|
2009
|
+
* v1.setHeading(HALF_PI);
|
|
2010
|
+
*
|
|
2011
|
+
* // Draw the blue arrow.
|
|
2012
|
+
* drawArrow(v0, v1, 'blue');
|
|
2013
|
+
* }
|
|
2014
|
+
*
|
|
2015
|
+
* // Draws an arrow between two vectors.
|
|
2016
|
+
* function drawArrow(base, vec, myColor) {
|
|
2017
|
+
* push();
|
|
2018
|
+
* stroke(myColor);
|
|
2019
|
+
* strokeWeight(3);
|
|
2020
|
+
* fill(myColor);
|
|
2021
|
+
* translate(base.x, base.y);
|
|
2022
|
+
* line(0, 0, vec.x, vec.y);
|
|
2023
|
+
* rotate(vec.heading());
|
|
2024
|
+
* let arrowSize = 7;
|
|
2025
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2026
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2027
|
+
* pop();
|
|
2028
|
+
* }
|
|
2029
|
+
* </code>
|
|
2030
|
+
* </div>
|
|
2031
|
+
*/
|
|
2032
|
+
function setHeading(a: any, angle: number): void;
|
|
2033
|
+
|
|
2034
|
+
/**
|
|
2035
|
+
* Rotates a 2D vector by an angle without changing its magnitude.By convention, the positive x-axis has an angle of 0. Angles increase in
|
|
2036
|
+
* the clockwise direction.If the vector was created with
|
|
2037
|
+
* createVector(), `rotate()` uses
|
|
2038
|
+
* the units of the current angleMode().The static version of `rotate()`, as in `p5.Vector.rotate(v, PI)`,
|
|
2039
|
+
* returns a new p5.Vector object and doesn't change
|
|
2040
|
+
* the original.
|
|
2041
|
+
*
|
|
2042
|
+
* @param angle of rotation.
|
|
2043
|
+
* @example <div class="norender">
|
|
2044
|
+
* <code>
|
|
2045
|
+
* function setup() {
|
|
2046
|
+
* // Create a p5.Vector object.
|
|
2047
|
+
* let v = createVector(1, 0);
|
|
2048
|
+
*
|
|
2049
|
+
* // Prints "p5.Vector Object : [1, 0, 0]" to the console.
|
|
2050
|
+
* print(v.toString());
|
|
2051
|
+
*
|
|
2052
|
+
* // Rotate a quarter turn.
|
|
2053
|
+
* v.rotate(HALF_PI);
|
|
2054
|
+
*
|
|
2055
|
+
* // Prints "p5.Vector Object : [0, 1, 0]" to the console.
|
|
2056
|
+
* print(v.toString());
|
|
2057
|
+
* }
|
|
2058
|
+
* </code>
|
|
2059
|
+
* </div>
|
|
2060
|
+
*
|
|
2061
|
+
* <div class="norender">
|
|
2062
|
+
* <code>
|
|
2063
|
+
* function setup() {
|
|
2064
|
+
* // Use degrees.
|
|
2065
|
+
* angleMode(DEGREES);
|
|
2066
|
+
*
|
|
2067
|
+
* // Create a p5.Vector object.
|
|
2068
|
+
* let v = createVector(1, 0);
|
|
2069
|
+
*
|
|
2070
|
+
* // Prints "p5.Vector Object : [1, 0, 0]" to the console.
|
|
2071
|
+
* print(v.toString());
|
|
2072
|
+
*
|
|
2073
|
+
* // Rotate a quarter turn.
|
|
2074
|
+
* v.rotate(90);
|
|
2075
|
+
*
|
|
2076
|
+
* // Prints "p5.Vector Object : [0, 1, 0]" to the console.
|
|
2077
|
+
* print(v.toString());
|
|
2078
|
+
* }
|
|
2079
|
+
* </code>
|
|
2080
|
+
* </div>
|
|
2081
|
+
*
|
|
2082
|
+
* <div class="norender">
|
|
2083
|
+
* <code>
|
|
2084
|
+
* function setup() {
|
|
2085
|
+
* // Create a p5.Vector object.
|
|
2086
|
+
* let v0 = createVector(1, 0);
|
|
2087
|
+
*
|
|
2088
|
+
* // Create a rotated copy.
|
|
2089
|
+
* let v1 = p5.Vector.rotate(v0, HALF_PI);
|
|
2090
|
+
*
|
|
2091
|
+
* // Prints "p5.Vector Object : [1, 0, 0]" to the console.
|
|
2092
|
+
* print(v0.toString());
|
|
2093
|
+
* // Prints "p5.Vector Object : [0, 1, 0]" to the console.
|
|
2094
|
+
* print(v1.toString());
|
|
2095
|
+
* }
|
|
2096
|
+
* </code>
|
|
2097
|
+
* </div>
|
|
2098
|
+
*
|
|
2099
|
+
* <div class="norender">
|
|
2100
|
+
* <code>
|
|
2101
|
+
* function setup() {
|
|
2102
|
+
* // Use degrees.
|
|
2103
|
+
* angleMode(DEGREES);
|
|
2104
|
+
*
|
|
2105
|
+
* // Create a p5.Vector object.
|
|
2106
|
+
* let v0 = createVector(1, 0);
|
|
2107
|
+
*
|
|
2108
|
+
* // Create a rotated copy.
|
|
2109
|
+
* let v1 = p5.Vector.rotate(v0, 90);
|
|
2110
|
+
*
|
|
2111
|
+
* // Prints "p5.Vector Object : [1, 0, 0]" to the console.
|
|
2112
|
+
* print(v0.toString());
|
|
2113
|
+
*
|
|
2114
|
+
* // Prints "p5.Vector Object : [0, 1, 0]" to the console.
|
|
2115
|
+
* print(v1.toString());
|
|
2116
|
+
* }
|
|
2117
|
+
* </code>
|
|
2118
|
+
* </div>
|
|
2119
|
+
*
|
|
2120
|
+
* <div>
|
|
2121
|
+
* <code>
|
|
2122
|
+
* let v0;
|
|
2123
|
+
* let v1;
|
|
2124
|
+
*
|
|
2125
|
+
* function setup() {
|
|
2126
|
+
* createCanvas(100, 100);
|
|
2127
|
+
*
|
|
2128
|
+
* // Create p5.Vector objects.
|
|
2129
|
+
* v0 = createVector(50, 50);
|
|
2130
|
+
* v1 = createVector(30, 0);
|
|
2131
|
+
*
|
|
2132
|
+
* describe('A black arrow extends from the center of a gray square. The arrow rotates clockwise.');
|
|
2133
|
+
* }
|
|
2134
|
+
*
|
|
2135
|
+
* function draw() {
|
|
2136
|
+
* background(240);
|
|
2137
|
+
*
|
|
2138
|
+
* // Rotate v1.
|
|
2139
|
+
* v1.rotate(0.01);
|
|
2140
|
+
*
|
|
2141
|
+
* // Draw the black arrow.
|
|
2142
|
+
* drawArrow(v0, v1, 'black');
|
|
2143
|
+
* }
|
|
2144
|
+
*
|
|
2145
|
+
* // Draws an arrow between two vectors.
|
|
2146
|
+
* function drawArrow(base, vec, myColor) {
|
|
2147
|
+
* push();
|
|
2148
|
+
* stroke(myColor);
|
|
2149
|
+
* strokeWeight(3);
|
|
2150
|
+
* fill(myColor);
|
|
2151
|
+
* translate(base.x, base.y);
|
|
2152
|
+
* line(0, 0, vec.x, vec.y);
|
|
2153
|
+
* rotate(vec.heading());
|
|
2154
|
+
* let arrowSize = 7;
|
|
2155
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2156
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2157
|
+
* pop();
|
|
2158
|
+
* }
|
|
2159
|
+
* </code>
|
|
2160
|
+
* </div>
|
|
2161
|
+
*/
|
|
2162
|
+
function rotate(a: any, angle: number): void;
|
|
2163
|
+
|
|
2164
|
+
/**
|
|
2165
|
+
* Calculates the angle between two vectors.The angles returned are signed, which means that
|
|
2166
|
+
* `v1.angleBetween(v2) === -v2.angleBetween(v1)`.If the vector was created with
|
|
2167
|
+
* createVector(), `angleBetween()` returns
|
|
2168
|
+
* angles in the units of the current
|
|
2169
|
+
* angleMode().
|
|
2170
|
+
*
|
|
2171
|
+
* @param x, y, and z components of a <a href="#/p5.Vector">p5.Vector</a>.
|
|
2172
|
+
* @return angle between the vectors.
|
|
2173
|
+
* @example <div class="norender">
|
|
2174
|
+
* <code>
|
|
2175
|
+
* function setup() {
|
|
2176
|
+
* // Create p5.Vector objects.
|
|
2177
|
+
* let v0 = createVector(1, 0);
|
|
2178
|
+
* let v1 = createVector(0, 1);
|
|
2179
|
+
*
|
|
2180
|
+
* // Prints "1.570..." to the console.
|
|
2181
|
+
* print(v0.angleBetween(v1));
|
|
2182
|
+
*
|
|
2183
|
+
* // Prints "-1.570..." to the console.
|
|
2184
|
+
* print(v1.angleBetween(v0));
|
|
2185
|
+
* }
|
|
2186
|
+
* </code>
|
|
2187
|
+
* </div>
|
|
2188
|
+
*
|
|
2189
|
+
* <div class="norender">
|
|
2190
|
+
* <code>
|
|
2191
|
+
* function setup() {
|
|
2192
|
+
* // Use degrees.
|
|
2193
|
+
* angleMode(DEGREES);
|
|
2194
|
+
* // Create p5.Vector objects.
|
|
2195
|
+
* let v0 = createVector(1, 0);
|
|
2196
|
+
* let v1 = createVector(0, 1);
|
|
2197
|
+
*
|
|
2198
|
+
* // Prints "90" to the console.
|
|
2199
|
+
* print(v0.angleBetween(v1));
|
|
2200
|
+
*
|
|
2201
|
+
* // Prints "-90" to the console.
|
|
2202
|
+
* print(v1.angleBetween(v0));
|
|
2203
|
+
* }
|
|
2204
|
+
* </code>
|
|
2205
|
+
* </div>
|
|
2206
|
+
*
|
|
2207
|
+
* <div class="norender">
|
|
2208
|
+
* <code>
|
|
2209
|
+
* function setup() {
|
|
2210
|
+
* // Create p5.Vector objects.
|
|
2211
|
+
* let v0 = createVector(1, 0);
|
|
2212
|
+
* let v1 = createVector(0, 1);
|
|
2213
|
+
*
|
|
2214
|
+
* // Prints "1.570..." to the console.
|
|
2215
|
+
* print(p5.Vector.angleBetween(v0, v1));
|
|
2216
|
+
*
|
|
2217
|
+
* // Prints "-1.570..." to the console.
|
|
2218
|
+
* print(p5.Vector.angleBetween(v1, v0));
|
|
2219
|
+
* }
|
|
2220
|
+
* </code>
|
|
2221
|
+
* </div>
|
|
2222
|
+
*
|
|
2223
|
+
* <div class="norender">
|
|
2224
|
+
* <code>
|
|
2225
|
+
* function setup() {
|
|
2226
|
+
* // Use degrees.
|
|
2227
|
+
* angleMode(DEGREES);
|
|
2228
|
+
*
|
|
2229
|
+
* // Create p5.Vector objects.
|
|
2230
|
+
* let v0 = createVector(1, 0);
|
|
2231
|
+
* let v1 = createVector(0, 1);
|
|
2232
|
+
*
|
|
2233
|
+
* // Prints "90" to the console.
|
|
2234
|
+
* print(p5.Vector.angleBetween(v0, v1));
|
|
2235
|
+
*
|
|
2236
|
+
* // Prints "-90" to the console.
|
|
2237
|
+
* print(p5.Vector.angleBetween(v1, v0));
|
|
2238
|
+
* }
|
|
2239
|
+
* </code>
|
|
2240
|
+
* </div>
|
|
2241
|
+
*
|
|
2242
|
+
* <div>
|
|
2243
|
+
* <code>
|
|
2244
|
+
* function setup() {
|
|
2245
|
+
* createCanvas(100, 100);
|
|
2246
|
+
*
|
|
2247
|
+
* describe('Two arrows extend from the center of a gray square. A red arrow points to the right and a blue arrow points down. The text "Radians: 1.57" and "Degrees: 90" is written above the arrows.');
|
|
2248
|
+
* }
|
|
2249
|
+
* function draw() {
|
|
2250
|
+
* background(200);
|
|
2251
|
+
*
|
|
2252
|
+
* // Create p5.Vector objects.
|
|
2253
|
+
* let v0 = createVector(50, 50);
|
|
2254
|
+
* let v1 = createVector(30, 0);
|
|
2255
|
+
* let v2 = createVector(0, 30);
|
|
2256
|
+
*
|
|
2257
|
+
* // Draw the red arrow.
|
|
2258
|
+
* drawArrow(v0, v1, 'red');
|
|
2259
|
+
*
|
|
2260
|
+
* // Draw the blue arrow.
|
|
2261
|
+
* drawArrow(v0, v2, 'blue');
|
|
2262
|
+
*
|
|
2263
|
+
* // Use radians.
|
|
2264
|
+
* angleMode(RADIANS);
|
|
2265
|
+
*
|
|
2266
|
+
* // Display the angle in radians.
|
|
2267
|
+
* let angle = round(v1.angleBetween(v2), 2);
|
|
2268
|
+
* text(`Radians: ${angle}`, 20, 20);
|
|
2269
|
+
*
|
|
2270
|
+
* // Use degrees.
|
|
2271
|
+
* angleMode(DEGREES);
|
|
2272
|
+
*
|
|
2273
|
+
* // Display the angle in degrees.
|
|
2274
|
+
* angle = round(v1.angleBetween(v2), 2);
|
|
2275
|
+
* text(`Degrees: ${angle}`, 20, 35);
|
|
2276
|
+
* }
|
|
2277
|
+
*
|
|
2278
|
+
* // Draws an arrow between two vectors.
|
|
2279
|
+
* function drawArrow(base, vec, myColor) {
|
|
2280
|
+
* push();
|
|
2281
|
+
* stroke(myColor);
|
|
2282
|
+
* strokeWeight(3);
|
|
2283
|
+
* fill(myColor);
|
|
2284
|
+
* translate(base.x, base.y);
|
|
2285
|
+
* line(0, 0, vec.x, vec.y);
|
|
2286
|
+
* rotate(vec.heading());
|
|
2287
|
+
* let arrowSize = 7;
|
|
2288
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2289
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2290
|
+
* pop();
|
|
2291
|
+
* }
|
|
2292
|
+
* </code>
|
|
2293
|
+
* </div>
|
|
2294
|
+
*/
|
|
2295
|
+
function angleBetween(v: any, value: p5.Vector): number;
|
|
2296
|
+
|
|
2297
|
+
/**
|
|
2298
|
+
* Calculates new `x`, `y`, and `z` components that are proportionally the
|
|
2299
|
+
* same distance between two vectors.The `amt` parameter is the amount to interpolate between the old vector and
|
|
2300
|
+
* the new vector. 0.0 keeps all components equal to the old vector's, 0.5 is
|
|
2301
|
+
* halfway between, and 1.0 sets all components equal to the new vector's.The static version of `lerp()`, as in `p5.Vector.lerp(v0, v1, 0.5)`,
|
|
2302
|
+
* returns a new p5.Vector object and doesn't change
|
|
2303
|
+
* the original.
|
|
2304
|
+
*
|
|
2305
|
+
* @param x component.
|
|
2306
|
+
* @param y component.
|
|
2307
|
+
* @param z component.
|
|
2308
|
+
* @param amount of interpolation between 0.0 (old vector)
|
|
2309
|
+
* and 1.0 (new vector). 0.5 is halfway between.
|
|
2310
|
+
* @example <div class="norender">
|
|
2311
|
+
* <code>
|
|
2312
|
+
* function setup() {
|
|
2313
|
+
* // Create a p5.Vector object.
|
|
2314
|
+
* let v0 = createVector(1, 1, 1);
|
|
2315
|
+
* let v1 = createVector(3, 3, 3);
|
|
2316
|
+
*
|
|
2317
|
+
* // Interpolate.
|
|
2318
|
+
* v0.lerp(v1, 0.5);
|
|
2319
|
+
*
|
|
2320
|
+
* // Prints "p5.Vector Object : [2, 2, 2]" to the console.
|
|
2321
|
+
* print(v0.toString());
|
|
2322
|
+
* }
|
|
2323
|
+
* </code>
|
|
2324
|
+
* </div>
|
|
2325
|
+
*
|
|
2326
|
+
* <div class="norender">
|
|
2327
|
+
* <code>
|
|
2328
|
+
* function setup() {
|
|
2329
|
+
* // Create a p5.Vector object.
|
|
2330
|
+
* let v = createVector(1, 1, 1);
|
|
2331
|
+
*
|
|
2332
|
+
* // Interpolate.
|
|
2333
|
+
* v.lerp(3, 3, 3, 0.5);
|
|
2334
|
+
*
|
|
2335
|
+
* // Prints "p5.Vector Object : [2, 2, 2]" to the console.
|
|
2336
|
+
* print(v.toString());
|
|
2337
|
+
* }
|
|
2338
|
+
* </code>
|
|
2339
|
+
* </div>
|
|
2340
|
+
*
|
|
2341
|
+
* <div class="norender">
|
|
2342
|
+
* <code>
|
|
2343
|
+
* function setup() {
|
|
2344
|
+
* // Create p5.Vector objects.
|
|
2345
|
+
* let v0 = createVector(1, 1, 1);
|
|
2346
|
+
* let v1 = createVector(3, 3, 3);
|
|
2347
|
+
*
|
|
2348
|
+
* // Interpolate.
|
|
2349
|
+
* let v2 = p5.Vector.lerp(v0, v1, 0.5);
|
|
2350
|
+
*
|
|
2351
|
+
* // Prints "p5.Vector Object : [2, 2, 2]" to the console.
|
|
2352
|
+
* print(v2.toString());
|
|
2353
|
+
* }
|
|
2354
|
+
* </code>
|
|
2355
|
+
* </div>
|
|
2356
|
+
*
|
|
2357
|
+
* <div>
|
|
2358
|
+
* <code>
|
|
2359
|
+
* function setup() {
|
|
2360
|
+
* createCanvas(100, 100);
|
|
2361
|
+
*
|
|
2362
|
+
* describe('Three arrows extend from the center of a gray square. A red arrow points to the right, a blue arrow points down, and a purple arrow points to the bottom right.');
|
|
2363
|
+
* }
|
|
2364
|
+
* function draw() {
|
|
2365
|
+
* background(200);
|
|
2366
|
+
*
|
|
2367
|
+
* // Create p5.Vector objects.
|
|
2368
|
+
* let v0 = createVector(50, 50);
|
|
2369
|
+
* let v1 = createVector(30, 0);
|
|
2370
|
+
* let v2 = createVector(0, 30);
|
|
2371
|
+
*
|
|
2372
|
+
* // Interpolate.
|
|
2373
|
+
* let v3 = p5.Vector.lerp(v1, v2, 0.5);
|
|
2374
|
+
*
|
|
2375
|
+
* // Draw the red arrow.
|
|
2376
|
+
* drawArrow(v0, v1, 'red');
|
|
2377
|
+
*
|
|
2378
|
+
* // Draw the blue arrow.
|
|
2379
|
+
* drawArrow(v0, v2, 'blue');
|
|
2380
|
+
*
|
|
2381
|
+
* // Draw the purple arrow.
|
|
2382
|
+
* drawArrow(v0, v3, 'purple');
|
|
2383
|
+
* }
|
|
2384
|
+
*
|
|
2385
|
+
* // Draws an arrow between two vectors.
|
|
2386
|
+
* function drawArrow(base, vec, myColor) {
|
|
2387
|
+
* push();
|
|
2388
|
+
* stroke(myColor);
|
|
2389
|
+
* strokeWeight(3);
|
|
2390
|
+
* fill(myColor);
|
|
2391
|
+
* translate(base.x, base.y);
|
|
2392
|
+
* line(0, 0, vec.x, vec.y);
|
|
2393
|
+
* rotate(vec.heading());
|
|
2394
|
+
* let arrowSize = 7;
|
|
2395
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2396
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2397
|
+
* pop();
|
|
2398
|
+
* }
|
|
2399
|
+
* </code>
|
|
2400
|
+
* </div>
|
|
2401
|
+
*/
|
|
2402
|
+
function lerp(x: number, y: number, z: number, amt: number): void;
|
|
2403
|
+
|
|
2404
|
+
/**
|
|
2405
|
+
* @param <a href="#/p5.Vector">p5.Vector</a> to lerp toward.
|
|
2406
|
+
*/
|
|
2407
|
+
function lerp(x: any, y: any, z: any, amt: number, v: p5.Vector): void;
|
|
2408
|
+
|
|
2409
|
+
/**
|
|
2410
|
+
* Calculates a new heading and magnitude that are between two vectors.The `amt` parameter is the amount to interpolate between the old vector and
|
|
2411
|
+
* the new vector. 0.0 keeps the heading and magnitude equal to the old
|
|
2412
|
+
* vector's, 0.5 sets them halfway between, and 1.0 sets the heading and
|
|
2413
|
+
* magnitude equal to the new vector's.`slerp()` differs from lerp() because
|
|
2414
|
+
* it interpolates magnitude. Calling `v0.slerp(v1, 0.5)` sets `v0`'s
|
|
2415
|
+
* magnitude to a value halfway between its original magnitude and `v1`'s.
|
|
2416
|
+
* Calling `v0.lerp(v1, 0.5)` makes no such guarantee.The static version of `slerp()`, as in `p5.Vector.slerp(v0, v1, 0.5)`,
|
|
2417
|
+
* returns a new p5.Vector object and doesn't change
|
|
2418
|
+
* the original.
|
|
2419
|
+
*
|
|
2420
|
+
* @param <a href="#/p5.Vector">p5.Vector</a> to slerp toward.
|
|
2421
|
+
* @param amount of interpolation between 0.0 (old vector)
|
|
2422
|
+
* and 1.0 (new vector). 0.5 is halfway between.
|
|
2423
|
+
* @example <div class="norender">
|
|
2424
|
+
* <code>
|
|
2425
|
+
* function setup() {
|
|
2426
|
+
* // Create a p5.Vector object.
|
|
2427
|
+
* let v0 = createVector(3, 0);
|
|
2428
|
+
*
|
|
2429
|
+
* // Prints "3" to the console.
|
|
2430
|
+
* print(v0.mag());
|
|
2431
|
+
*
|
|
2432
|
+
* // Prints "0" to the console.
|
|
2433
|
+
* print(v0.heading());
|
|
2434
|
+
*
|
|
2435
|
+
* // Create a p5.Vector object.
|
|
2436
|
+
* let v1 = createVector(0, 1);
|
|
2437
|
+
*
|
|
2438
|
+
* // Prints "1" to the console.
|
|
2439
|
+
* print(v1.mag());
|
|
2440
|
+
*
|
|
2441
|
+
* // Prints "1.570..." to the console.
|
|
2442
|
+
* print(v1.heading());
|
|
2443
|
+
*
|
|
2444
|
+
* // Interpolate halfway between v0 and v1.
|
|
2445
|
+
* v0.slerp(v1, 0.5);
|
|
2446
|
+
*
|
|
2447
|
+
* // Prints "2" to the console.
|
|
2448
|
+
* print(v0.mag());
|
|
2449
|
+
*
|
|
2450
|
+
* // Prints "0.785..." to the console.
|
|
2451
|
+
* print(v0.heading());
|
|
2452
|
+
* }
|
|
2453
|
+
* </code>
|
|
2454
|
+
* </div>
|
|
2455
|
+
*
|
|
2456
|
+
* <div class="norender">
|
|
2457
|
+
* <code>
|
|
2458
|
+
* function setup() {
|
|
2459
|
+
* // Create a p5.Vector object.
|
|
2460
|
+
* let v0 = createVector(3, 0);
|
|
2461
|
+
*
|
|
2462
|
+
* // Prints "3" to the console.
|
|
2463
|
+
* print(v0.mag());
|
|
2464
|
+
*
|
|
2465
|
+
* // Prints "0" to the console.
|
|
2466
|
+
* print(v0.heading());
|
|
2467
|
+
*
|
|
2468
|
+
* // Create a p5.Vector object.
|
|
2469
|
+
* let v1 = createVector(0, 1);
|
|
2470
|
+
*
|
|
2471
|
+
* // Prints "1" to the console.
|
|
2472
|
+
* print(v1.mag());
|
|
2473
|
+
*
|
|
2474
|
+
* // Prints "1.570..." to the console.
|
|
2475
|
+
* print(v1.heading());
|
|
2476
|
+
*
|
|
2477
|
+
* // Create a p5.Vector that's halfway between v0 and v1.
|
|
2478
|
+
* let v3 = p5.Vector.slerp(v0, v1, 0.5);
|
|
2479
|
+
*
|
|
2480
|
+
* // Prints "2" to the console.
|
|
2481
|
+
* print(v3.mag());
|
|
2482
|
+
*
|
|
2483
|
+
* // Prints "0.785..." to the console.
|
|
2484
|
+
* print(v3.heading());
|
|
2485
|
+
* }
|
|
2486
|
+
* </code>
|
|
2487
|
+
* </div>
|
|
2488
|
+
*
|
|
2489
|
+
* <div>
|
|
2490
|
+
* <code>
|
|
2491
|
+
* function setup() {
|
|
2492
|
+
* createCanvas(100, 100);
|
|
2493
|
+
*
|
|
2494
|
+
* describe('Three arrows extend from the center of a gray square. A red arrow points to the right, a blue arrow points to the left, and a purple arrow points down.');
|
|
2495
|
+
* }
|
|
2496
|
+
*
|
|
2497
|
+
* function draw() {
|
|
2498
|
+
* background(200);
|
|
2499
|
+
*
|
|
2500
|
+
* // Create p5.Vector objects.
|
|
2501
|
+
* let v0 = createVector(50, 50);
|
|
2502
|
+
* let v1 = createVector(20, 0);
|
|
2503
|
+
* let v2 = createVector(-40, 0);
|
|
2504
|
+
*
|
|
2505
|
+
* // Create a p5.Vector that's halfway between v1 and v2.
|
|
2506
|
+
* let v3 = p5.Vector.slerp(v1, v2, 0.5);
|
|
2507
|
+
*
|
|
2508
|
+
* // Draw the red arrow.
|
|
2509
|
+
* drawArrow(v0, v1, 'red');
|
|
2510
|
+
*
|
|
2511
|
+
* // Draw the blue arrow.
|
|
2512
|
+
* drawArrow(v0, v2, 'blue');
|
|
2513
|
+
*
|
|
2514
|
+
* // Draw the purple arrow.
|
|
2515
|
+
* drawArrow(v0, v3, 'purple');
|
|
2516
|
+
* }
|
|
2517
|
+
*
|
|
2518
|
+
* // Draws an arrow between two vectors.
|
|
2519
|
+
* function drawArrow(base, vec, myColor) {
|
|
2520
|
+
* push();
|
|
2521
|
+
* stroke(myColor);
|
|
2522
|
+
* strokeWeight(3);
|
|
2523
|
+
* fill(myColor);
|
|
2524
|
+
* translate(base.x, base.y);
|
|
2525
|
+
* line(0, 0, vec.x, vec.y);
|
|
2526
|
+
* rotate(vec.heading());
|
|
2527
|
+
* let arrowSize = 7;
|
|
2528
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2529
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2530
|
+
* pop();
|
|
2531
|
+
* }
|
|
2532
|
+
* </code>
|
|
2533
|
+
* </div>
|
|
2534
|
+
*/
|
|
2535
|
+
function slerp(v: p5.Vector, amt: number): p5.Vector;
|
|
2536
|
+
|
|
2537
|
+
/**
|
|
2538
|
+
* Reflects a vector about a line in 2D or a plane in 3D.The orientation of the line or plane is described by a normal vector that
|
|
2539
|
+
* points away from the shape.The static version of `reflect()`, as in `p5.Vector.reflect(v, n)`,
|
|
2540
|
+
* returns a new p5.Vector object and doesn't change
|
|
2541
|
+
* the original.
|
|
2542
|
+
*
|
|
2543
|
+
* @param <a href="#/p5.Vector">p5.Vector</a>
|
|
2544
|
+
* to reflect about.
|
|
2545
|
+
* @example <div class="norender">
|
|
2546
|
+
* <code>
|
|
2547
|
+
* function setup() {
|
|
2548
|
+
* // Create a normal vector.
|
|
2549
|
+
* let n = createVector(0, 1);
|
|
2550
|
+
* // Create a vector to reflect.
|
|
2551
|
+
* let v = createVector(4, 6);
|
|
2552
|
+
*
|
|
2553
|
+
* // Reflect v about n.
|
|
2554
|
+
* v.reflect(n);
|
|
2555
|
+
*
|
|
2556
|
+
* // Prints "p5.Vector Object : [4, -6, 0]" to the console.
|
|
2557
|
+
* print(v.toString());
|
|
2558
|
+
* }
|
|
2559
|
+
* </code>
|
|
2560
|
+
* </div>
|
|
2561
|
+
*
|
|
2562
|
+
* <div class="norender">
|
|
2563
|
+
* <code>
|
|
2564
|
+
* function setup() {
|
|
2565
|
+
* // Create a normal vector.
|
|
2566
|
+
* let n = createVector(0, 1);
|
|
2567
|
+
*
|
|
2568
|
+
* // Create a vector to reflect.
|
|
2569
|
+
* let v0 = createVector(4, 6);
|
|
2570
|
+
*
|
|
2571
|
+
* // Create a reflected vector.
|
|
2572
|
+
* let v1 = p5.Vector.reflect(v0, n);
|
|
2573
|
+
*
|
|
2574
|
+
* // Prints "p5.Vector Object : [4, -6, 0]" to the console.
|
|
2575
|
+
* print(v1.toString());
|
|
2576
|
+
* }
|
|
2577
|
+
* </code>
|
|
2578
|
+
* </div>
|
|
2579
|
+
*
|
|
2580
|
+
* <div>
|
|
2581
|
+
* <code>
|
|
2582
|
+
* function setup() {
|
|
2583
|
+
* createCanvas(100, 100);
|
|
2584
|
+
*
|
|
2585
|
+
* describe('Three arrows extend from the center of a gray square with a vertical line down its middle. A black arrow points to the right, a blue arrow points to the bottom left, and a red arrow points to the bottom right.');
|
|
2586
|
+
* }
|
|
2587
|
+
* function draw() {
|
|
2588
|
+
* background(200);
|
|
2589
|
+
*
|
|
2590
|
+
* // Draw a vertical line.
|
|
2591
|
+
* line(50, 0, 50, 100);
|
|
2592
|
+
*
|
|
2593
|
+
* // Create a normal vector.
|
|
2594
|
+
* let n = createVector(1, 0);
|
|
2595
|
+
*
|
|
2596
|
+
* // Center.
|
|
2597
|
+
* let v0 = createVector(50, 50);
|
|
2598
|
+
*
|
|
2599
|
+
* // Create a vector to reflect.
|
|
2600
|
+
* let v1 = createVector(30, 40);
|
|
2601
|
+
*
|
|
2602
|
+
* // Create a reflected vector.
|
|
2603
|
+
* let v2 = p5.Vector.reflect(v1, n);
|
|
2604
|
+
*
|
|
2605
|
+
* // Scale the normal vector for drawing.
|
|
2606
|
+
* n.setMag(30);
|
|
2607
|
+
*
|
|
2608
|
+
* // Draw the black arrow.
|
|
2609
|
+
* drawArrow(v0, n, 'black');
|
|
2610
|
+
*
|
|
2611
|
+
* // Draw the red arrow.
|
|
2612
|
+
* drawArrow(v0, v1, 'red');
|
|
2613
|
+
*
|
|
2614
|
+
* // Draw the blue arrow.
|
|
2615
|
+
* drawArrow(v0, v2, 'blue');
|
|
2616
|
+
* }
|
|
2617
|
+
*
|
|
2618
|
+
* // Draws an arrow between two vectors.
|
|
2619
|
+
* function drawArrow(base, vec, myColor) {
|
|
2620
|
+
* push();
|
|
2621
|
+
* stroke(myColor);
|
|
2622
|
+
* strokeWeight(3);
|
|
2623
|
+
* fill(myColor);
|
|
2624
|
+
* translate(base.x, base.y);
|
|
2625
|
+
* line(0, 0, vec.x, vec.y);
|
|
2626
|
+
* rotate(vec.heading());
|
|
2627
|
+
* let arrowSize = 7;
|
|
2628
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2629
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2630
|
+
* pop();
|
|
2631
|
+
* }
|
|
2632
|
+
* </code>
|
|
2633
|
+
* </div>
|
|
2634
|
+
*/
|
|
2635
|
+
function reflect(surfaceNormal: p5.Vector): void;
|
|
2636
|
+
|
|
2637
|
+
/**
|
|
2638
|
+
* Returns the vector's components as an array of numbers.
|
|
2639
|
+
*
|
|
2640
|
+
* @return array with the vector's components.
|
|
2641
|
+
* @example <div class = "norender">
|
|
2642
|
+
* <code>
|
|
2643
|
+
* function setup() {
|
|
2644
|
+
* // Create a p5.Vector object.
|
|
2645
|
+
* let v = createVector(20, 30);
|
|
2646
|
+
*
|
|
2647
|
+
* // Prints "[20, 30, 0]" to the console.
|
|
2648
|
+
* print(v.array());
|
|
2649
|
+
* }
|
|
2650
|
+
* </code>
|
|
2651
|
+
* </div>
|
|
2652
|
+
*/
|
|
2653
|
+
function array(): number[];
|
|
2654
|
+
|
|
2655
|
+
/**
|
|
2656
|
+
* Checks whether all the vector's components are equal to another vector's.`equals()` returns `true` if the vector's components are all the same as another
|
|
2657
|
+
* vector's and `false` if not.The version of `equals()` with one parameter interprets it as another
|
|
2658
|
+
* p5.Vector object.The version of `equals()` with multiple parameters interprets them as the
|
|
2659
|
+
* components of another vector. Any missing parameters are assigned the value
|
|
2660
|
+
* 0.The static version of `equals()`, as in `p5.Vector.equals(v0, v1)`,
|
|
2661
|
+
* interprets both parameters as p5.Vector objects.
|
|
2662
|
+
*
|
|
2663
|
+
* @param x component of the vector.
|
|
2664
|
+
* @param y component of the vector.
|
|
2665
|
+
* @param z component of the vector.
|
|
2666
|
+
* @return whether the vectors are equal.
|
|
2667
|
+
* @example <div class="norender">
|
|
2668
|
+
* <code>
|
|
2669
|
+
* function setup() {
|
|
2670
|
+
* // Create p5.Vector objects.
|
|
2671
|
+
* let v0 = createVector(10, 20, 30);
|
|
2672
|
+
* let v1 = createVector(10, 20, 30);
|
|
2673
|
+
* let v2 = createVector(0, 0, 0);
|
|
2674
|
+
*
|
|
2675
|
+
* // Prints "true" to the console.
|
|
2676
|
+
* print(v0.equals(v1));
|
|
2677
|
+
*
|
|
2678
|
+
* // Prints "false" to the console.
|
|
2679
|
+
* print(v0.equals(v2));
|
|
2680
|
+
* }
|
|
2681
|
+
* </code>
|
|
2682
|
+
* </div>
|
|
2683
|
+
*
|
|
2684
|
+
* <div class = "norender">
|
|
2685
|
+
* <code>
|
|
2686
|
+
* function setup() {
|
|
2687
|
+
* // Create p5.Vector objects.
|
|
2688
|
+
* let v0 = createVector(5, 10, 20);
|
|
2689
|
+
* let v1 = createVector(5, 10, 20);
|
|
2690
|
+
* let v2 = createVector(13, 10, 19);
|
|
2691
|
+
*
|
|
2692
|
+
* // Prints "true" to the console.
|
|
2693
|
+
* print(v0.equals(v1.x, v1.y, v1.z));
|
|
2694
|
+
*
|
|
2695
|
+
* // Prints "false" to the console.
|
|
2696
|
+
* print(v0.equals(v2.x, v2.y, v2.z));
|
|
2697
|
+
* }
|
|
2698
|
+
* </code>
|
|
2699
|
+
* </div>
|
|
2700
|
+
*
|
|
2701
|
+
* <div class="norender">
|
|
2702
|
+
* <code>
|
|
2703
|
+
* function setup() {
|
|
2704
|
+
* // Create p5.Vector objects.
|
|
2705
|
+
* let v0 = createVector(10, 20, 30);
|
|
2706
|
+
* let v1 = createVector(10, 20, 30);
|
|
2707
|
+
* let v2 = createVector(0, 0, 0);
|
|
2708
|
+
*
|
|
2709
|
+
* // Prints "true" to the console.
|
|
2710
|
+
* print(p5.Vector.equals(v0, v1));
|
|
2711
|
+
*
|
|
2712
|
+
* // Prints "false" to the console.
|
|
2713
|
+
* print(p5.Vector.equals(v0, v2));
|
|
2714
|
+
* }
|
|
2715
|
+
* </code>
|
|
2716
|
+
* </div>
|
|
2717
|
+
*/
|
|
2718
|
+
function equals(...args: any[], x?: number, y?: number, z?: number): boolean;
|
|
2719
|
+
|
|
2720
|
+
/**
|
|
2721
|
+
* @param vector to compare.
|
|
2722
|
+
*/
|
|
2723
|
+
function equals(...args: any[], value: p5.Vector | Array): boolean;
|
|
2724
|
+
|
|
2725
|
+
/**
|
|
2726
|
+
* Creates a new 2D vector from an angle.
|
|
2727
|
+
*
|
|
2728
|
+
* @param desired angle, in radians. Unaffected by <a href="#/p5/angleMode">angleMode()</a>.
|
|
2729
|
+
* @param length of the new vector (defaults to 1).
|
|
2730
|
+
* @return new <a href="#/p5.Vector">p5.Vector</a> object.
|
|
2731
|
+
* @example <div class="norender">
|
|
2732
|
+
* <code>
|
|
2733
|
+
* function setup() {
|
|
2734
|
+
* // Create a p5.Vector object.
|
|
2735
|
+
* let v = p5.Vector.fromAngle(0);
|
|
2736
|
+
*
|
|
2737
|
+
* // Prints "p5.Vector Object : [1, 0, 0]" to the console.
|
|
2738
|
+
* print(v.toString());
|
|
2739
|
+
* }
|
|
2740
|
+
* </code>
|
|
2741
|
+
* </div>
|
|
2742
|
+
*
|
|
2743
|
+
* <div class="norender">
|
|
2744
|
+
* <code>
|
|
2745
|
+
* function setup() {
|
|
2746
|
+
* // Create a p5.Vector object.
|
|
2747
|
+
* let v = p5.Vector.fromAngle(0, 30);
|
|
2748
|
+
*
|
|
2749
|
+
* // Prints "p5.Vector Object : [30, 0, 0]" to the console.
|
|
2750
|
+
* print(v.toString());
|
|
2751
|
+
* }
|
|
2752
|
+
* </code>
|
|
2753
|
+
* </div>
|
|
2754
|
+
*
|
|
2755
|
+
* <div>
|
|
2756
|
+
* <code>
|
|
2757
|
+
* function setup() {
|
|
2758
|
+
* createCanvas(100, 100);
|
|
2759
|
+
*
|
|
2760
|
+
* describe('A black arrow extends from the center of a gray square. It points to the right.');
|
|
2761
|
+
* }
|
|
2762
|
+
* function draw() {
|
|
2763
|
+
* background(200);
|
|
2764
|
+
*
|
|
2765
|
+
* // Create a p5.Vector to the center.
|
|
2766
|
+
* let v0 = createVector(50, 50);
|
|
2767
|
+
*
|
|
2768
|
+
* // Create a p5.Vector with an angle 0 and magnitude 30.
|
|
2769
|
+
* let v1 = p5.Vector.fromAngle(0, 30);
|
|
2770
|
+
*
|
|
2771
|
+
* // Draw the black arrow.
|
|
2772
|
+
* drawArrow(v0, v1, 'black');
|
|
2773
|
+
* }
|
|
2774
|
+
*
|
|
2775
|
+
* // Draws an arrow between two vectors.
|
|
2776
|
+
* function drawArrow(base, vec, myColor) {
|
|
2777
|
+
* push();
|
|
2778
|
+
* stroke(myColor);
|
|
2779
|
+
* strokeWeight(3);
|
|
2780
|
+
* fill(myColor);
|
|
2781
|
+
* translate(base.x, base.y);
|
|
2782
|
+
* line(0, 0, vec.x, vec.y);
|
|
2783
|
+
* rotate(vec.heading());
|
|
2784
|
+
* let arrowSize = 7;
|
|
2785
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2786
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2787
|
+
* pop();
|
|
2788
|
+
* }
|
|
2789
|
+
* </code>
|
|
2790
|
+
* </div>
|
|
2791
|
+
*/
|
|
2792
|
+
function fromAngle(angle: number, length?: number): p5.Vector;
|
|
2793
|
+
|
|
2794
|
+
/**
|
|
2795
|
+
* Creates a new 3D vector from a pair of ISO spherical angles.
|
|
2796
|
+
*
|
|
2797
|
+
* @param polar angle in radians (zero is up).
|
|
2798
|
+
* @param azimuthal angle in radians
|
|
2799
|
+
* (zero is out of the screen).
|
|
2800
|
+
* @param length of the new vector (defaults to 1).
|
|
2801
|
+
* @return new <a href="#/p5.Vector">p5.Vector</a> object.
|
|
2802
|
+
* @example <div class="norender">
|
|
2803
|
+
* <code>
|
|
2804
|
+
* function setup() {
|
|
2805
|
+
* // Create a p5.Vector object.
|
|
2806
|
+
* let v = p5.Vector.fromAngles(0, 0);
|
|
2807
|
+
*
|
|
2808
|
+
* // Prints "p5.Vector Object : [0, -1, 0]" to the console.
|
|
2809
|
+
* print(v.toString());
|
|
2810
|
+
* }
|
|
2811
|
+
* </code>
|
|
2812
|
+
* </div>
|
|
2813
|
+
*
|
|
2814
|
+
* <div>
|
|
2815
|
+
* <code>
|
|
2816
|
+
* function setup() {
|
|
2817
|
+
* createCanvas(100, 100, WEBGL);
|
|
2818
|
+
*
|
|
2819
|
+
* describe('A light shines on a pink sphere as it orbits.');
|
|
2820
|
+
* }
|
|
2821
|
+
*
|
|
2822
|
+
* function draw() {
|
|
2823
|
+
* background(0);
|
|
2824
|
+
*
|
|
2825
|
+
* // Calculate the ISO angles.
|
|
2826
|
+
* let theta = frameCount * 0.05;
|
|
2827
|
+
* let phi = 0;
|
|
2828
|
+
*
|
|
2829
|
+
* // Create a p5.Vector object.
|
|
2830
|
+
* let v = p5.Vector.fromAngles(theta, phi, 100);
|
|
2831
|
+
*
|
|
2832
|
+
* // Create a point light using the p5.Vector.
|
|
2833
|
+
* let c = color('deeppink');
|
|
2834
|
+
* pointLight(c, v);
|
|
2835
|
+
*
|
|
2836
|
+
* // Style the sphere.
|
|
2837
|
+
* fill(255);
|
|
2838
|
+
* noStroke();
|
|
2839
|
+
*
|
|
2840
|
+
* // Draw the sphere.
|
|
2841
|
+
* sphere(35);
|
|
2842
|
+
* }
|
|
2843
|
+
* </code>
|
|
2844
|
+
* </div>
|
|
2845
|
+
*/
|
|
2846
|
+
function fromAngles(theta: number, phi: number, length?: number): p5.Vector;
|
|
2847
|
+
|
|
2848
|
+
/**
|
|
2849
|
+
* Creates a new 2D unit vector with a random heading.
|
|
2850
|
+
*
|
|
2851
|
+
* @return new <a href="#/p5.Vector">p5.Vector</a> object.
|
|
2852
|
+
* @example <div class="norender">
|
|
2853
|
+
* <code>
|
|
2854
|
+
* function setup() {
|
|
2855
|
+
* // Create a p5.Vector object.
|
|
2856
|
+
* let v = p5.Vector.random2D();
|
|
2857
|
+
*
|
|
2858
|
+
* // Prints "p5.Vector Object : [x, y, 0]" to the console
|
|
2859
|
+
* // where x and y are small random numbers.
|
|
2860
|
+
* print(v.toString());
|
|
2861
|
+
* }
|
|
2862
|
+
* </code>
|
|
2863
|
+
* </div>
|
|
2864
|
+
*
|
|
2865
|
+
* <div>
|
|
2866
|
+
* <code>
|
|
2867
|
+
* function setup() {
|
|
2868
|
+
* createCanvas(100, 100);
|
|
2869
|
+
*
|
|
2870
|
+
* // Slow the frame rate.
|
|
2871
|
+
* frameRate(1);
|
|
2872
|
+
*
|
|
2873
|
+
* describe('A black arrow in extends from the center of a gray square. It changes direction once per second.');
|
|
2874
|
+
* }
|
|
2875
|
+
*
|
|
2876
|
+
* function draw() {
|
|
2877
|
+
* background(200);
|
|
2878
|
+
*
|
|
2879
|
+
* // Create a p5.Vector to the center.
|
|
2880
|
+
* let v0 = createVector(50, 50);
|
|
2881
|
+
*
|
|
2882
|
+
* // Create a random p5.Vector.
|
|
2883
|
+
* let v1 = p5.Vector.random2D();
|
|
2884
|
+
*
|
|
2885
|
+
* // Scale v1 for drawing.
|
|
2886
|
+
* v1.mult(30);
|
|
2887
|
+
*
|
|
2888
|
+
* // Draw the black arrow.
|
|
2889
|
+
* drawArrow(v0, v1, 'black');
|
|
2890
|
+
* }
|
|
2891
|
+
*
|
|
2892
|
+
* // Draws an arrow between two vectors.
|
|
2893
|
+
* function drawArrow(base, vec, myColor) {
|
|
2894
|
+
* push();
|
|
2895
|
+
* stroke(myColor);
|
|
2896
|
+
* strokeWeight(3);
|
|
2897
|
+
* fill(myColor);
|
|
2898
|
+
* translate(base.x, base.y);
|
|
2899
|
+
* line(0, 0, vec.x, vec.y);
|
|
2900
|
+
* rotate(vec.heading());
|
|
2901
|
+
* let arrowSize = 7;
|
|
2902
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
2903
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
2904
|
+
* pop();
|
|
2905
|
+
* }
|
|
2906
|
+
* </code>
|
|
2907
|
+
* </div>
|
|
2908
|
+
*/
|
|
2909
|
+
function random2D(): p5.Vector;
|
|
2910
|
+
|
|
2911
|
+
/**
|
|
2912
|
+
* Creates a new 3D unit vector with a random heading.
|
|
2913
|
+
*
|
|
2914
|
+
* @return new <a href="#/p5.Vector">p5.Vector</a> object.
|
|
2915
|
+
* @example <div class="norender">
|
|
2916
|
+
* <code>
|
|
2917
|
+
* function setup() {
|
|
2918
|
+
* // Create a p5.Vector object.
|
|
2919
|
+
* let v = p5.Vector.random3D();
|
|
2920
|
+
*
|
|
2921
|
+
* // Prints "p5.Vector Object : [x, y, z]" to the console
|
|
2922
|
+
* // where x, y, and z are small random numbers.
|
|
2923
|
+
* print(v.toString());
|
|
2924
|
+
* }
|
|
2925
|
+
* </code>
|
|
2926
|
+
* </div>
|
|
2927
|
+
*/
|
|
2928
|
+
function random3D(): p5.Vector;
|
|
2929
|
+
|
|
2930
|
+
/**
|
|
2931
|
+
* @param the <a href="#/p5.Vector">p5.Vector</a> to create a copy of
|
|
2932
|
+
* @return the copy of the <a href="#/p5.Vector">p5.Vector</a> object
|
|
2933
|
+
*/
|
|
2934
|
+
function copy(v: p5.Vector): p5.Vector;
|
|
2935
|
+
|
|
2936
|
+
/**
|
|
2937
|
+
* @param A <a href="#/p5.Vector">p5.Vector</a> to add
|
|
2938
|
+
* @param A <a href="#/p5.Vector">p5.Vector</a> to add
|
|
2939
|
+
* @param vector to receive the result.
|
|
2940
|
+
* @return resulting <a href="#/p5.Vector">p5.Vector</a>.
|
|
2941
|
+
*/
|
|
2942
|
+
function add(v1: p5.Vector, v2: p5.Vector, target?: p5.Vector): p5.Vector;
|
|
2943
|
+
|
|
2944
|
+
/**
|
|
2945
|
+
* @param The dividend <a href="#/p5.Vector">p5.Vector</a>
|
|
2946
|
+
* @param The divisor <a href="#/p5.Vector">p5.Vector</a>
|
|
2947
|
+
*/
|
|
2948
|
+
function rem(v1: p5.Vector, v2: p5.Vector): void;
|
|
2949
|
+
|
|
2950
|
+
/**
|
|
2951
|
+
* @return The resulting <a href="#/p5.Vector">p5.Vector</a>
|
|
2952
|
+
*/
|
|
2953
|
+
function rem(v1: p5.Vector, v2: p5.Vector): p5.Vector;
|
|
2954
|
+
|
|
2955
|
+
/**
|
|
2956
|
+
* @param A <a href="#/p5.Vector">p5.Vector</a> to subtract from
|
|
2957
|
+
* @param A <a href="#/p5.Vector">p5.Vector</a> to subtract
|
|
2958
|
+
* @param vector to receive the result.
|
|
2959
|
+
* @return The resulting <a href="#/p5.Vector">p5.Vector</a>
|
|
2960
|
+
*/
|
|
2961
|
+
function sub(v1: p5.Vector, v2: p5.Vector, target?: p5.Vector): p5.Vector;
|
|
2962
|
+
|
|
2963
|
+
/**
|
|
2964
|
+
* Multiplies a vector by a scalar and returns a new vector.
|
|
2965
|
+
*
|
|
2966
|
+
*/
|
|
2967
|
+
function mult(v: any, n: any, target: any): void;
|
|
2968
|
+
|
|
2969
|
+
/**
|
|
2970
|
+
* @return resulting new <a href="#/p5.Vector">p5.Vector</a>.
|
|
2971
|
+
*/
|
|
2972
|
+
function mult(v: any, n: any, target: any, x: number, y: number, z?: number): p5.Vector;
|
|
2973
|
+
|
|
2974
|
+
/**
|
|
2975
|
+
* @param vector to receive the result.
|
|
2976
|
+
*/
|
|
2977
|
+
function mult(v: p5.Vector, n: number, target?: p5.Vector): void;
|
|
2978
|
+
|
|
2979
|
+
/**
|
|
2980
|
+
*/
|
|
2981
|
+
function mult(v: any, n: any, target?: p5.Vector, v0: p5.Vector, v1: p5.Vector): void;
|
|
2982
|
+
|
|
2983
|
+
/**
|
|
2984
|
+
*/
|
|
2985
|
+
function mult(v: any, n: any, target?: p5.Vector, v0: p5.Vector, arr: number[]): void;
|
|
2986
|
+
|
|
2987
|
+
/**
|
|
2988
|
+
* Rotates the vector (only 2D vectors) by the given angle; magnitude remains the same. Returns a new vector.
|
|
2989
|
+
*
|
|
2990
|
+
*/
|
|
2991
|
+
function rotate(v: any, a: any, target: any): void;
|
|
2992
|
+
|
|
2993
|
+
/**
|
|
2994
|
+
* @param The vector to receive the result
|
|
2995
|
+
*/
|
|
2996
|
+
function rotate(v: p5.Vector, a: any, target?: p5.Vector, angle: number): void;
|
|
2997
|
+
|
|
2998
|
+
/**
|
|
2999
|
+
* Divides a vector by a scalar and returns a new vector.
|
|
3000
|
+
*
|
|
3001
|
+
*/
|
|
3002
|
+
function div(v: any, n: any, target: any): void;
|
|
3003
|
+
|
|
3004
|
+
/**
|
|
3005
|
+
* @return The resulting new <a href="#/p5.Vector">p5.Vector</a>
|
|
3006
|
+
*/
|
|
3007
|
+
function div(v: any, n: any, target: any, x: number, y: number, z?: number): p5.Vector;
|
|
3008
|
+
|
|
3009
|
+
/**
|
|
3010
|
+
* @param The vector to receive the result
|
|
3011
|
+
*/
|
|
3012
|
+
function div(v: p5.Vector, n: number, target?: p5.Vector): void;
|
|
3013
|
+
|
|
3014
|
+
/**
|
|
3015
|
+
*/
|
|
3016
|
+
function div(v: any, n: any, target?: p5.Vector, v0: p5.Vector, v1: p5.Vector): void;
|
|
3017
|
+
|
|
3018
|
+
/**
|
|
3019
|
+
*/
|
|
3020
|
+
function div(v: any, n: any, target?: p5.Vector, v0: p5.Vector, arr: number[]): void;
|
|
3021
|
+
|
|
3022
|
+
/**
|
|
3023
|
+
* Calculates the dot product of two vectors.
|
|
3024
|
+
*
|
|
3025
|
+
*/
|
|
3026
|
+
function dot(v1: any, v2: any): void;
|
|
3027
|
+
|
|
3028
|
+
/**
|
|
3029
|
+
* @param first <a href="#/p5.Vector">p5.Vector</a>.
|
|
3030
|
+
* @param second <a href="#/p5.Vector">p5.Vector</a>.
|
|
3031
|
+
* @return dot product.
|
|
3032
|
+
*/
|
|
3033
|
+
function dot(v1: p5.Vector, v2: p5.Vector): number;
|
|
3034
|
+
|
|
3035
|
+
/**
|
|
3036
|
+
* Calculates the cross product of two vectors.
|
|
3037
|
+
*
|
|
3038
|
+
*/
|
|
3039
|
+
function cross(v1: any, v2: any): void;
|
|
3040
|
+
|
|
3041
|
+
/**
|
|
3042
|
+
* @param first <a href="#/p5.Vector">p5.Vector</a>.
|
|
3043
|
+
* @param second <a href="#/p5.Vector">p5.Vector</a>.
|
|
3044
|
+
* @return cross product.
|
|
3045
|
+
*/
|
|
3046
|
+
function cross(v1: p5.Vector, v2: p5.Vector): number;
|
|
3047
|
+
|
|
3048
|
+
/**
|
|
3049
|
+
* Calculates the Euclidean distance between two points (considering a
|
|
3050
|
+
* point as a vector object).
|
|
3051
|
+
*
|
|
3052
|
+
*/
|
|
3053
|
+
function dist(v1: any, v2: any): void;
|
|
3054
|
+
|
|
3055
|
+
/**
|
|
3056
|
+
* @param The first <a href="#/p5.Vector">p5.Vector</a>
|
|
3057
|
+
* @param The second <a href="#/p5.Vector">p5.Vector</a>
|
|
3058
|
+
* @return The distance
|
|
3059
|
+
*/
|
|
3060
|
+
function dist(v1: p5.Vector, v2: p5.Vector): number;
|
|
3061
|
+
|
|
3062
|
+
/**
|
|
3063
|
+
* Linear interpolate a vector to another vector and return the result as a
|
|
3064
|
+
* new vector.
|
|
3065
|
+
*
|
|
3066
|
+
*/
|
|
3067
|
+
function lerp(v1: any, v2: any, amt: any, target: any): void;
|
|
3068
|
+
|
|
3069
|
+
/**
|
|
3070
|
+
* @param The vector to receive the result
|
|
3071
|
+
* @return The lerped value
|
|
3072
|
+
*/
|
|
3073
|
+
function lerp(v1: p5.Vector, v2: p5.Vector, amt: number, target?: p5.Vector): p5.Vector;
|
|
3074
|
+
|
|
3075
|
+
/**
|
|
3076
|
+
* Performs spherical linear interpolation with the other vector
|
|
3077
|
+
* and returns the resulting vector.
|
|
3078
|
+
* This works in both 3D and 2D. As for 2D, the result of slerping
|
|
3079
|
+
* between 2D vectors is always a 2D vector.
|
|
3080
|
+
*
|
|
3081
|
+
*/
|
|
3082
|
+
function slerp(v1: any, v2: any, amt: any, target: any): void;
|
|
3083
|
+
|
|
3084
|
+
/**
|
|
3085
|
+
* @param old vector.
|
|
3086
|
+
* @param new vector.
|
|
3087
|
+
* @param vector to receive the result.
|
|
3088
|
+
* @return slerped vector between v1 and v2
|
|
3089
|
+
*/
|
|
3090
|
+
function slerp(v1: p5.Vector, v2: p5.Vector, amt: number, target?: p5.Vector): p5.Vector;
|
|
3091
|
+
|
|
3092
|
+
/**
|
|
3093
|
+
* Calculates the magnitude (length) of the vector and returns the result as
|
|
3094
|
+
* a float (this is simply the equation `sqrt(x*x + y*y + z*z)`.)
|
|
3095
|
+
*
|
|
3096
|
+
*/
|
|
3097
|
+
function mag(vecT: any): void;
|
|
3098
|
+
|
|
3099
|
+
/**
|
|
3100
|
+
* @param The vector to return the magnitude of
|
|
3101
|
+
* @return The magnitude of vecT
|
|
3102
|
+
*/
|
|
3103
|
+
function mag(vecT: p5.Vector): number;
|
|
3104
|
+
|
|
3105
|
+
/**
|
|
3106
|
+
* Calculates the squared magnitude of the vector and returns the result
|
|
3107
|
+
* as a float (this is simply the equation (x*x + y*y + z*z).)
|
|
3108
|
+
* Faster if the real length is not required in the
|
|
3109
|
+
* case of comparing vectors, etc.
|
|
3110
|
+
*
|
|
3111
|
+
*/
|
|
3112
|
+
function magSq(vecT: any): void;
|
|
3113
|
+
|
|
3114
|
+
/**
|
|
3115
|
+
* @param the vector to return the squared magnitude of
|
|
3116
|
+
* @return the squared magnitude of vecT
|
|
3117
|
+
*/
|
|
3118
|
+
function magSq(vecT: p5.Vector): number;
|
|
3119
|
+
|
|
3120
|
+
/**
|
|
3121
|
+
* Normalize the vector to length 1 (make it a unit vector).
|
|
3122
|
+
*
|
|
3123
|
+
*/
|
|
3124
|
+
function normalize(v: any, target: any): void;
|
|
3125
|
+
|
|
3126
|
+
/**
|
|
3127
|
+
* @param The vector to normalize
|
|
3128
|
+
* @param The vector to receive the result
|
|
3129
|
+
* @return The vector v, normalized to a length of 1
|
|
3130
|
+
*/
|
|
3131
|
+
function normalize(v: p5.Vector, target?: p5.Vector): p5.Vector;
|
|
3132
|
+
|
|
3133
|
+
/**
|
|
3134
|
+
* Limit the magnitude of the vector to the value used for the max
|
|
3135
|
+
* parameter.
|
|
3136
|
+
*
|
|
3137
|
+
*/
|
|
3138
|
+
function limit(v: any, max: any, target: any): void;
|
|
3139
|
+
|
|
3140
|
+
/**
|
|
3141
|
+
* @param the vector to limit
|
|
3142
|
+
* @param the vector to receive the result (Optional)
|
|
3143
|
+
* @return v with a magnitude limited to max
|
|
3144
|
+
*/
|
|
3145
|
+
function limit(v: p5.Vector, max: number, target?: p5.Vector): p5.Vector;
|
|
3146
|
+
|
|
3147
|
+
/**
|
|
3148
|
+
* Set the magnitude of the vector to the value used for the len
|
|
3149
|
+
* parameter.
|
|
3150
|
+
*
|
|
3151
|
+
*/
|
|
3152
|
+
function setMag(v: any, len: any, target: any): void;
|
|
3153
|
+
|
|
3154
|
+
/**
|
|
3155
|
+
* @param the vector to set the magnitude of
|
|
3156
|
+
* @param the vector to receive the result (Optional)
|
|
3157
|
+
* @return v with a magnitude set to len
|
|
3158
|
+
*/
|
|
3159
|
+
function setMag(v: p5.Vector, len: number, target?: p5.Vector): p5.Vector;
|
|
3160
|
+
|
|
3161
|
+
/**
|
|
3162
|
+
* Calculate the angle of rotation for this vector (only 2D vectors).
|
|
3163
|
+
* p5.Vectors created using createVector()
|
|
3164
|
+
* will take the current angleMode into
|
|
3165
|
+
* consideration, and give the angle in radians or degrees accordingly.
|
|
3166
|
+
*
|
|
3167
|
+
*/
|
|
3168
|
+
function heading(v: any): void;
|
|
3169
|
+
|
|
3170
|
+
/**
|
|
3171
|
+
* @param the vector to find the angle of
|
|
3172
|
+
* @return the angle of rotation
|
|
3173
|
+
*/
|
|
3174
|
+
function heading(v: p5.Vector): number;
|
|
3175
|
+
|
|
3176
|
+
/**
|
|
3177
|
+
* Calculates and returns the angle between two vectors. This function will take
|
|
3178
|
+
* the angleMode on v1 into consideration, and
|
|
3179
|
+
* give the angle in radians or degrees accordingly.
|
|
3180
|
+
*
|
|
3181
|
+
*/
|
|
3182
|
+
function angleBetween(v1: any, v2: any): void;
|
|
3183
|
+
|
|
3184
|
+
/**
|
|
3185
|
+
* @param the first vector.
|
|
3186
|
+
* @param the second vector.
|
|
3187
|
+
* @return angle between the two vectors.
|
|
3188
|
+
*/
|
|
3189
|
+
function angleBetween(v1: p5.Vector, v2: p5.Vector): number;
|
|
3190
|
+
|
|
3191
|
+
/**
|
|
3192
|
+
* Reflect a vector about a normal to a line in 2D, or about a normal to a
|
|
3193
|
+
* plane in 3D.
|
|
3194
|
+
*
|
|
3195
|
+
*/
|
|
3196
|
+
function reflect(incidentVector: any, surfaceNormal: any, target: any): void;
|
|
3197
|
+
|
|
3198
|
+
/**
|
|
3199
|
+
* @param vector to be reflected.
|
|
3200
|
+
* @param vector to receive the result.
|
|
3201
|
+
* @return the reflected vector
|
|
3202
|
+
*/
|
|
3203
|
+
function reflect(incidentVector: p5.Vector, surfaceNormal: p5.Vector, target?: p5.Vector): p5.Vector;
|
|
3204
|
+
|
|
3205
|
+
/**
|
|
3206
|
+
* Return a representation of this vector as a float array. This is only
|
|
3207
|
+
* for temporary use. If used in any other fashion, the contents should be
|
|
3208
|
+
* copied by using the p5.Vector.copy()
|
|
3209
|
+
* method to copy into your own vector.
|
|
3210
|
+
*
|
|
3211
|
+
*/
|
|
3212
|
+
function array(v: any): void;
|
|
3213
|
+
|
|
3214
|
+
/**
|
|
3215
|
+
* @param the vector to convert to an array
|
|
3216
|
+
* @return an Array with the 3 values
|
|
3217
|
+
*/
|
|
3218
|
+
function array(v: p5.Vector): number[];
|
|
3219
|
+
|
|
3220
|
+
/**
|
|
3221
|
+
* Equality check against a p5.Vector
|
|
3222
|
+
*
|
|
3223
|
+
*/
|
|
3224
|
+
function equals(v1: any, v2: any): void;
|
|
3225
|
+
|
|
3226
|
+
/**
|
|
3227
|
+
* @param the first vector to compare
|
|
3228
|
+
* @param the second vector to compare
|
|
3229
|
+
*/
|
|
3230
|
+
function equals(v1: p5.Vector | Array, v2: p5.Vector | Array): boolean;
|
|
3231
|
+
|
|
3232
|
+
/**
|
|
3233
|
+
* Multiplies a vector's `x`, `y`, and `z` components.`mult()` can use separate numbers, as in `v.mult(1, 2, 3)`, another
|
|
3234
|
+
* p5.Vector object, as in `v.mult(v2)`, or an array
|
|
3235
|
+
* of numbers, as in `v.mult([1, 2, 3])`.If only one value is provided, as in `v.mult(2)`, then all the components
|
|
3236
|
+
* will be multiplied by 2. If a value isn't provided for a component, it
|
|
3237
|
+
* won't change. For example, `v.mult(4, 5)` multiplies `v.x` by, `v.y` by 5,
|
|
3238
|
+
* and `v.z` by 1. Calling `mult()` with no arguments, as in `v.mult()`, has
|
|
3239
|
+
* no effect.The static version of `mult()`, as in `p5.Vector.mult(v, 2)`, returns a new
|
|
3240
|
+
* p5.Vector object and doesn't change the
|
|
3241
|
+
* originals.
|
|
3242
|
+
*
|
|
3243
|
+
* @param The number to multiply with the vector
|
|
3244
|
+
* @example <div>
|
|
3245
|
+
* <code>
|
|
3246
|
+
* function setup() {
|
|
3247
|
+
* createCanvas(100, 100);
|
|
3248
|
+
*
|
|
3249
|
+
* background(200);
|
|
3250
|
+
*
|
|
3251
|
+
* // Style the points.
|
|
3252
|
+
* strokeWeight(5);
|
|
3253
|
+
*
|
|
3254
|
+
* // Top-left.
|
|
3255
|
+
* let p = createVector(25, 25);
|
|
3256
|
+
* point(p);
|
|
3257
|
+
*
|
|
3258
|
+
* // Center.
|
|
3259
|
+
* // Multiply all components by 2.
|
|
3260
|
+
* p.mult(2);
|
|
3261
|
+
* point(p);
|
|
3262
|
+
*
|
|
3263
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.');
|
|
3264
|
+
* }
|
|
3265
|
+
* </code>
|
|
3266
|
+
* </div>
|
|
3267
|
+
*
|
|
3268
|
+
* <div>
|
|
3269
|
+
* <code>
|
|
3270
|
+
* function setup() {
|
|
3271
|
+
* strokeWeight(5);
|
|
3272
|
+
*
|
|
3273
|
+
* // Top-left.
|
|
3274
|
+
* let p = createVector(25, 25);
|
|
3275
|
+
* point(p);
|
|
3276
|
+
*
|
|
3277
|
+
* // Bottom-right.
|
|
3278
|
+
* // Multiply p.x * 2 and p.y * 3
|
|
3279
|
+
* p.mult(2, 3);
|
|
3280
|
+
* point(p);
|
|
3281
|
+
*
|
|
3282
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
3283
|
+
* }
|
|
3284
|
+
* </code>
|
|
3285
|
+
* </div>
|
|
3286
|
+
*
|
|
3287
|
+
* <div>
|
|
3288
|
+
* <code>
|
|
3289
|
+
* function setup() {
|
|
3290
|
+
* createCanvas(100, 100);
|
|
3291
|
+
*
|
|
3292
|
+
* background(200);
|
|
3293
|
+
*
|
|
3294
|
+
* // Style the points.
|
|
3295
|
+
* strokeWeight(5);
|
|
3296
|
+
*
|
|
3297
|
+
* // Top-left.
|
|
3298
|
+
* let p = createVector(25, 25);
|
|
3299
|
+
* point(p);
|
|
3300
|
+
*
|
|
3301
|
+
* // Bottom-right.
|
|
3302
|
+
* // Multiply p.x * 2 and p.y * 3
|
|
3303
|
+
* let arr = [2, 3];
|
|
3304
|
+
* p.mult(arr);
|
|
3305
|
+
* point(p);
|
|
3306
|
+
*
|
|
3307
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
3308
|
+
* }
|
|
3309
|
+
* </code>
|
|
3310
|
+
* </div>
|
|
3311
|
+
*
|
|
3312
|
+
* <div>
|
|
3313
|
+
* <code>
|
|
3314
|
+
* function setup() {
|
|
3315
|
+
* createCanvas(100, 100);
|
|
3316
|
+
*
|
|
3317
|
+
* background(200);
|
|
3318
|
+
*
|
|
3319
|
+
* // Style the points.
|
|
3320
|
+
* strokeWeight(5);
|
|
3321
|
+
*
|
|
3322
|
+
* // Top-left.
|
|
3323
|
+
* let p = createVector(25, 25);
|
|
3324
|
+
* point(p);
|
|
3325
|
+
*
|
|
3326
|
+
* // Bottom-right.
|
|
3327
|
+
* // Multiply p.x * p2.x and p.y * p2.y
|
|
3328
|
+
* let p2 = createVector(2, 3);
|
|
3329
|
+
* p.mult(p2);
|
|
3330
|
+
* point(p);
|
|
3331
|
+
*
|
|
3332
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
3333
|
+
* }
|
|
3334
|
+
* </code>
|
|
3335
|
+
* </div>
|
|
3336
|
+
*
|
|
3337
|
+
* <div>
|
|
3338
|
+
* <code>
|
|
3339
|
+
* function setup() {
|
|
3340
|
+
* createCanvas(100, 100);
|
|
3341
|
+
*
|
|
3342
|
+
* background(200);
|
|
3343
|
+
*
|
|
3344
|
+
* // Style the points.
|
|
3345
|
+
* strokeWeight(5);
|
|
3346
|
+
*
|
|
3347
|
+
* // Top-left.
|
|
3348
|
+
* let p = createVector(25, 25);
|
|
3349
|
+
* point(p);
|
|
3350
|
+
*
|
|
3351
|
+
* // Bottom-right.
|
|
3352
|
+
* // Create a new p5.Vector with
|
|
3353
|
+
* // p3.x = p.x * p2.x
|
|
3354
|
+
* // p3.y = p.y * p2.y
|
|
3355
|
+
* let p2 = createVector(2, 3);
|
|
3356
|
+
* let p3 = p5.Vector.mult(p, p2);
|
|
3357
|
+
* point(p3);
|
|
3358
|
+
*
|
|
3359
|
+
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
|
|
3360
|
+
* }
|
|
3361
|
+
* </code>
|
|
3362
|
+
* </div>
|
|
3363
|
+
*
|
|
3364
|
+
* <div>
|
|
3365
|
+
* <code>
|
|
3366
|
+
* function setup() {
|
|
3367
|
+
* createCanvas(100, 100);
|
|
3368
|
+
*
|
|
3369
|
+
* describe('Two arrows extending from the top left corner. The blue arrow is twice the length of the red arrow.');
|
|
3370
|
+
* }
|
|
3371
|
+
*
|
|
3372
|
+
* function draw() {
|
|
3373
|
+
* background(200);
|
|
3374
|
+
*
|
|
3375
|
+
* let origin = createVector(0, 0);
|
|
3376
|
+
*
|
|
3377
|
+
* // Draw the red arrow.
|
|
3378
|
+
* let v1 = createVector(25, 25);
|
|
3379
|
+
* drawArrow(origin, v1, 'red');
|
|
3380
|
+
*
|
|
3381
|
+
* // Draw the blue arrow.
|
|
3382
|
+
* let v2 = p5.Vector.mult(v1, 2);
|
|
3383
|
+
* drawArrow(origin, v2, 'blue');
|
|
3384
|
+
* }
|
|
3385
|
+
*
|
|
3386
|
+
* // Draws an arrow between two vectors.
|
|
3387
|
+
* function drawArrow(base, vec, myColor) {
|
|
3388
|
+
* push();
|
|
3389
|
+
* stroke(myColor);
|
|
3390
|
+
* strokeWeight(3);
|
|
3391
|
+
* fill(myColor);
|
|
3392
|
+
* translate(base.x, base.y);
|
|
3393
|
+
* line(0, 0, vec.x, vec.y);
|
|
3394
|
+
* rotate(vec.heading());
|
|
3395
|
+
* let arrowSize = 7;
|
|
3396
|
+
* translate(vec.mag() - arrowSize, 0);
|
|
3397
|
+
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
|
|
3398
|
+
* pop();
|
|
3399
|
+
* }
|
|
3400
|
+
* </code>
|
|
3401
|
+
* </div>
|
|
3402
|
+
*/
|
|
3403
|
+
function mult(n: number): void;
|
|
3404
|
+
|
|
3405
|
+
/**
|
|
3406
|
+
* Replaces the components of a p5.Vector that are very close to zero with zero.In computers, handling numbers with decimals can give slightly imprecise answers due to the way those numbers are represented.
|
|
3407
|
+
* This can make it hard to check if a number is zero, as it may be close but not exactly zero.
|
|
3408
|
+
* This method rounds very close numbers to zero to make those checks easier
|
|
3409
|
+
*
|
|
3410
|
+
* @return with components very close to zero replaced with zero.
|
|
3411
|
+
*/
|
|
3412
|
+
function clampToZero(): p5.Vector;
|
|
3413
|
+
|
|
3414
|
+
}
|
|
3415
|
+
|
|
3416
|
+
export default function p5_Vector(p5: any, fn: any): void;
|