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