propane 3.1.0.pre-java → 3.2.0-java

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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  3. data/CHANGELOG.md +1 -5
  4. data/README.md +23 -12
  5. data/Rakefile +23 -12
  6. data/lib/propane/helpers/version_error.rb +6 -0
  7. data/lib/propane/runner.rb +12 -0
  8. data/lib/propane/version.rb +1 -1
  9. data/library/slider/slider.rb +1 -1
  10. data/mvnw +234 -0
  11. data/mvnw.cmd +145 -0
  12. data/pom.xml +28 -27
  13. data/propane.gemspec +2 -2
  14. data/src/main/java/japplemenubar/JAppleMenuBar.java +41 -47
  15. data/src/main/java/monkstone/ColorUtil.java +1 -1
  16. data/src/main/java/monkstone/MathToolModule.java +12 -11
  17. data/src/main/java/monkstone/PropaneLibrary.java +9 -10
  18. data/src/main/java/monkstone/core/LibraryProxy.java +124 -113
  19. data/src/main/java/monkstone/fastmath/Deglut.java +86 -89
  20. data/src/main/java/monkstone/filechooser/Chooser.java +7 -13
  21. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -1
  22. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +4 -4
  23. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  24. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +9 -9
  25. data/src/main/java/monkstone/slider/SimpleSlider.java +0 -9
  26. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +11 -13
  27. data/src/main/java/monkstone/slider/Slider.java +1 -1
  28. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  29. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  30. data/src/main/java/monkstone/slider/WheelHandler.java +8 -9
  31. data/src/main/java/monkstone/vecmath/AppRender.java +2 -2
  32. data/src/main/java/monkstone/vecmath/ShapeRender.java +2 -2
  33. data/src/main/java/monkstone/vecmath/package-info.java +2 -2
  34. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +2 -2
  35. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
  36. data/src/main/java/monkstone/videoevent/VideoInterface.java +11 -5
  37. data/src/main/java/monkstone/videoevent/package-info.java +2 -2
  38. data/src/main/java/processing/awt/PGraphicsJava2D.java +1742 -2243
  39. data/src/main/java/processing/awt/PShapeJava2D.java +268 -270
  40. data/src/main/java/processing/awt/PSurfaceAWT.java +821 -920
  41. data/src/main/java/processing/core/DesktopHandler.java +94 -0
  42. data/src/main/java/processing/core/PApplet.java +14170 -14082
  43. data/src/main/java/processing/core/PConstants.java +447 -473
  44. data/src/main/java/processing/core/PFont.java +867 -873
  45. data/src/main/java/processing/core/PGraphics.java +7193 -7428
  46. data/src/main/java/processing/core/PImage.java +3051 -3117
  47. data/src/main/java/processing/core/PMatrix.java +159 -172
  48. data/src/main/java/processing/core/PMatrix2D.java +403 -444
  49. data/src/main/java/processing/core/PMatrix3D.java +735 -749
  50. data/src/main/java/processing/core/PShape.java +2651 -2793
  51. data/src/main/java/processing/core/PShapeOBJ.java +415 -422
  52. data/src/main/java/processing/core/PShapeSVG.java +1466 -1475
  53. data/src/main/java/processing/core/PStyle.java +37 -40
  54. data/src/main/java/processing/core/PSurface.java +98 -103
  55. data/src/main/java/processing/core/PSurfaceNone.java +208 -236
  56. data/src/main/java/processing/core/PVector.java +961 -990
  57. data/src/main/java/processing/data/DoubleDict.java +709 -753
  58. data/src/main/java/processing/data/DoubleList.java +695 -748
  59. data/src/main/java/processing/data/FloatDict.java +702 -746
  60. data/src/main/java/processing/data/FloatList.java +697 -751
  61. data/src/main/java/processing/data/IntDict.java +673 -718
  62. data/src/main/java/processing/data/IntList.java +633 -699
  63. data/src/main/java/processing/data/JSONArray.java +873 -931
  64. data/src/main/java/processing/data/JSONObject.java +1165 -1262
  65. data/src/main/java/processing/data/JSONTokener.java +341 -351
  66. data/src/main/java/processing/data/LongDict.java +662 -707
  67. data/src/main/java/processing/data/LongList.java +634 -700
  68. data/src/main/java/processing/data/Sort.java +41 -37
  69. data/src/main/java/processing/data/StringDict.java +486 -522
  70. data/src/main/java/processing/data/StringList.java +580 -624
  71. data/src/main/java/processing/data/Table.java +3508 -3686
  72. data/src/main/java/processing/data/TableRow.java +183 -182
  73. data/src/main/java/processing/data/XML.java +883 -957
  74. data/src/main/java/processing/event/Event.java +66 -87
  75. data/src/main/java/processing/event/KeyEvent.java +41 -48
  76. data/src/main/java/processing/event/MouseEvent.java +93 -103
  77. data/src/main/java/processing/event/TouchEvent.java +6 -10
  78. data/src/main/java/processing/javafx/PGraphicsFX2D.java +5 -69
  79. data/src/main/java/processing/javafx/PSurfaceFX.java +2 -7
  80. data/src/main/java/processing/opengl/FontTexture.java +270 -290
  81. data/src/main/java/processing/opengl/FrameBuffer.java +363 -375
  82. data/src/main/java/processing/opengl/LinePath.java +500 -543
  83. data/src/main/java/processing/opengl/LineStroker.java +582 -593
  84. data/src/main/java/processing/opengl/PGL.java +2881 -2904
  85. data/src/main/java/processing/opengl/PGraphics2D.java +315 -408
  86. data/src/main/java/processing/opengl/PGraphics3D.java +72 -107
  87. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12043 -12230
  88. data/src/main/java/processing/opengl/PJOGL.java +1681 -1745
  89. data/src/main/java/processing/opengl/PShader.java +1257 -1260
  90. data/src/main/java/processing/opengl/PShapeOpenGL.java +4599 -4662
  91. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1030 -1047
  92. data/src/main/java/processing/opengl/Texture.java +1397 -1462
  93. data/src/main/java/processing/opengl/VertexBuffer.java +55 -57
  94. data/src/main/resources/icon/icon-1024.png +0 -0
  95. data/src/main/resources/icon/icon-128.png +0 -0
  96. data/src/main/resources/icon/icon-16.png +0 -0
  97. data/src/main/resources/icon/icon-256.png +0 -0
  98. data/src/main/resources/icon/icon-32.png +0 -0
  99. data/src/main/resources/icon/icon-48.png +0 -0
  100. data/src/main/resources/icon/icon-512.png +0 -0
  101. data/src/main/resources/icon/icon-64.png +0 -0
  102. data/vendors/Rakefile +1 -1
  103. metadata +12 -8
  104. data/src/main/java/processing/core/ThinkDifferent.java +0 -70
@@ -1,5 +1,3 @@
1
- /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
-
3
1
  /*
4
2
  Part of the Processing project - http://processing.org
5
3
 
@@ -21,7 +19,6 @@
21
19
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22
20
  Boston, MA 02111-1307 USA
23
21
  */
24
-
25
22
  package processing.core;
26
23
 
27
24
  import java.io.Serializable;
@@ -32,24 +29,25 @@ import processing.core.PConstants;
32
29
  /**
33
30
  * ( begin auto-generated from PVector.xml )
34
31
  *
35
- * A class to describe a two or three dimensional vector. This datatype
36
- * stores two or three variables that are commonly used as a position,
37
- * velocity, and/or acceleration. Technically, <em>position</em> is a point
38
- * and <em>velocity</em> and <em>acceleration</em> are vectors, but this is
39
- * often simplified to consider all three as vectors. For example, if you
40
- * consider a rectangle moving across the screen, at any given instant it
41
- * has a position (the object's location, expressed as a point.), a
42
- * velocity (the rate at which the object's position changes per time unit,
43
- * expressed as a vector), and acceleration (the rate at which the object's
44
- * velocity changes per time unit, expressed as a vector). Since vectors
45
- * represent groupings of values, we cannot simply use traditional
46
- * addition/multiplication/etc. Instead, we'll need to do some "vector"
47
- * math, which is made easy by the methods inside the <b>PVector</b>
32
+ * A class to describe a two or three dimensional vector. This datatype stores
33
+ * two or three variables that are commonly used as a position, velocity, and/or
34
+ * acceleration. Technically, <em>position</em> is a point and <em>velocity</em>
35
+ * and <em>acceleration</em> are vectors, but this is often simplified to
36
+ * consider all three as vectors. For example, if you consider a rectangle
37
+ * moving across the screen, at any given instant it has a position (the
38
+ * object's location, expressed as a point.), a velocity (the rate at which the
39
+ * object's position changes per time unit, expressed as a vector), and
40
+ * acceleration (the rate at which the object's velocity changes per time unit,
41
+ * expressed as a vector). Since vectors represent groupings of values, we
42
+ * cannot simply use traditional addition/multiplication/etc. Instead, we'll
43
+ * need to do some "vector" math, which is made easy by the methods inside the
44
+ * <b>PVector</b>
48
45
  * class.<br />
49
46
  * <br />
50
47
  * The methods for this class are extensive. For a complete list, visit the
51
48
  * <a
52
- * href="http://processing.googlecode.com/svn/trunk/processing/build/javadoc/core/">developer's reference.</a>
49
+ * href="http://processing.googlecode.com/svn/trunk/processing/build/javadoc/core/">developer's
50
+ * reference.</a>
53
51
  *
54
52
  * ( end auto-generated )
55
53
  *
@@ -57,1007 +55,980 @@ import processing.core.PConstants;
57
55
  * <p>
58
56
  * The result of all functions are applied to the vector itself, with the
59
57
  * exception of cross(), which returns a new PVector (or writes to a specified
60
- * 'target' PVector). That is, add() will add the contents of one vector to
61
- * this one. Using add() with additional parameters allows you to put the
62
- * result into a new PVector. Functions that act on multiple vectors also
63
- * include static versions. Because creating new objects can be computationally
64
- * expensive, most functions include an optional 'target' PVector, so that a
65
- * new PVector object is not created with each operation.
58
+ * 'target' PVector). That is, add() will add the contents of one vector to this
59
+ * one. Using add() with additional parameters allows you to put the result into
60
+ * a new PVector. Functions that act on multiple vectors also include static
61
+ * versions. Because creating new objects can be computationally expensive, most
62
+ * functions include an optional 'target' PVector, so that a new PVector object
63
+ * is not created with each operation.
66
64
  * <p>
67
- * Initially based on the Vector3D class by <a href="http://www.shiffman.net">Dan Shiffman</a>.
65
+ * Initially based on the Vector3D class by
66
+ * <a href="http://www.shiffman.net">Dan Shiffman</a>.
68
67
  *
69
68
  * @webref math
70
69
  */
71
70
  public class PVector implements Serializable {
72
- /**
73
- * ( begin auto-generated from PVector_x.xml )
74
- *
75
- * The x component of the vector. This field (variable) can be used to both
76
- * get and set the value (see above example.)
77
- *
78
- * ( end auto-generated )
79
- *
80
- * @webref pvector:field
81
- * @usage web_application
82
- * @brief The x component of the vector
83
- */
84
- public float x;
85
-
86
- /**
87
- * ( begin auto-generated from PVector_y.xml )
88
- *
89
- * The y component of the vector. This field (variable) can be used to both
90
- * get and set the value (see above example.)
91
- *
92
- * ( end auto-generated )
93
- *
94
- * @webref pvector:field
95
- * @usage web_application
96
- * @brief The y component of the vector
97
- */
98
- public float y;
99
-
100
- /**
101
- * ( begin auto-generated from PVector_z.xml )
102
- *
103
- * The z component of the vector. This field (variable) can be used to both
104
- * get and set the value (see above example.)
105
- *
106
- * ( end auto-generated )
107
- *
108
- * @webref pvector:field
109
- * @usage web_application
110
- * @brief The z component of the vector
111
- */
112
- public float z;
113
-
114
- /** Array so that this can be temporarily used in an array context */
115
- transient protected float[] array;
116
-
117
-
118
- /**
119
- * Constructor for an empty vector: x, y, and z are set to 0.
120
- */
121
- public PVector() {
122
- }
123
-
124
-
125
- /**
126
- * Constructor for a 3D vector.
127
- *
128
- * @param x the x coordinate.
129
- * @param y the y coordinate.
130
- * @param z the z coordinate.
131
- */
132
- public PVector(float x, float y, float z) {
133
- this.x = x;
134
- this.y = y;
135
- this.z = z;
136
- }
137
-
138
-
139
- /**
140
- * Constructor for a 2D vector: z coordinate is set to 0.
141
- */
142
- public PVector(float x, float y) {
143
- this.x = x;
144
- this.y = y;
145
- }
146
-
147
-
148
- /**
149
- * ( begin auto-generated from PVector_set.xml )
150
- *
151
- * Sets the x, y, and z component of the vector using two or three separate
152
- * variables, the data from a PVector, or the values from a float array.
153
- *
154
- * ( end auto-generated )
155
- *
156
- * @webref pvector:method
157
- * @param x the x component of the vector
158
- * @param y the y component of the vector
159
- * @param z the z component of the vector
160
- * @brief Set the components of the vector
161
- */
162
- public PVector set(float x, float y, float z) {
163
- this.x = x;
164
- this.y = y;
165
- this.z = z;
166
- return this;
167
- }
168
-
169
-
170
- /**
171
- * @param x the x component of the vector
172
- * @param y the y component of the vector
173
- */
174
- public PVector set(float x, float y) {
175
- this.x = x;
176
- this.y = y;
177
- this.z = 0;
178
- return this;
179
- }
180
-
181
-
182
- /**
183
- * @param v any variable of type PVector
184
- */
185
- public PVector set(PVector v) {
186
- x = v.x;
187
- y = v.y;
188
- z = v.z;
189
- return this;
190
- }
191
-
192
-
193
- /**
194
- * Set the x, y (and maybe z) coordinates using a float[] array as the source.
195
- * @param source array to copy from
196
- */
197
- public PVector set(float[] source) {
198
- if (source.length >= 2) {
199
- x = source[0];
200
- y = source[1];
71
+
72
+ /**
73
+ * ( begin auto-generated from PVector_x.xml )
74
+ *
75
+ * The x component of the vector. This field (variable) can be used to both
76
+ * get and set the value (see above example.)
77
+ *
78
+ * ( end auto-generated )
79
+ *
80
+ * @webref pvector:field
81
+ * @usage web_application
82
+ * @brief The x component of the vector
83
+ */
84
+ public float x;
85
+
86
+ /**
87
+ * ( begin auto-generated from PVector_y.xml )
88
+ *
89
+ * The y component of the vector. This field (variable) can be used to both
90
+ * get and set the value (see above example.)
91
+ *
92
+ * ( end auto-generated )
93
+ *
94
+ * @webref pvector:field
95
+ * @usage web_application
96
+ * @brief The y component of the vector
97
+ */
98
+ public float y;
99
+
100
+ /**
101
+ * ( begin auto-generated from PVector_z.xml )
102
+ *
103
+ * The z component of the vector. This field (variable) can be used to both
104
+ * get and set the value (see above example.)
105
+ *
106
+ * ( end auto-generated )
107
+ *
108
+ * @webref pvector:field
109
+ * @usage web_application
110
+ * @brief The z component of the vector
111
+ */
112
+ public float z;
113
+
114
+ /**
115
+ * Array so that this can be temporarily used in an array context
116
+ */
117
+ transient protected float[] array;
118
+
119
+ /**
120
+ * Constructor for an empty vector: x, y, and z are set to 0.
121
+ */
122
+ public PVector() {
201
123
  }
202
- if (source.length >= 3) {
203
- z = source[2];
204
- } else {
205
- z = 0;
124
+
125
+ /**
126
+ * Constructor for a 3D vector.
127
+ *
128
+ * @param x the x coordinate.
129
+ * @param y the y coordinate.
130
+ * @param z the z coordinate.
131
+ */
132
+ public PVector(float x, float y, float z) {
133
+ this.x = x;
134
+ this.y = y;
135
+ this.z = z;
206
136
  }
207
- return this;
208
- }
209
-
210
-
211
- /**
212
- * ( begin auto-generated from PVector_random2D.xml )
213
- *
214
- * Make a new 2D unit vector with a random direction. If you pass in "this"
215
- * as an argument, it will use the PApplet's random number generator. You can
216
- * also pass in a target PVector to fill.
217
- *
218
- * @webref pvector:method
219
- * @usage web_application
220
- * @return the random PVector
221
- * @brief Make a new 2D unit vector with a random direction.
222
- * @see PVector#random3D()
223
- */
224
- static public PVector random2D() {
225
- return random2D(null, null);
226
- }
227
-
228
-
229
- /**
230
- * Make a new 2D unit vector with a random direction
231
- * using Processing's current random number generator
232
- * @param parent current PApplet instance
233
- * @return the random PVector
234
- */
235
- static public PVector random2D(PApplet parent) {
236
- return random2D(null, parent);
237
- }
238
-
239
- /**
240
- * Set a 2D vector to a random unit vector with a random direction
241
- * @param target the target vector (if null, a new vector will be created)
242
- * @return the random PVector
243
- */
244
- static public PVector random2D(PVector target) {
245
- return random2D(target, null);
246
- }
247
-
248
-
249
- /**
250
- * Make a new 2D unit vector with a random direction. Pass in the parent
251
- * PApplet if you want randomSeed() to work (and be predictable). Or leave
252
- * it null and be... random.
253
- * @return the random PVector
254
- */
255
- static public PVector random2D(PVector target, PApplet parent) {
256
- return (parent == null) ?
257
- fromAngle((float) (Math.random() * Math.PI*2), target) :
258
- fromAngle(parent.random(PConstants.TAU), target);
259
- }
260
-
261
-
262
- /**
263
- * ( begin auto-generated from PVector_random3D.xml )
264
- *
265
- * Make a new 3D unit vector with a random direction. If you pass in "this"
266
- * as an argument, it will use the PApplet's random number generator. You can
267
- * also pass in a target PVector to fill.
268
- *
269
- * @webref pvector:method
270
- * @usage web_application
271
- * @return the random PVector
272
- * @brief Make a new 3D unit vector with a random direction.
273
- * @see PVector#random2D()
274
- */
275
- static public PVector random3D() {
276
- return random3D(null, null);
277
- }
278
-
279
-
280
- /**
281
- * Make a new 3D unit vector with a random direction
282
- * using Processing's current random number generator
283
- * @param parent current PApplet instance
284
- * @return the random PVector
285
- */
286
- static public PVector random3D(PApplet parent) {
287
- return random3D(null, parent);
288
- }
289
-
290
-
291
- /**
292
- * Set a 3D vector to a random unit vector with a random direction
293
- * @param target the target vector (if null, a new vector will be created)
294
- * @return the random PVector
295
- */
296
- static public PVector random3D(PVector target) {
297
- return random3D(target, null);
298
- }
299
-
300
-
301
- /**
302
- * Make a new 3D unit vector with a random direction
303
- * @return the random PVector
304
- */
305
- static public PVector random3D(PVector target, PApplet parent) {
306
- float angle;
307
- float vz;
308
- if (parent == null) {
309
- angle = (float) (Math.random()*Math.PI*2);
310
- vz = (float) (Math.random()*2-1);
311
- } else {
312
- angle = parent.random(PConstants.TWO_PI);
313
- vz = parent.random(-1,1);
137
+
138
+ /**
139
+ * Constructor for a 2D vector: z coordinate is set to 0.
140
+ */
141
+ public PVector(float x, float y) {
142
+ this.x = x;
143
+ this.y = y;
314
144
  }
315
- float vx = (float) (Math.sqrt(1-vz*vz)*Math.cos(angle));
316
- float vy = (float) (Math.sqrt(1-vz*vz)*Math.sin(angle));
317
- if (target == null) {
318
- target = new PVector(vx, vy, vz);
319
- //target.normalize(); // Should be unnecessary
320
- } else {
321
- target.set(vx,vy,vz);
145
+
146
+ /**
147
+ * ( begin auto-generated from PVector_set.xml )
148
+ *
149
+ * Sets the x, y, and z component of the vector using two or three separate
150
+ * variables, the data from a PVector, or the values from a float array.
151
+ *
152
+ * ( end auto-generated )
153
+ *
154
+ * @webref pvector:method
155
+ * @param x the x component of the vector
156
+ * @param y the y component of the vector
157
+ * @param z the z component of the vector
158
+ * @brief Set the components of the vector
159
+ */
160
+ public PVector set(float x, float y, float z) {
161
+ this.x = x;
162
+ this.y = y;
163
+ this.z = z;
164
+ return this;
322
165
  }
323
- return target;
324
- }
325
-
326
-
327
- /**
328
- * ( begin auto-generated from PVector_sub.xml )
329
- *
330
- * Make a new 2D unit vector from an angle.
331
- *
332
- * ( end auto-generated )
333
- *
334
- * @webref pvector:method
335
- * @usage web_application
336
- * @brief Make a new 2D unit vector from an angle
337
- * @param angle the angle in radians
338
- * @return the new unit PVector
339
- */
340
- static public PVector fromAngle(float angle) {
341
- return fromAngle(angle,null);
342
- }
343
-
344
-
345
- /**
346
- * Make a new 2D unit vector from an angle
347
- *
348
- * @param target the target vector (if null, a new vector will be created)
349
- * @return the PVector
350
- */
351
- static public PVector fromAngle(float angle, PVector target) {
352
- if (target == null) {
353
- target = new PVector((float)Math.cos(angle),(float)Math.sin(angle),0);
354
- } else {
355
- target.set((float)Math.cos(angle),(float)Math.sin(angle),0);
166
+
167
+ /**
168
+ * @param x the x component of the vector
169
+ * @param y the y component of the vector
170
+ */
171
+ public PVector set(float x, float y) {
172
+ this.x = x;
173
+ this.y = y;
174
+ this.z = 0;
175
+ return this;
356
176
  }
357
- return target;
358
- }
359
-
360
-
361
- /**
362
- * ( begin auto-generated from PVector_copy.xml )
363
- *
364
- * Gets a copy of the vector, returns a PVector object.
365
- *
366
- * ( end auto-generated )
367
- *
368
- * @webref pvector:method
369
- * @usage web_application
370
- * @brief Get a copy of the vector
371
- */
372
- public PVector copy() {
373
- return new PVector(x, y, z);
374
- }
375
-
376
-
377
- @Deprecated
378
- public PVector get() {
379
- return copy();
380
- }
381
-
382
-
383
- /**
384
- * @param target
385
- */
386
- public float[] get(float[] target) {
387
- if (target == null) {
388
- return new float[] { x, y, z };
177
+
178
+ /**
179
+ * @param v any variable of type PVector
180
+ */
181
+ public PVector set(PVector v) {
182
+ x = v.x;
183
+ y = v.y;
184
+ z = v.z;
185
+ return this;
186
+ }
187
+
188
+ /**
189
+ * Set the x, y (and maybe z) coordinates using a float[] array as the
190
+ * source.
191
+ *
192
+ * @param source array to copy from
193
+ */
194
+ public PVector set(float[] source) {
195
+ if (source.length >= 2) {
196
+ x = source[0];
197
+ y = source[1];
198
+ }
199
+ if (source.length >= 3) {
200
+ z = source[2];
201
+ } else {
202
+ z = 0;
203
+ }
204
+ return this;
389
205
  }
390
- if (target.length >= 2) {
391
- target[0] = x;
392
- target[1] = y;
206
+
207
+ /**
208
+ * ( begin auto-generated from PVector_random2D.xml )
209
+ *
210
+ * Make a new 2D unit vector with a random direction. If you pass in "this"
211
+ * as an argument, it will use the PApplet's random number generator. You
212
+ * can also pass in a target PVector to fill.
213
+ *
214
+ * @webref pvector:method
215
+ * @usage web_application
216
+ * @return the random PVector
217
+ * @brief Make a new 2D unit vector with a random direction.
218
+ * @see PVector#random3D()
219
+ */
220
+ static public PVector random2D() {
221
+ return random2D(null, null);
222
+ }
223
+
224
+ /**
225
+ * Make a new 2D unit vector with a random direction using Processing's
226
+ * current random number generator
227
+ *
228
+ * @param parent current PApplet instance
229
+ * @return the random PVector
230
+ */
231
+ static public PVector random2D(PApplet parent) {
232
+ return random2D(null, parent);
393
233
  }
394
- if (target.length >= 3) {
395
- target[2] = z;
234
+
235
+ /**
236
+ * Set a 2D vector to a random unit vector with a random direction
237
+ *
238
+ * @param target the target vector (if null, a new vector will be created)
239
+ * @return the random PVector
240
+ */
241
+ static public PVector random2D(PVector target) {
242
+ return random2D(target, null);
396
243
  }
397
- return target;
398
- }
399
-
400
-
401
- /**
402
- * ( begin auto-generated from PVector_mag.xml )
403
- *
404
- * Calculates the magnitude (length) of the vector and returns the result
405
- * as a float (this is simply the equation <em>sqrt(x*x + y*y + z*z)</em>.)
406
- *
407
- * ( end auto-generated )
408
- *
409
- * @webref pvector:method
410
- * @usage web_application
411
- * @brief Calculate the magnitude of the vector
412
- * @return magnitude (length) of the vector
413
- * @see PVector#magSq()
414
- */
415
- public float mag() {
416
- return (float) Math.sqrt(x*x + y*y + z*z);
417
- }
418
-
419
-
420
- /**
421
- * ( begin auto-generated from PVector_mag.xml )
422
- *
423
- * Calculates the squared magnitude of the vector and returns the result
424
- * as a float (this is simply the equation <em>(x*x + y*y + z*z)</em>.)
425
- * Faster if the real length is not required in the
426
- * case of comparing vectors, etc.
427
- *
428
- * ( end auto-generated )
429
- *
430
- * @webref pvector:method
431
- * @usage web_application
432
- * @brief Calculate the magnitude of the vector, squared
433
- * @return squared magnitude of the vector
434
- * @see PVector#mag()
435
- */
436
- public float magSq() {
437
- return (x*x + y*y + z*z);
438
- }
439
-
440
-
441
- /**
442
- * ( begin auto-generated from PVector_add.xml )
443
- *
444
- * Adds x, y, and z components to a vector, adds one vector to another, or
445
- * adds two independent vectors together. The version of the method that
446
- * adds two vectors together is a static method and returns a PVector, the
447
- * others have no return value -- they act directly on the vector. See the
448
- * examples for more context.
449
- *
450
- * ( end auto-generated )
451
- *
452
- * @webref pvector:method
453
- * @usage web_application
454
- * @param v the vector to be added
455
- * @brief Adds x, y, and z components to a vector, one vector to another, or two independent vectors
456
- */
457
- public PVector add(PVector v) {
458
- x += v.x;
459
- y += v.y;
460
- z += v.z;
461
- return this;
462
- }
463
-
464
-
465
- /**
466
- * @param x x component of the vector
467
- * @param y y component of the vector
468
- */
469
- public PVector add(float x, float y) {
470
- this.x += x;
471
- this.y += y;
472
- return this;
473
- }
474
-
475
-
476
- /**
477
- * @param z z component of the vector
478
- */
479
- public PVector add(float x, float y, float z) {
480
- this.x += x;
481
- this.y += y;
482
- this.z += z;
483
- return this;
484
- }
485
-
486
-
487
- /**
488
- * Add two vectors
489
- * @param v1 a vector
490
- * @param v2 another vector
491
- */
492
- static public PVector add(PVector v1, PVector v2) {
493
- return add(v1, v2, null);
494
- }
495
-
496
-
497
- /**
498
- * Add two vectors into a target vector
499
- * @param target the target vector (if null, a new vector will be created)
500
- */
501
- static public PVector add(PVector v1, PVector v2, PVector target) {
502
- if (target == null) {
503
- target = new PVector(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
504
- } else {
505
- target.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
244
+
245
+ /**
246
+ * Make a new 2D unit vector with a random direction. Pass in the parent
247
+ * PApplet if you want randomSeed() to work (and be predictable). Or leave
248
+ * it null and be... random.
249
+ *
250
+ * @return the random PVector
251
+ */
252
+ static public PVector random2D(PVector target, PApplet parent) {
253
+ return (parent == null)
254
+ ? fromAngle((float) (Math.random() * Math.PI * 2), target)
255
+ : fromAngle(parent.random(PConstants.TAU), target);
506
256
  }
507
- return target;
508
- }
509
-
510
-
511
- /**
512
- * ( begin auto-generated from PVector_sub.xml )
513
- *
514
- * Subtracts x, y, and z components from a vector, subtracts one vector
515
- * from another, or subtracts two independent vectors. The version of the
516
- * method that subtracts two vectors is a static method and returns a
517
- * PVector, the others have no return value -- they act directly on the
518
- * vector. See the examples for more context.
519
- *
520
- * ( end auto-generated )
521
- *
522
- * @webref pvector:method
523
- * @usage web_application
524
- * @param v any variable of type PVector
525
- * @brief Subtract x, y, and z components from a vector, one vector from another, or two independent vectors
526
- */
527
- public PVector sub(PVector v) {
528
- x -= v.x;
529
- y -= v.y;
530
- z -= v.z;
531
- return this;
532
- }
533
-
534
-
535
- /**
536
- * @param x the x component of the vector
537
- * @param y the y component of the vector
538
- */
539
- public PVector sub(float x, float y) {
540
- this.x -= x;
541
- this.y -= y;
542
- return this;
543
- }
544
-
545
-
546
- /**
547
- * @param z the z component of the vector
548
- */
549
- public PVector sub(float x, float y, float z) {
550
- this.x -= x;
551
- this.y -= y;
552
- this.z -= z;
553
- return this;
554
- }
555
-
556
-
557
- /**
558
- * Subtract one vector from another
559
- * @param v1 the x, y, and z components of a PVector object
560
- * @param v2 the x, y, and z components of a PVector object
561
- */
562
- static public PVector sub(PVector v1, PVector v2) {
563
- return sub(v1, v2, null);
564
- }
565
-
566
-
567
- /**
568
- * Subtract one vector from another and store in another vector
569
- * @param target PVector in which to store the result
570
- */
571
- static public PVector sub(PVector v1, PVector v2, PVector target) {
572
- if (target == null) {
573
- target = new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
574
- } else {
575
- target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
257
+
258
+ /**
259
+ * ( begin auto-generated from PVector_random3D.xml )
260
+ *
261
+ * Make a new 3D unit vector with a random direction. If you pass in "this"
262
+ * as an argument, it will use the PApplet's random number generator. You
263
+ * can also pass in a target PVector to fill.
264
+ *
265
+ * @webref pvector:method
266
+ * @usage web_application
267
+ * @return the random PVector
268
+ * @brief Make a new 3D unit vector with a random direction.
269
+ * @see PVector#random2D()
270
+ */
271
+ static public PVector random3D() {
272
+ return random3D(null, null);
576
273
  }
577
- return target;
578
- }
579
-
580
-
581
- /**
582
- * ( begin auto-generated from PVector_mult.xml )
583
- *
584
- * Multiplies a vector by a scalar or multiplies one vector by another.
585
- *
586
- * ( end auto-generated )
587
- *
588
- * @webref pvector:method
589
- * @usage web_application
590
- * @brief Multiply a vector by a scalar
591
- * @param n the number to multiply with the vector
592
- */
593
- public PVector mult(float n) {
594
- x *= n;
595
- y *= n;
596
- z *= n;
597
- return this;
598
- }
599
-
600
-
601
- /**
602
- * @param v the vector to multiply by the scalar
603
- */
604
- static public PVector mult(PVector v, float n) {
605
- return mult(v, n, null);
606
- }
607
-
608
-
609
- /**
610
- * Multiply a vector by a scalar, and write the result into a target PVector.
611
- * @param target PVector in which to store the result
612
- */
613
- static public PVector mult(PVector v, float n, PVector target) {
614
- if (target == null) {
615
- target = new PVector(v.x*n, v.y*n, v.z*n);
616
- } else {
617
- target.set(v.x*n, v.y*n, v.z*n);
274
+
275
+ /**
276
+ * Make a new 3D unit vector with a random direction using Processing's
277
+ * current random number generator
278
+ *
279
+ * @param parent current PApplet instance
280
+ * @return the random PVector
281
+ */
282
+ static public PVector random3D(PApplet parent) {
283
+ return random3D(null, parent);
618
284
  }
619
- return target;
620
- }
621
-
622
-
623
- /**
624
- * ( begin auto-generated from PVector_div.xml )
625
- *
626
- * Divides a vector by a scalar or divides one vector by another.
627
- *
628
- * ( end auto-generated )
629
- *
630
- * @webref pvector:method
631
- * @usage web_application
632
- * @brief Divide a vector by a scalar
633
- * @param n the number by which to divide the vector
634
- */
635
- public PVector div(float n) {
636
- x /= n;
637
- y /= n;
638
- z /= n;
639
- return this;
640
- }
641
-
642
-
643
- /**
644
- * Divide a vector by a scalar and return the result in a new vector.
645
- * @param v the vector to divide by the scalar
646
- * @return a new vector that is v1 / n
647
- */
648
- static public PVector div(PVector v, float n) {
649
- return div(v, n, null);
650
- }
651
-
652
-
653
- /**
654
- * Divide a vector by a scalar and store the result in another vector.
655
- * @param target PVector in which to store the result
656
- */
657
- static public PVector div(PVector v, float n, PVector target) {
658
- if (target == null) {
659
- target = new PVector(v.x/n, v.y/n, v.z/n);
660
- } else {
661
- target.set(v.x/n, v.y/n, v.z/n);
285
+
286
+ /**
287
+ * Set a 3D vector to a random unit vector with a random direction
288
+ *
289
+ * @param target the target vector (if null, a new vector will be created)
290
+ * @return the random PVector
291
+ */
292
+ static public PVector random3D(PVector target) {
293
+ return random3D(target, null);
662
294
  }
663
- return target;
664
- }
665
-
666
-
667
- /**
668
- * ( begin auto-generated from PVector_dist.xml )
669
- *
670
- * Calculates the Euclidean distance between two points (considering a
671
- * point as a vector object).
672
- *
673
- * ( end auto-generated )
674
- *
675
- * @webref pvector:method
676
- * @usage web_application
677
- * @param v the x, y, and z coordinates of a PVector
678
- * @brief Calculate the distance between two points
679
- */
680
- public float dist(PVector v) {
681
- float dx = x - v.x;
682
- float dy = y - v.y;
683
- float dz = z - v.z;
684
- return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
685
- }
686
-
687
-
688
- /**
689
- * @param v1 any variable of type PVector
690
- * @param v2 any variable of type PVector
691
- * @return the Euclidean distance between v1 and v2
692
- */
693
- static public float dist(PVector v1, PVector v2) {
694
- float dx = v1.x - v2.x;
695
- float dy = v1.y - v2.y;
696
- float dz = v1.z - v2.z;
697
- return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
698
- }
699
-
700
-
701
- /**
702
- * ( begin auto-generated from PVector_dot.xml )
703
- *
704
- * Calculates the dot product of two vectors.
705
- *
706
- * ( end auto-generated )
707
- *
708
- * @webref pvector:method
709
- * @usage web_application
710
- * @param v any variable of type PVector
711
- * @return the dot product
712
- * @brief Calculate the dot product of two vectors
713
- */
714
- public float dot(PVector v) {
715
- return x*v.x + y*v.y + z*v.z;
716
- }
717
-
718
-
719
- /**
720
- * @param x x component of the vector
721
- * @param y y component of the vector
722
- * @param z z component of the vector
723
- */
724
- public float dot(float x, float y, float z) {
725
- return this.x*x + this.y*y + this.z*z;
726
- }
727
-
728
-
729
- /**
730
- * @param v1 any variable of type PVector
731
- * @param v2 any variable of type PVector
732
- */
733
- static public float dot(PVector v1, PVector v2) {
734
- return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
735
- }
736
-
737
-
738
- /**
739
- * ( begin auto-generated from PVector_cross.xml )
740
- *
741
- * Calculates and returns a vector composed of the cross product between
742
- * two vectors.
743
- *
744
- * ( end auto-generated )
745
- *
746
- * @webref pvector:method
747
- * @param v the vector to calculate the cross product
748
- * @brief Calculate and return the cross product
749
- */
750
- public PVector cross(PVector v) {
751
- return cross(v, null);
752
- }
753
-
754
-
755
- /**
756
- * @param v any variable of type PVector
757
- * @param target PVector to store the result
758
- */
759
- public PVector cross(PVector v, PVector target) {
760
- float crossX = y * v.z - v.y * z;
761
- float crossY = z * v.x - v.z * x;
762
- float crossZ = x * v.y - v.x * y;
763
-
764
- if (target == null) {
765
- target = new PVector(crossX, crossY, crossZ);
766
- } else {
767
- target.set(crossX, crossY, crossZ);
295
+
296
+ /**
297
+ * Make a new 3D unit vector with a random direction
298
+ *
299
+ * @return the random PVector
300
+ */
301
+ static public PVector random3D(PVector target, PApplet parent) {
302
+ float angle;
303
+ float vz;
304
+ if (parent == null) {
305
+ angle = (float) (Math.random() * Math.PI * 2);
306
+ vz = (float) (Math.random() * 2 - 1);
307
+ } else {
308
+ angle = parent.random(PConstants.TWO_PI);
309
+ vz = parent.random(-1, 1);
310
+ }
311
+ float vx = (float) (Math.sqrt(1 - vz * vz) * Math.cos(angle));
312
+ float vy = (float) (Math.sqrt(1 - vz * vz) * Math.sin(angle));
313
+ if (target == null) {
314
+ target = new PVector(vx, vy, vz);
315
+ //target.normalize(); // Should be unnecessary
316
+ } else {
317
+ target.set(vx, vy, vz);
318
+ }
319
+ return target;
768
320
  }
769
- return target;
770
- }
771
-
772
-
773
- /**
774
- * @param v1 any variable of type PVector
775
- * @param v2 any variable of type PVector
776
- * @param target PVector to store the result
777
- */
778
- static public PVector cross(PVector v1, PVector v2, PVector target) {
779
- float crossX = v1.y * v2.z - v2.y * v1.z;
780
- float crossY = v1.z * v2.x - v2.z * v1.x;
781
- float crossZ = v1.x * v2.y - v2.x * v1.y;
782
-
783
- if (target == null) {
784
- target = new PVector(crossX, crossY, crossZ);
785
- } else {
786
- target.set(crossX, crossY, crossZ);
321
+
322
+ /**
323
+ * ( begin auto-generated from PVector_sub.xml )
324
+ *
325
+ * Make a new 2D unit vector from an angle.
326
+ *
327
+ * ( end auto-generated )
328
+ *
329
+ * @webref pvector:method
330
+ * @usage web_application
331
+ * @brief Make a new 2D unit vector from an angle
332
+ * @param angle the angle in radians
333
+ * @return the new unit PVector
334
+ */
335
+ static public PVector fromAngle(float angle) {
336
+ return fromAngle(angle, null);
787
337
  }
788
- return target;
789
- }
790
-
791
-
792
- /**
793
- * ( begin auto-generated from PVector_normalize.xml )
794
- *
795
- * Normalize the vector to length 1 (make it a unit vector).
796
- *
797
- * ( end auto-generated )
798
- *
799
- * @webref pvector:method
800
- * @usage web_application
801
- * @brief Normalize the vector to a length of 1
802
- */
803
- public PVector normalize() {
804
- float m = mag();
805
- if (m != 0 && m != 1) {
806
- div(m);
338
+
339
+ /**
340
+ * Make a new 2D unit vector from an angle
341
+ *
342
+ * @param target the target vector (if null, a new vector will be created)
343
+ * @return the PVector
344
+ */
345
+ static public PVector fromAngle(float angle, PVector target) {
346
+ if (target == null) {
347
+ target = new PVector((float) Math.cos(angle), (float) Math.sin(angle), 0);
348
+ } else {
349
+ target.set((float) Math.cos(angle), (float) Math.sin(angle), 0);
350
+ }
351
+ return target;
807
352
  }
808
- return this;
809
- }
810
353
 
354
+ /**
355
+ * ( begin auto-generated from PVector_copy.xml )
356
+ *
357
+ * Gets a copy of the vector, returns a PVector object.
358
+ *
359
+ * ( end auto-generated )
360
+ *
361
+ * @webref pvector:method
362
+ * @usage web_application
363
+ * @brief Get a copy of the vector
364
+ */
365
+ public PVector copy() {
366
+ return new PVector(x, y, z);
367
+ }
811
368
 
812
- /**
813
- * @param target Set to null to create a new vector
814
- * @return a new vector (if target was null), or target
815
- */
816
- public PVector normalize(PVector target) {
817
- if (target == null) {
818
- target = new PVector();
369
+ @Deprecated
370
+ public PVector get() {
371
+ return copy();
819
372
  }
820
- float m = mag();
821
- if (m > 0) {
822
- target.set(x/m, y/m, z/m);
823
- } else {
824
- target.set(x, y, z);
373
+
374
+ /**
375
+ * @param target
376
+ */
377
+ public float[] get(float[] target) {
378
+ if (target == null) {
379
+ return new float[]{x, y, z};
380
+ }
381
+ if (target.length >= 2) {
382
+ target[0] = x;
383
+ target[1] = y;
384
+ }
385
+ if (target.length >= 3) {
386
+ target[2] = z;
387
+ }
388
+ return target;
825
389
  }
826
- return target;
827
- }
828
-
829
-
830
- /**
831
- * ( begin auto-generated from PVector_limit.xml )
832
- *
833
- * Limit the magnitude of this vector to the value used for the <b>max</b> parameter.
834
- *
835
- * ( end auto-generated )
836
- *
837
- * @webref pvector:method
838
- * @usage web_application
839
- * @param max the maximum magnitude for the vector
840
- * @brief Limit the magnitude of the vector
841
- */
842
- public PVector limit(float max) {
843
- if (magSq() > max*max) {
844
- normalize();
845
- mult(max);
390
+
391
+ /**
392
+ * ( begin auto-generated from PVector_mag.xml )
393
+ *
394
+ * Calculates the magnitude (length) of the vector and returns the result as
395
+ * a float (this is simply the equation <em>sqrt(x*x + y*y + z*z)</em>.)
396
+ *
397
+ * ( end auto-generated )
398
+ *
399
+ * @webref pvector:method
400
+ * @usage web_application
401
+ * @brief Calculate the magnitude of the vector
402
+ * @return magnitude (length) of the vector
403
+ * @see PVector#magSq()
404
+ */
405
+ public float mag() {
406
+ return (float) Math.sqrt(x * x + y * y + z * z);
846
407
  }
847
- return this;
848
- }
849
-
850
-
851
- /**
852
- * ( begin auto-generated from PVector_setMag.xml )
853
- *
854
- * Set the magnitude of this vector to the value used for the <b>len</b> parameter.
855
- *
856
- * ( end auto-generated )
857
- *
858
- * @webref pvector:method
859
- * @usage web_application
860
- * @param len the new length for this vector
861
- * @brief Set the magnitude of the vector
862
- */
863
- public PVector setMag(float len) {
864
- normalize();
865
- mult(len);
866
- return this;
867
- }
868
-
869
-
870
- /**
871
- * Sets the magnitude of this vector, storing the result in another vector.
872
- * @param target Set to null to create a new vector
873
- * @param len the new length for the new vector
874
- * @return a new vector (if target was null), or target
875
- */
876
- public PVector setMag(PVector target, float len) {
877
- target = normalize(target);
878
- target.mult(len);
879
- return target;
880
- }
881
-
882
-
883
- /**
884
- * ( begin auto-generated from PVector_setMag.xml )
885
- *
886
- * Calculate the angle of rotation for this vector (only 2D vectors)
887
- *
888
- * ( end auto-generated )
889
- *
890
- * @webref pvector:method
891
- * @usage web_application
892
- * @return the angle of rotation
893
- * @brief Calculate the angle of rotation for this vector
894
- */
895
- public float heading() {
896
- float angle = (float) Math.atan2(y, x);
897
- return angle;
898
- }
899
-
900
-
901
- @Deprecated
902
- public float heading2D() {
903
- return heading();
904
- }
905
-
906
-
907
- /**
908
- * ( begin auto-generated from PVector_rotate.xml )
909
- *
910
- * Rotate the vector by an angle (only 2D vectors), magnitude remains the same
911
- *
912
- * ( end auto-generated )
913
- *
914
- * @webref pvector:method
915
- * @usage web_application
916
- * @brief Rotate the vector by an angle (2D only)
917
- * @param theta the angle of rotation
918
- */
919
- public PVector rotate(float theta) {
920
- float temp = x;
921
- // Might need to check for rounding errors like with angleBetween function?
922
- x = x*PApplet.cos(theta) - y*PApplet.sin(theta);
923
- y = temp*PApplet.sin(theta) + y*PApplet.cos(theta);
924
- return this;
925
- }
926
-
927
-
928
- /**
929
- * ( begin auto-generated from PVector_rotate.xml )
930
- *
931
- * Linear interpolate the vector to another vector
932
- *
933
- * ( end auto-generated )
934
- *
935
- * @webref pvector:method
936
- * @usage web_application
937
- * @brief Linear interpolate the vector to another vector
938
- * @param v the vector to lerp to
939
- * @param amt The amount of interpolation; some value between 0.0 (old vector) and 1.0 (new vector). 0.1 is very near the old vector; 0.5 is halfway in between.
940
- * @see PApplet#lerp(float, float, float)
941
- */
942
- public PVector lerp(PVector v, float amt) {
943
- x = PApplet.lerp(x, v.x, amt);
944
- y = PApplet.lerp(y, v.y, amt);
945
- z = PApplet.lerp(z, v.z, amt);
946
- return this;
947
- }
948
-
949
-
950
- /**
951
- * Linear interpolate between two vectors (returns a new PVector object)
952
- * @param v1 the vector to start from
953
- * @param v2 the vector to lerp to
954
- */
955
- public static PVector lerp(PVector v1, PVector v2, float amt) {
956
- PVector v = v1.copy();
957
- v.lerp(v2, amt);
958
- return v;
959
- }
960
-
961
-
962
- /**
963
- * Linear interpolate the vector to x,y,z values
964
- * @param x the x component to lerp to
965
- * @param y the y component to lerp to
966
- * @param z the z component to lerp to
967
- */
968
- public PVector lerp(float x, float y, float z, float amt) {
969
- this.x = PApplet.lerp(this.x, x, amt);
970
- this.y = PApplet.lerp(this.y, y, amt);
971
- this.z = PApplet.lerp(this.z, z, amt);
972
- return this;
973
- }
974
-
975
-
976
- /**
977
- * ( begin auto-generated from PVector_angleBetween.xml )
978
- *
979
- * Calculates and returns the angle (in radians) between two vectors.
980
- *
981
- * ( end auto-generated )
982
- *
983
- * @webref pvector:method
984
- * @usage web_application
985
- * @param v1 the x, y, and z components of a PVector
986
- * @param v2 the x, y, and z components of a PVector
987
- * @brief Calculate and return the angle between two vectors
988
- */
989
- static public float angleBetween(PVector v1, PVector v2) {
990
-
991
- // We get NaN if we pass in a zero vector which can cause problems
992
- // Zero seems like a reasonable angle between a (0,0,0) vector and something else
993
- if (v1.x == 0 && v1.y == 0 && v1.z == 0 ) return 0.0f;
994
- if (v2.x == 0 && v2.y == 0 && v2.z == 0 ) return 0.0f;
995
-
996
- double dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
997
- double v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
998
- double v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
999
- // This should be a number between -1 and 1, since it's "normalized"
1000
- double amt = dot / (v1mag * v2mag);
1001
- // But if it's not due to rounding error, then we need to fix it
1002
- // http://code.google.com/p/processing/issues/detail?id=340
1003
- // Otherwise if outside the range, acos() will return NaN
1004
- // http://www.cppreference.com/wiki/c/math/acos
1005
- if (amt <= -1) {
1006
- return PConstants.PI;
1007
- } else if (amt >= 1) {
1008
- // http://code.google.com/p/processing/issues/detail?id=435
1009
- return 0;
408
+
409
+ /**
410
+ * ( begin auto-generated from PVector_mag.xml )
411
+ *
412
+ * Calculates the squared magnitude of the vector and returns the result as
413
+ * a float (this is simply the equation <em>(x*x + y*y + z*z)</em>.) Faster
414
+ * if the real length is not required in the case of comparing vectors, etc.
415
+ *
416
+ * ( end auto-generated )
417
+ *
418
+ * @webref pvector:method
419
+ * @usage web_application
420
+ * @brief Calculate the magnitude of the vector, squared
421
+ * @return squared magnitude of the vector
422
+ * @see PVector#mag()
423
+ */
424
+ public float magSq() {
425
+ return (x * x + y * y + z * z);
1010
426
  }
1011
- return (float) Math.acos(amt);
1012
- }
1013
-
1014
-
1015
- @Override
1016
- public String toString() {
1017
- return "[ " + x + ", " + y + ", " + z + " ]";
1018
- }
1019
-
1020
-
1021
- /**
1022
- * ( begin auto-generated from PVector_array.xml )
1023
- *
1024
- * Return a representation of this vector as a float array. This is only
1025
- * for temporary use. If used in any other fashion, the contents should be
1026
- * copied by using the <b>PVector.get()</b> method to copy into your own array.
1027
- *
1028
- * ( end auto-generated )
1029
- *
1030
- * @webref pvector:method
1031
- * @usage: web_application
1032
- * @brief Return a representation of the vector as a float array
1033
- */
1034
- public float[] array() {
1035
- if (array == null) {
1036
- array = new float[3];
427
+
428
+ /**
429
+ * ( begin auto-generated from PVector_add.xml )
430
+ *
431
+ * Adds x, y, and z components to a vector, adds one vector to another, or
432
+ * adds two independent vectors together. The version of the method that
433
+ * adds two vectors together is a static method and returns a PVector, the
434
+ * others have no return value -- they act directly on the vector. See the
435
+ * examples for more context.
436
+ *
437
+ * ( end auto-generated )
438
+ *
439
+ * @webref pvector:method
440
+ * @usage web_application
441
+ * @param v the vector to be added
442
+ * @brief Adds x, y, and z components to a vector, one vector to another, or
443
+ * two independent vectors
444
+ */
445
+ public PVector add(PVector v) {
446
+ x += v.x;
447
+ y += v.y;
448
+ z += v.z;
449
+ return this;
1037
450
  }
1038
- array[0] = x;
1039
- array[1] = y;
1040
- array[2] = z;
1041
- return array;
1042
- }
1043
451
 
452
+ /**
453
+ * @param x x component of the vector
454
+ * @param y y component of the vector
455
+ */
456
+ public PVector add(float x, float y) {
457
+ this.x += x;
458
+ this.y += y;
459
+ return this;
460
+ }
461
+
462
+ /**
463
+ * @param z z component of the vector
464
+ */
465
+ public PVector add(float x, float y, float z) {
466
+ this.x += x;
467
+ this.y += y;
468
+ this.z += z;
469
+ return this;
470
+ }
471
+
472
+ /**
473
+ * Add two vectors
474
+ *
475
+ * @param v1 a vector
476
+ * @param v2 another vector
477
+ */
478
+ static public PVector add(PVector v1, PVector v2) {
479
+ return add(v1, v2, null);
480
+ }
481
+
482
+ /**
483
+ * Add two vectors into a target vector
484
+ *
485
+ * @param target the target vector (if null, a new vector will be created)
486
+ */
487
+ static public PVector add(PVector v1, PVector v2, PVector target) {
488
+ if (target == null) {
489
+ target = new PVector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
490
+ } else {
491
+ target.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
492
+ }
493
+ return target;
494
+ }
495
+
496
+ /**
497
+ * ( begin auto-generated from PVector_sub.xml )
498
+ *
499
+ * Subtracts x, y, and z components from a vector, subtracts one vector from
500
+ * another, or subtracts two independent vectors. The version of the method
501
+ * that subtracts two vectors is a static method and returns a PVector, the
502
+ * others have no return value -- they act directly on the vector. See the
503
+ * examples for more context.
504
+ *
505
+ * ( end auto-generated )
506
+ *
507
+ * @webref pvector:method
508
+ * @usage web_application
509
+ * @param v any variable of type PVector
510
+ * @brief Subtract x, y, and z components from a vector, one vector from
511
+ * another, or two independent vectors
512
+ */
513
+ public PVector sub(PVector v) {
514
+ x -= v.x;
515
+ y -= v.y;
516
+ z -= v.z;
517
+ return this;
518
+ }
519
+
520
+ /**
521
+ * @param x the x component of the vector
522
+ * @param y the y component of the vector
523
+ */
524
+ public PVector sub(float x, float y) {
525
+ this.x -= x;
526
+ this.y -= y;
527
+ return this;
528
+ }
529
+
530
+ /**
531
+ * @param z the z component of the vector
532
+ */
533
+ public PVector sub(float x, float y, float z) {
534
+ this.x -= x;
535
+ this.y -= y;
536
+ this.z -= z;
537
+ return this;
538
+ }
539
+
540
+ /**
541
+ * Subtract one vector from another
542
+ *
543
+ * @param v1 the x, y, and z components of a PVector object
544
+ * @param v2 the x, y, and z components of a PVector object
545
+ */
546
+ static public PVector sub(PVector v1, PVector v2) {
547
+ return sub(v1, v2, null);
548
+ }
549
+
550
+ /**
551
+ * Subtract one vector from another and store in another vector
552
+ *
553
+ * @param target PVector in which to store the result
554
+ */
555
+ static public PVector sub(PVector v1, PVector v2, PVector target) {
556
+ if (target == null) {
557
+ target = new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
558
+ } else {
559
+ target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
560
+ }
561
+ return target;
562
+ }
563
+
564
+ /**
565
+ * ( begin auto-generated from PVector_mult.xml )
566
+ *
567
+ * Multiplies a vector by a scalar or multiplies one vector by another.
568
+ *
569
+ * ( end auto-generated )
570
+ *
571
+ * @webref pvector:method
572
+ * @usage web_application
573
+ * @brief Multiply a vector by a scalar
574
+ * @param n the number to multiply with the vector
575
+ */
576
+ public PVector mult(float n) {
577
+ x *= n;
578
+ y *= n;
579
+ z *= n;
580
+ return this;
581
+ }
582
+
583
+ /**
584
+ * @param v the vector to multiply by the scalar
585
+ */
586
+ static public PVector mult(PVector v, float n) {
587
+ return mult(v, n, null);
588
+ }
589
+
590
+ /**
591
+ * Multiply a vector by a scalar, and write the result into a target
592
+ * PVector.
593
+ *
594
+ * @param target PVector in which to store the result
595
+ */
596
+ static public PVector mult(PVector v, float n, PVector target) {
597
+ if (target == null) {
598
+ target = new PVector(v.x * n, v.y * n, v.z * n);
599
+ } else {
600
+ target.set(v.x * n, v.y * n, v.z * n);
601
+ }
602
+ return target;
603
+ }
604
+
605
+ /**
606
+ * ( begin auto-generated from PVector_div.xml )
607
+ *
608
+ * Divides a vector by a scalar or divides one vector by another.
609
+ *
610
+ * ( end auto-generated )
611
+ *
612
+ * @webref pvector:method
613
+ * @usage web_application
614
+ * @brief Divide a vector by a scalar
615
+ * @param n the number by which to divide the vector
616
+ */
617
+ public PVector div(float n) {
618
+ x /= n;
619
+ y /= n;
620
+ z /= n;
621
+ return this;
622
+ }
623
+
624
+ /**
625
+ * Divide a vector by a scalar and return the result in a new vector.
626
+ *
627
+ * @param v the vector to divide by the scalar
628
+ * @return a new vector that is v1 / n
629
+ */
630
+ static public PVector div(PVector v, float n) {
631
+ return div(v, n, null);
632
+ }
633
+
634
+ /**
635
+ * Divide a vector by a scalar and store the result in another vector.
636
+ *
637
+ * @param target PVector in which to store the result
638
+ */
639
+ static public PVector div(PVector v, float n, PVector target) {
640
+ if (target == null) {
641
+ target = new PVector(v.x / n, v.y / n, v.z / n);
642
+ } else {
643
+ target.set(v.x / n, v.y / n, v.z / n);
644
+ }
645
+ return target;
646
+ }
647
+
648
+ /**
649
+ * ( begin auto-generated from PVector_dist.xml )
650
+ *
651
+ * Calculates the Euclidean distance between two points (considering a point
652
+ * as a vector object).
653
+ *
654
+ * ( end auto-generated )
655
+ *
656
+ * @webref pvector:method
657
+ * @usage web_application
658
+ * @param v the x, y, and z coordinates of a PVector
659
+ * @brief Calculate the distance between two points
660
+ */
661
+ public float dist(PVector v) {
662
+ float dx = x - v.x;
663
+ float dy = y - v.y;
664
+ float dz = z - v.z;
665
+ return (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
666
+ }
667
+
668
+ /**
669
+ * @param v1 any variable of type PVector
670
+ * @param v2 any variable of type PVector
671
+ * @return the Euclidean distance between v1 and v2
672
+ */
673
+ static public float dist(PVector v1, PVector v2) {
674
+ float dx = v1.x - v2.x;
675
+ float dy = v1.y - v2.y;
676
+ float dz = v1.z - v2.z;
677
+ return (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
678
+ }
679
+
680
+ /**
681
+ * ( begin auto-generated from PVector_dot.xml )
682
+ *
683
+ * Calculates the dot product of two vectors.
684
+ *
685
+ * ( end auto-generated )
686
+ *
687
+ * @webref pvector:method
688
+ * @usage web_application
689
+ * @param v any variable of type PVector
690
+ * @return the dot product
691
+ * @brief Calculate the dot product of two vectors
692
+ */
693
+ public float dot(PVector v) {
694
+ return x * v.x + y * v.y + z * v.z;
695
+ }
696
+
697
+ /**
698
+ * @param x x component of the vector
699
+ * @param y y component of the vector
700
+ * @param z z component of the vector
701
+ */
702
+ public float dot(float x, float y, float z) {
703
+ return this.x * x + this.y * y + this.z * z;
704
+ }
705
+
706
+ /**
707
+ * @param v1 any variable of type PVector
708
+ * @param v2 any variable of type PVector
709
+ */
710
+ static public float dot(PVector v1, PVector v2) {
711
+ return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
712
+ }
713
+
714
+ /**
715
+ * ( begin auto-generated from PVector_cross.xml )
716
+ *
717
+ * Calculates and returns a vector composed of the cross product between two
718
+ * vectors.
719
+ *
720
+ * ( end auto-generated )
721
+ *
722
+ * @webref pvector:method
723
+ * @param v the vector to calculate the cross product
724
+ * @brief Calculate and return the cross product
725
+ */
726
+ public PVector cross(PVector v) {
727
+ return cross(v, null);
728
+ }
729
+
730
+ /**
731
+ * @param v any variable of type PVector
732
+ * @param target PVector to store the result
733
+ */
734
+ public PVector cross(PVector v, PVector target) {
735
+ float crossX = y * v.z - v.y * z;
736
+ float crossY = z * v.x - v.z * x;
737
+ float crossZ = x * v.y - v.x * y;
738
+
739
+ if (target == null) {
740
+ target = new PVector(crossX, crossY, crossZ);
741
+ } else {
742
+ target.set(crossX, crossY, crossZ);
743
+ }
744
+ return target;
745
+ }
746
+
747
+ /**
748
+ * @param v1 any variable of type PVector
749
+ * @param v2 any variable of type PVector
750
+ * @param target PVector to store the result
751
+ */
752
+ static public PVector cross(PVector v1, PVector v2, PVector target) {
753
+ float crossX = v1.y * v2.z - v2.y * v1.z;
754
+ float crossY = v1.z * v2.x - v2.z * v1.x;
755
+ float crossZ = v1.x * v2.y - v2.x * v1.y;
756
+
757
+ if (target == null) {
758
+ target = new PVector(crossX, crossY, crossZ);
759
+ } else {
760
+ target.set(crossX, crossY, crossZ);
761
+ }
762
+ return target;
763
+ }
764
+
765
+ /**
766
+ * ( begin auto-generated from PVector_normalize.xml )
767
+ *
768
+ * Normalize the vector to length 1 (make it a unit vector).
769
+ *
770
+ * ( end auto-generated )
771
+ *
772
+ * @webref pvector:method
773
+ * @usage web_application
774
+ * @brief Normalize the vector to a length of 1
775
+ */
776
+ public PVector normalize() {
777
+ float m = mag();
778
+ if (m != 0 && m != 1) {
779
+ div(m);
780
+ }
781
+ return this;
782
+ }
783
+
784
+ /**
785
+ * @param target Set to null to create a new vector
786
+ * @return a new vector (if target was null), or target
787
+ */
788
+ public PVector normalize(PVector target) {
789
+ if (target == null) {
790
+ target = new PVector();
791
+ }
792
+ float m = mag();
793
+ if (m > 0) {
794
+ target.set(x / m, y / m, z / m);
795
+ } else {
796
+ target.set(x, y, z);
797
+ }
798
+ return target;
799
+ }
800
+
801
+ /**
802
+ * ( begin auto-generated from PVector_limit.xml )
803
+ *
804
+ * Limit the magnitude of this vector to the value used for the <b>max</b>
805
+ * parameter.
806
+ *
807
+ * ( end auto-generated )
808
+ *
809
+ * @webref pvector:method
810
+ * @usage web_application
811
+ * @param max the maximum magnitude for the vector
812
+ * @brief Limit the magnitude of the vector
813
+ */
814
+ public PVector limit(float max) {
815
+ if (magSq() > max * max) {
816
+ normalize();
817
+ mult(max);
818
+ }
819
+ return this;
820
+ }
821
+
822
+ /**
823
+ * ( begin auto-generated from PVector_setMag.xml )
824
+ *
825
+ * Set the magnitude of this vector to the value used for the <b>len</b>
826
+ * parameter.
827
+ *
828
+ * ( end auto-generated )
829
+ *
830
+ * @webref pvector:method
831
+ * @usage web_application
832
+ * @param len the new length for this vector
833
+ * @brief Set the magnitude of the vector
834
+ */
835
+ public PVector setMag(float len) {
836
+ normalize();
837
+ mult(len);
838
+ return this;
839
+ }
840
+
841
+ /**
842
+ * Sets the magnitude of this vector, storing the result in another vector.
843
+ *
844
+ * @param target Set to null to create a new vector
845
+ * @param len the new length for the new vector
846
+ * @return a new vector (if target was null), or target
847
+ */
848
+ public PVector setMag(PVector target, float len) {
849
+ target = normalize(target);
850
+ target.mult(len);
851
+ return target;
852
+ }
853
+
854
+ /**
855
+ * ( begin auto-generated from PVector_setMag.xml )
856
+ *
857
+ * Calculate the angle of rotation for this vector (only 2D vectors)
858
+ *
859
+ * ( end auto-generated )
860
+ *
861
+ * @webref pvector:method
862
+ * @usage web_application
863
+ * @return the angle of rotation
864
+ * @brief Calculate the angle of rotation for this vector
865
+ */
866
+ public float heading() {
867
+ float angle = (float) Math.atan2(y, x);
868
+ return angle;
869
+ }
870
+
871
+ @Deprecated
872
+ public float heading2D() {
873
+ return heading();
874
+ }
875
+
876
+ /**
877
+ * ( begin auto-generated from PVector_rotate.xml )
878
+ *
879
+ * Rotate the vector by an angle (only 2D vectors), magnitude remains the
880
+ * same
881
+ *
882
+ * ( end auto-generated )
883
+ *
884
+ * @webref pvector:method
885
+ * @usage web_application
886
+ * @brief Rotate the vector by an angle (2D only)
887
+ * @param theta the angle of rotation
888
+ */
889
+ public PVector rotate(float theta) {
890
+ float temp = x;
891
+ // Might need to check for rounding errors like with angleBetween function?
892
+ x = x * PApplet.cos(theta) - y * PApplet.sin(theta);
893
+ y = temp * PApplet.sin(theta) + y * PApplet.cos(theta);
894
+ return this;
895
+ }
896
+
897
+ /**
898
+ * ( begin auto-generated from PVector_rotate.xml )
899
+ *
900
+ * Linear interpolate the vector to another vector
901
+ *
902
+ * ( end auto-generated )
903
+ *
904
+ * @webref pvector:method
905
+ * @usage web_application
906
+ * @brief Linear interpolate the vector to another vector
907
+ * @param v the vector to lerp to
908
+ * @param amt The amount of interpolation; some value between 0.0 (old
909
+ * vector) and 1.0 (new vector). 0.1 is very near the old vector; 0.5 is
910
+ * halfway in between.
911
+ * @see PApplet#lerp(float, float, float)
912
+ */
913
+ public PVector lerp(PVector v, float amt) {
914
+ x = PApplet.lerp(x, v.x, amt);
915
+ y = PApplet.lerp(y, v.y, amt);
916
+ z = PApplet.lerp(z, v.z, amt);
917
+ return this;
918
+ }
919
+
920
+ /**
921
+ * Linear interpolate between two vectors (returns a new PVector object)
922
+ *
923
+ * @param v1 the vector to start from
924
+ * @param v2 the vector to lerp to
925
+ */
926
+ public static PVector lerp(PVector v1, PVector v2, float amt) {
927
+ PVector v = v1.copy();
928
+ v.lerp(v2, amt);
929
+ return v;
930
+ }
931
+
932
+ /**
933
+ * Linear interpolate the vector to x,y,z values
934
+ *
935
+ * @param x the x component to lerp to
936
+ * @param y the y component to lerp to
937
+ * @param z the z component to lerp to
938
+ */
939
+ public PVector lerp(float x, float y, float z, float amt) {
940
+ this.x = PApplet.lerp(this.x, x, amt);
941
+ this.y = PApplet.lerp(this.y, y, amt);
942
+ this.z = PApplet.lerp(this.z, z, amt);
943
+ return this;
944
+ }
945
+
946
+ /**
947
+ * ( begin auto-generated from PVector_angleBetween.xml )
948
+ *
949
+ * Calculates and returns the angle (in radians) between two vectors.
950
+ *
951
+ * ( end auto-generated )
952
+ *
953
+ * @webref pvector:method
954
+ * @usage web_application
955
+ * @param v1 the x, y, and z components of a PVector
956
+ * @param v2 the x, y, and z components of a PVector
957
+ * @brief Calculate and return the angle between two vectors
958
+ */
959
+ static public float angleBetween(PVector v1, PVector v2) {
960
+
961
+ // We get NaN if we pass in a zero vector which can cause problems
962
+ // Zero seems like a reasonable angle between a (0,0,0) vector and something else
963
+ if (v1.x == 0 && v1.y == 0 && v1.z == 0) {
964
+ return 0.0f;
965
+ }
966
+ if (v2.x == 0 && v2.y == 0 && v2.z == 0) {
967
+ return 0.0f;
968
+ }
969
+
970
+ double dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
971
+ double v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
972
+ double v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
973
+ // This should be a number between -1 and 1, since it's "normalized"
974
+ double amt = dot / (v1mag * v2mag);
975
+ // But if it's not due to rounding error, then we need to fix it
976
+ // http://code.google.com/p/processing/issues/detail?id=340
977
+ // Otherwise if outside the range, acos() will return NaN
978
+ // http://www.cppreference.com/wiki/c/math/acos
979
+ if (amt <= -1) {
980
+ return PConstants.PI;
981
+ } else if (amt >= 1) {
982
+ // http://code.google.com/p/processing/issues/detail?id=435
983
+ return 0;
984
+ }
985
+ return (float) Math.acos(amt);
986
+ }
987
+
988
+ @Override
989
+ public String toString() {
990
+ return "[ " + x + ", " + y + ", " + z + " ]";
991
+ }
992
+
993
+ /**
994
+ * ( begin auto-generated from PVector_array.xml )
995
+ *
996
+ * Return a representation of this vector as a float array. This is only for
997
+ * temporary use. If used in any other fashion, the contents should be
998
+ * copied by using the <b>PVector.get()</b> method to copy into your own
999
+ * array.
1000
+ *
1001
+ * ( end auto-generated )
1002
+ *
1003
+ * @webref pvector:method
1004
+ * @usage: web_application
1005
+ * @brief Return a representation of the vector as a float array
1006
+ */
1007
+ public float[] array() {
1008
+ if (array == null) {
1009
+ array = new float[3];
1010
+ }
1011
+ array[0] = x;
1012
+ array[1] = y;
1013
+ array[2] = z;
1014
+ return array;
1015
+ }
1016
+
1017
+ @Override
1018
+ public boolean equals(Object obj) {
1019
+ if (!(obj instanceof PVector)) {
1020
+ return false;
1021
+ }
1022
+ final PVector p = (PVector) obj;
1023
+ return x == p.x && y == p.y && z == p.z;
1024
+ }
1044
1025
 
1045
- @Override
1046
- public boolean equals(Object obj) {
1047
- if (!(obj instanceof PVector)) {
1048
- return false;
1026
+ @Override
1027
+ public int hashCode() {
1028
+ int result = 1;
1029
+ result = 31 * result + Float.floatToIntBits(x);
1030
+ result = 31 * result + Float.floatToIntBits(y);
1031
+ result = 31 * result + Float.floatToIntBits(z);
1032
+ return result;
1049
1033
  }
1050
- final PVector p = (PVector) obj;
1051
- return x == p.x && y == p.y && z == p.z;
1052
- }
1053
-
1054
-
1055
- @Override
1056
- public int hashCode() {
1057
- int result = 1;
1058
- result = 31 * result + Float.floatToIntBits(x);
1059
- result = 31 * result + Float.floatToIntBits(y);
1060
- result = 31 * result + Float.floatToIntBits(z);
1061
- return result;
1062
- }
1063
1034
  }