picrate 0.0.2-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +47 -0
- data/.mvn/extensions.xml +9 -0
- data/.mvn/wrapper/maven-wrapper.properties +1 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE.md +165 -0
- data/README.md +51 -0
- data/Rakefile +59 -0
- data/bin/picrate +8 -0
- data/docs/.gitignore +6 -0
- data/docs/_config.yml +30 -0
- data/docs/_includes/footer.html +38 -0
- data/docs/_includes/head.html +16 -0
- data/docs/_includes/header.html +27 -0
- data/docs/_includes/icon-github.html +1 -0
- data/docs/_includes/icon-github.svg +1 -0
- data/docs/_includes/icon-twitter.html +1 -0
- data/docs/_includes/icon-twitter.svg +1 -0
- data/docs/_includes/navigation.html +24 -0
- data/docs/_layouts/default.html +20 -0
- data/docs/_layouts/page.html +14 -0
- data/docs/_layouts/post.html +15 -0
- data/docs/_posts/2018-05-06-getting_started.md +8 -0
- data/docs/_posts/2018-05-06-install_jruby.md +35 -0
- data/docs/_sass/_base.scss +206 -0
- data/docs/_sass/_layout.scss +242 -0
- data/docs/_sass/_syntax-highlighting.scss +71 -0
- data/docs/about.md +10 -0
- data/docs/css/main.scss +38 -0
- data/docs/favicon.ico +0 -0
- data/docs/feed.xml +30 -0
- data/docs/index.html +38 -0
- data/lib/picrate.rb +10 -0
- data/lib/picrate/app.rb +187 -0
- data/lib/picrate/creators/sketch_class.rb +57 -0
- data/lib/picrate/creators/sketch_factory.rb +12 -0
- data/lib/picrate/creators/sketch_writer.rb +21 -0
- data/lib/picrate/helper_methods.rb +214 -0
- data/lib/picrate/helpers/numeric.rb +9 -0
- data/lib/picrate/library.rb +69 -0
- data/lib/picrate/library_loader.rb +53 -0
- data/lib/picrate/native_folder.rb +35 -0
- data/lib/picrate/native_loader.rb +27 -0
- data/lib/picrate/runner.rb +81 -0
- data/lib/picrate/version.rb +4 -0
- data/library/boids/boids.rb +209 -0
- data/library/chooser/chooser.rb +19 -0
- data/library/control_panel/control_panel.rb +182 -0
- data/library/library_proxy/README.md +99 -0
- data/library/library_proxy/library_proxy.rb +14 -0
- data/library/slider/slider.rb +42 -0
- data/library/vector_utils/vector_utils.rb +69 -0
- data/library/video_event/video_event.rb +3 -0
- data/license.txt +508 -0
- data/picrate.gemspec +35 -0
- data/pom.rb +122 -0
- data/pom.xml +214 -0
- data/src/main/java/japplemenubar/JAppleMenuBar.java +88 -0
- data/src/main/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
- data/src/main/java/monkstone/ColorUtil.java +115 -0
- data/src/main/java/monkstone/MathToolModule.java +236 -0
- data/src/main/java/monkstone/PicrateLibrary.java +47 -0
- data/src/main/java/monkstone/core/LibraryProxy.java +127 -0
- data/src/main/java/monkstone/fastmath/Deglut.java +122 -0
- data/src/main/java/monkstone/fastmath/package-info.java +6 -0
- data/src/main/java/monkstone/filechooser/Chooser.java +48 -0
- data/src/main/java/monkstone/noise/SimplexNoise.java +465 -0
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +168 -0
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +182 -0
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +149 -0
- data/src/main/java/monkstone/slider/SimpleSlider.java +196 -0
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +163 -0
- data/src/main/java/monkstone/slider/Slider.java +67 -0
- data/src/main/java/monkstone/slider/SliderBar.java +277 -0
- data/src/main/java/monkstone/slider/SliderGroup.java +78 -0
- data/src/main/java/monkstone/slider/WheelHandler.java +35 -0
- data/src/main/java/monkstone/vecmath/AppRender.java +87 -0
- data/src/main/java/monkstone/vecmath/JRender.java +56 -0
- data/src/main/java/monkstone/vecmath/ShapeRender.java +87 -0
- data/src/main/java/monkstone/vecmath/package-info.java +20 -0
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +757 -0
- data/src/main/java/monkstone/vecmath/vec2/package-info.java +6 -0
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +727 -0
- data/src/main/java/monkstone/vecmath/vec3/package-info.java +6 -0
- data/src/main/java/monkstone/videoevent/VideoInterface.java +42 -0
- data/src/main/java/monkstone/videoevent/package-info.java +20 -0
- data/src/main/java/processing/awt/PGraphicsJava2D.java +3098 -0
- data/src/main/java/processing/awt/PShapeJava2D.java +401 -0
- data/src/main/java/processing/awt/PSurfaceAWT.java +1660 -0
- data/src/main/java/processing/core/PApplet.java +17647 -0
- data/src/main/java/processing/core/PConstants.java +1033 -0
- data/src/main/java/processing/core/PFont.java +1250 -0
- data/src/main/java/processing/core/PGraphics.java +9614 -0
- data/src/main/java/processing/core/PImage.java +3608 -0
- data/src/main/java/processing/core/PMatrix.java +347 -0
- data/src/main/java/processing/core/PMatrix2D.java +694 -0
- data/src/main/java/processing/core/PMatrix3D.java +1153 -0
- data/src/main/java/processing/core/PShape.java +4332 -0
- data/src/main/java/processing/core/PShapeOBJ.java +544 -0
- data/src/main/java/processing/core/PShapeSVG.java +1987 -0
- data/src/main/java/processing/core/PStyle.java +208 -0
- data/src/main/java/processing/core/PSurface.java +242 -0
- data/src/main/java/processing/core/PSurfaceNone.java +479 -0
- data/src/main/java/processing/core/PVector.java +1140 -0
- data/src/main/java/processing/data/FloatDict.java +829 -0
- data/src/main/java/processing/data/FloatList.java +912 -0
- data/src/main/java/processing/data/IntDict.java +796 -0
- data/src/main/java/processing/data/IntList.java +913 -0
- data/src/main/java/processing/data/JSONArray.java +1260 -0
- data/src/main/java/processing/data/JSONObject.java +2282 -0
- data/src/main/java/processing/data/JSONTokener.java +435 -0
- data/src/main/java/processing/data/Sort.java +46 -0
- data/src/main/java/processing/data/StringDict.java +601 -0
- data/src/main/java/processing/data/StringList.java +775 -0
- data/src/main/java/processing/data/Table.java +4923 -0
- data/src/main/java/processing/data/TableRow.java +198 -0
- data/src/main/java/processing/data/XML.java +1149 -0
- data/src/main/java/processing/event/Event.java +108 -0
- data/src/main/java/processing/event/KeyEvent.java +70 -0
- data/src/main/java/processing/event/MouseEvent.java +149 -0
- data/src/main/java/processing/event/TouchEvent.java +57 -0
- data/src/main/java/processing/javafx/PGraphicsFX2D.java +354 -0
- data/src/main/java/processing/opengl/FontTexture.java +379 -0
- data/src/main/java/processing/opengl/FrameBuffer.java +503 -0
- data/src/main/java/processing/opengl/LinePath.java +623 -0
- data/src/main/java/processing/opengl/LineStroker.java +685 -0
- data/src/main/java/processing/opengl/PGL.java +3366 -0
- data/src/main/java/processing/opengl/PGraphics2D.java +615 -0
- data/src/main/java/processing/opengl/PGraphics3D.java +281 -0
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13634 -0
- data/src/main/java/processing/opengl/PJOGL.java +1966 -0
- data/src/main/java/processing/opengl/PShader.java +1478 -0
- data/src/main/java/processing/opengl/PShapeOpenGL.java +5234 -0
- data/src/main/java/processing/opengl/PSurfaceJOGL.java +1315 -0
- data/src/main/java/processing/opengl/Texture.java +1670 -0
- data/src/main/java/processing/opengl/VertexBuffer.java +88 -0
- data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
- data/src/main/java/processing/opengl/cursors/cross.png +0 -0
- data/src/main/java/processing/opengl/cursors/hand.png +0 -0
- data/src/main/java/processing/opengl/cursors/license.txt +27 -0
- data/src/main/java/processing/opengl/cursors/move.png +0 -0
- data/src/main/java/processing/opengl/cursors/text.png +0 -0
- data/src/main/java/processing/opengl/cursors/wait.png +0 -0
- data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/ColorVert.glsl +34 -0
- data/src/main/java/processing/opengl/shaders/LightFrag.glsl +33 -0
- data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +154 -0
- data/src/main/java/processing/opengl/shaders/LightVert.glsl +151 -0
- data/src/main/java/processing/opengl/shaders/LineFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/LineVert.glsl +100 -0
- data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
- data/src/main/java/processing/opengl/shaders/PointFrag.glsl +32 -0
- data/src/main/java/processing/opengl/shaders/PointVert.glsl +56 -0
- data/src/main/java/processing/opengl/shaders/TexFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +160 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
- data/src/main/java/processing/opengl/shaders/TexVert.glsl +38 -0
- data/src/main/resources/icon/icon-1024.png +0 -0
- data/src/main/resources/icon/icon-128.png +0 -0
- data/src/main/resources/icon/icon-16.png +0 -0
- data/src/main/resources/icon/icon-256.png +0 -0
- data/src/main/resources/icon/icon-32.png +0 -0
- data/src/main/resources/icon/icon-48.png +0 -0
- data/src/main/resources/icon/icon-512.png +0 -0
- data/src/main/resources/icon/icon-64.png +0 -0
- data/src/main/resources/license.txt +508 -0
- data/test/create_test.rb +68 -0
- data/test/deglut_spec_test.rb +24 -0
- data/test/helper_methods_test.rb +58 -0
- data/test/math_tool_test.rb +75 -0
- data/test/respond_to_test.rb +215 -0
- data/test/sketches/key_event.rb +37 -0
- data/test/sketches/library/my_library/my_library.rb +32 -0
- data/test/test_helper.rb +3 -0
- data/test/vecmath_spec_test.rb +522 -0
- data/vendors/Rakefile +127 -0
- metadata +289 -0
@@ -0,0 +1,1140 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Part of the Processing project - http://processing.org
|
5
|
+
|
6
|
+
Copyright (c) 2012-17 The Processing Foundation
|
7
|
+
Copyright (c) 2008-12 Ben Fry and Casey Reas
|
8
|
+
Copyright (c) 2008 Dan Shiffman
|
9
|
+
|
10
|
+
This library is free software; you can redistribute it and/or
|
11
|
+
modify it under the terms of the GNU Lesser General Public
|
12
|
+
License version 2.1 as published by the Free Software Foundation.
|
13
|
+
|
14
|
+
This library is distributed in the hope that it will be useful,
|
15
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17
|
+
Lesser General Public License for more details.
|
18
|
+
|
19
|
+
You should have received a copy of the GNU Lesser General
|
20
|
+
Public License along with this library; if not, write to the
|
21
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
22
|
+
Boston, MA 02111-1307 USA
|
23
|
+
*/
|
24
|
+
|
25
|
+
package processing.core;
|
26
|
+
|
27
|
+
import java.io.Serializable;
|
28
|
+
|
29
|
+
import processing.core.PApplet;
|
30
|
+
import processing.core.PConstants;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* ( begin auto-generated from PVector.xml )
|
34
|
+
*
|
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>
|
48
|
+
* class.<br />
|
49
|
+
* <br />
|
50
|
+
* The methods for this class are extensive. For a complete list, visit the
|
51
|
+
* <a
|
52
|
+
* href="http://processing.googlecode.com/svn/trunk/processing/build/javadoc/core/">developer's reference.</a>
|
53
|
+
*
|
54
|
+
* ( end auto-generated )
|
55
|
+
*
|
56
|
+
* A class to describe a two or three dimensional vector.
|
57
|
+
* <p>
|
58
|
+
* The result of all functions are applied to the vector itself, with the
|
59
|
+
* 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.
|
66
|
+
* <p>
|
67
|
+
* Initially based on the Vector3D class by <a href="http://www.shiffman.net">Dan Shiffman</a>.
|
68
|
+
*
|
69
|
+
* @webref math
|
70
|
+
*/
|
71
|
+
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
|
+
* @param x
|
142
|
+
* @param y
|
143
|
+
*/
|
144
|
+
public PVector(float x, float y) {
|
145
|
+
this.x = x;
|
146
|
+
this.y = y;
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
/**
|
151
|
+
* ( begin auto-generated from PVector_set.xml )
|
152
|
+
*
|
153
|
+
* Sets the x, y, and z component of the vector using two or three separate
|
154
|
+
* variables, the data from a PVector, or the values from a float array.
|
155
|
+
*
|
156
|
+
* ( end auto-generated )
|
157
|
+
*
|
158
|
+
* @return
|
159
|
+
* @webref pvector:method
|
160
|
+
* @param x the x component of the vector
|
161
|
+
* @param y the y component of the vector
|
162
|
+
* @param z the z component of the vector
|
163
|
+
* @brief Set the components of the vector
|
164
|
+
*/
|
165
|
+
public PVector set(float x, float y, float z) {
|
166
|
+
this.x = x;
|
167
|
+
this.y = y;
|
168
|
+
this.z = z;
|
169
|
+
return this;
|
170
|
+
}
|
171
|
+
|
172
|
+
|
173
|
+
/**
|
174
|
+
* @param x the x component of the vector
|
175
|
+
* @param y the y component of the vector
|
176
|
+
* @return
|
177
|
+
*/
|
178
|
+
public PVector set(float x, float y) {
|
179
|
+
this.x = x;
|
180
|
+
this.y = y;
|
181
|
+
this.z = 0;
|
182
|
+
return this;
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
/**
|
187
|
+
* @param v any variable of type PVector
|
188
|
+
* @return
|
189
|
+
*/
|
190
|
+
public PVector set(PVector v) {
|
191
|
+
x = v.x;
|
192
|
+
y = v.y;
|
193
|
+
z = v.z;
|
194
|
+
return this;
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
/**
|
199
|
+
* Set the x, y (and maybe z) coordinates using a float[] array as the source.
|
200
|
+
* @param source array to copy from
|
201
|
+
* @return
|
202
|
+
*/
|
203
|
+
public PVector set(float[] source) {
|
204
|
+
if (source.length >= 2) {
|
205
|
+
x = source[0];
|
206
|
+
y = source[1];
|
207
|
+
}
|
208
|
+
if (source.length >= 3) {
|
209
|
+
z = source[2];
|
210
|
+
} else {
|
211
|
+
z = 0;
|
212
|
+
}
|
213
|
+
return this;
|
214
|
+
}
|
215
|
+
|
216
|
+
|
217
|
+
/**
|
218
|
+
* ( begin auto-generated from PVector_random2D.xml )
|
219
|
+
*
|
220
|
+
* Make a new 2D unit vector with a random direction. If you pass in "this"
|
221
|
+
* as an argument, it will use the PApplet's random number generator. You can
|
222
|
+
* also pass in a target PVector to fill.
|
223
|
+
*
|
224
|
+
* @webref pvector:method
|
225
|
+
* @usage web_application
|
226
|
+
* @return the random PVector
|
227
|
+
* @brief Make a new 2D unit vector with a random direction.
|
228
|
+
* @see PVector#random3D()
|
229
|
+
*/
|
230
|
+
static public PVector random2D() {
|
231
|
+
return random2D(null, null);
|
232
|
+
}
|
233
|
+
|
234
|
+
|
235
|
+
/**
|
236
|
+
* Make a new 2D unit vector with a random direction
|
237
|
+
* using Processing's current random number generator
|
238
|
+
* @param parent current PApplet instance
|
239
|
+
* @return the random PVector
|
240
|
+
*/
|
241
|
+
static public PVector random2D(PApplet parent) {
|
242
|
+
return random2D(null, parent);
|
243
|
+
}
|
244
|
+
|
245
|
+
/**
|
246
|
+
* Set a 2D vector to a random unit vector with a random direction
|
247
|
+
* @param target the target vector (if null, a new vector will be created)
|
248
|
+
* @return the random PVector
|
249
|
+
*/
|
250
|
+
static public PVector random2D(PVector target) {
|
251
|
+
return random2D(target, null);
|
252
|
+
}
|
253
|
+
|
254
|
+
|
255
|
+
/**
|
256
|
+
* Make a new 2D unit vector with a random direction. Pass in the parent
|
257
|
+
* PApplet if you want randomSeed() to work (and be predictable). Or leave
|
258
|
+
* it null and be... random.
|
259
|
+
* @param target
|
260
|
+
* @param parent
|
261
|
+
* @return the random PVector
|
262
|
+
*/
|
263
|
+
static public PVector random2D(PVector target, PApplet parent) {
|
264
|
+
return (parent == null) ?
|
265
|
+
fromAngle((float) (Math.random() * Math.PI*2), target) :
|
266
|
+
fromAngle(parent.random(PConstants.TAU), target);
|
267
|
+
}
|
268
|
+
|
269
|
+
|
270
|
+
/**
|
271
|
+
* ( begin auto-generated from PVector_random3D.xml )
|
272
|
+
*
|
273
|
+
* Make a new 3D unit vector with a random direction. If you pass in "this"
|
274
|
+
* as an argument, it will use the PApplet's random number generator. You can
|
275
|
+
* also pass in a target PVector to fill.
|
276
|
+
*
|
277
|
+
* @webref pvector:method
|
278
|
+
* @usage web_application
|
279
|
+
* @return the random PVector
|
280
|
+
* @brief Make a new 3D unit vector with a random direction.
|
281
|
+
* @see PVector#random2D()
|
282
|
+
*/
|
283
|
+
static public PVector random3D() {
|
284
|
+
return random3D(null, null);
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
/**
|
289
|
+
* Make a new 3D unit vector with a random direction
|
290
|
+
* using Processing's current random number generator
|
291
|
+
* @param parent current PApplet instance
|
292
|
+
* @return the random PVector
|
293
|
+
*/
|
294
|
+
static public PVector random3D(PApplet parent) {
|
295
|
+
return random3D(null, parent);
|
296
|
+
}
|
297
|
+
|
298
|
+
|
299
|
+
/**
|
300
|
+
* Set a 3D vector to a random unit vector with a random direction
|
301
|
+
* @param target the target vector (if null, a new vector will be created)
|
302
|
+
* @return the random PVector
|
303
|
+
*/
|
304
|
+
static public PVector random3D(PVector target) {
|
305
|
+
return random3D(target, null);
|
306
|
+
}
|
307
|
+
|
308
|
+
|
309
|
+
/**
|
310
|
+
* Make a new 3D unit vector with a random direction
|
311
|
+
* @param target
|
312
|
+
* @param parent
|
313
|
+
* @return the random PVector
|
314
|
+
*/
|
315
|
+
static public PVector random3D(PVector target, PApplet parent) {
|
316
|
+
float angle;
|
317
|
+
float vz;
|
318
|
+
if (parent == null) {
|
319
|
+
angle = (float) (Math.random()*Math.PI*2);
|
320
|
+
vz = (float) (Math.random()*2-1);
|
321
|
+
} else {
|
322
|
+
angle = parent.random(PConstants.TWO_PI);
|
323
|
+
vz = parent.random(-1,1);
|
324
|
+
}
|
325
|
+
float vx = (float) (Math.sqrt(1-vz*vz)*Math.cos(angle));
|
326
|
+
float vy = (float) (Math.sqrt(1-vz*vz)*Math.sin(angle));
|
327
|
+
if (target == null) {
|
328
|
+
target = new PVector(vx, vy, vz);
|
329
|
+
//target.normalize(); // Should be unnecessary
|
330
|
+
} else {
|
331
|
+
target.set(vx,vy,vz);
|
332
|
+
}
|
333
|
+
return target;
|
334
|
+
}
|
335
|
+
|
336
|
+
|
337
|
+
/**
|
338
|
+
* ( begin auto-generated from PVector_sub.xml )
|
339
|
+
*
|
340
|
+
* Make a new 2D unit vector from an angle.
|
341
|
+
*
|
342
|
+
* ( end auto-generated )
|
343
|
+
*
|
344
|
+
* @webref pvector:method
|
345
|
+
* @usage web_application
|
346
|
+
* @brief Make a new 2D unit vector from an angle
|
347
|
+
* @param angle the angle in radians
|
348
|
+
* @return the new unit PVector
|
349
|
+
*/
|
350
|
+
static public PVector fromAngle(float angle) {
|
351
|
+
return fromAngle(angle,null);
|
352
|
+
}
|
353
|
+
|
354
|
+
|
355
|
+
/**
|
356
|
+
* Make a new 2D unit vector from an angle
|
357
|
+
*
|
358
|
+
* @param angle
|
359
|
+
* @param target the target vector (if null, a new vector will be created)
|
360
|
+
* @return the PVector
|
361
|
+
*/
|
362
|
+
static public PVector fromAngle(float angle, PVector target) {
|
363
|
+
if (target == null) {
|
364
|
+
target = new PVector((float)Math.cos(angle),(float)Math.sin(angle),0);
|
365
|
+
} else {
|
366
|
+
target.set((float)Math.cos(angle),(float)Math.sin(angle),0);
|
367
|
+
}
|
368
|
+
return target;
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
/**
|
373
|
+
* ( begin auto-generated from PVector_copy.xml )
|
374
|
+
*
|
375
|
+
* Gets a copy of the vector, returns a PVector object.
|
376
|
+
*
|
377
|
+
* ( end auto-generated )
|
378
|
+
*
|
379
|
+
* @return
|
380
|
+
* @webref pvector:method
|
381
|
+
* @usage web_application
|
382
|
+
* @brief Get a copy of the vector
|
383
|
+
*/
|
384
|
+
public PVector copy() {
|
385
|
+
return new PVector(x, y, z);
|
386
|
+
}
|
387
|
+
|
388
|
+
/**
|
389
|
+
*
|
390
|
+
* @return
|
391
|
+
* @deprecated
|
392
|
+
*/
|
393
|
+
@Deprecated
|
394
|
+
public PVector get() {
|
395
|
+
return copy();
|
396
|
+
}
|
397
|
+
|
398
|
+
|
399
|
+
/**
|
400
|
+
* @param target
|
401
|
+
* @return
|
402
|
+
*/
|
403
|
+
public float[] get(float[] target) {
|
404
|
+
if (target == null) {
|
405
|
+
return new float[] { x, y, z };
|
406
|
+
}
|
407
|
+
if (target.length >= 2) {
|
408
|
+
target[0] = x;
|
409
|
+
target[1] = y;
|
410
|
+
}
|
411
|
+
if (target.length >= 3) {
|
412
|
+
target[2] = z;
|
413
|
+
}
|
414
|
+
return target;
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
/**
|
419
|
+
* ( begin auto-generated from PVector_mag.xml )
|
420
|
+
*
|
421
|
+
* Calculates the magnitude (length) of the vector and returns the result
|
422
|
+
* as a float (this is simply the equation <em>sqrt(x*x + y*y + z*z)</em>.)
|
423
|
+
*
|
424
|
+
* ( end auto-generated )
|
425
|
+
*
|
426
|
+
* @webref pvector:method
|
427
|
+
* @usage web_application
|
428
|
+
* @brief Calculate the magnitude of the vector
|
429
|
+
* @return magnitude (length) of the vector
|
430
|
+
* @see PVector#magSq()
|
431
|
+
*/
|
432
|
+
public float mag() {
|
433
|
+
return (float) Math.sqrt(x*x + y*y + z*z);
|
434
|
+
}
|
435
|
+
|
436
|
+
|
437
|
+
/**
|
438
|
+
* ( begin auto-generated from PVector_mag.xml )
|
439
|
+
*
|
440
|
+
* Calculates the squared magnitude of the vector and returns the result
|
441
|
+
* as a float (this is simply the equation <em>(x*x + y*y + z*z)</em>.)
|
442
|
+
* Faster if the real length is not required in the
|
443
|
+
* case of comparing vectors, etc.
|
444
|
+
*
|
445
|
+
* ( end auto-generated )
|
446
|
+
*
|
447
|
+
* @webref pvector:method
|
448
|
+
* @usage web_application
|
449
|
+
* @brief Calculate the magnitude of the vector, squared
|
450
|
+
* @return squared magnitude of the vector
|
451
|
+
* @see PVector#mag()
|
452
|
+
*/
|
453
|
+
public float magSq() {
|
454
|
+
return (x*x + y*y + z*z);
|
455
|
+
}
|
456
|
+
|
457
|
+
|
458
|
+
/**
|
459
|
+
* ( begin auto-generated from PVector_add.xml )
|
460
|
+
*
|
461
|
+
* Adds x, y, and z components to a vector, adds one vector to another, or
|
462
|
+
* adds two independent vectors together. The version of the method that
|
463
|
+
* adds two vectors together is a static method and returns a PVector, the
|
464
|
+
* others have no return value -- they act directly on the vector. See the
|
465
|
+
* examples for more context.
|
466
|
+
*
|
467
|
+
* ( end auto-generated )
|
468
|
+
*
|
469
|
+
* @return
|
470
|
+
* @webref pvector:method
|
471
|
+
* @usage web_application
|
472
|
+
* @param v the vector to be added
|
473
|
+
* @brief Adds x, y, and z components to a vector, one vector to another, or two independent vectors
|
474
|
+
*/
|
475
|
+
public PVector add(PVector v) {
|
476
|
+
x += v.x;
|
477
|
+
y += v.y;
|
478
|
+
z += v.z;
|
479
|
+
return this;
|
480
|
+
}
|
481
|
+
|
482
|
+
|
483
|
+
/**
|
484
|
+
* @param x x component of the vector
|
485
|
+
* @param y y component of the vector
|
486
|
+
* @return
|
487
|
+
*/
|
488
|
+
public PVector add(float x, float y) {
|
489
|
+
this.x += x;
|
490
|
+
this.y += y;
|
491
|
+
return this;
|
492
|
+
}
|
493
|
+
|
494
|
+
|
495
|
+
/**
|
496
|
+
* @param x
|
497
|
+
* @param y
|
498
|
+
* @param z z component of the vector
|
499
|
+
* @return
|
500
|
+
*/
|
501
|
+
public PVector add(float x, float y, float z) {
|
502
|
+
this.x += x;
|
503
|
+
this.y += y;
|
504
|
+
this.z += z;
|
505
|
+
return this;
|
506
|
+
}
|
507
|
+
|
508
|
+
|
509
|
+
/**
|
510
|
+
* Add two vectors
|
511
|
+
* @param v1 a vector
|
512
|
+
* @param v2 another vector
|
513
|
+
* @return
|
514
|
+
*/
|
515
|
+
static public PVector add(PVector v1, PVector v2) {
|
516
|
+
return add(v1, v2, null);
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
/**
|
521
|
+
* Add two vectors into a target vector
|
522
|
+
* @param v1
|
523
|
+
* @param target the target vector (if null, a new vector will be created)
|
524
|
+
* @param v2
|
525
|
+
* @return
|
526
|
+
*/
|
527
|
+
static public PVector add(PVector v1, PVector v2, PVector target) {
|
528
|
+
if (target == null) {
|
529
|
+
target = new PVector(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
|
530
|
+
} else {
|
531
|
+
target.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
|
532
|
+
}
|
533
|
+
return target;
|
534
|
+
}
|
535
|
+
|
536
|
+
|
537
|
+
/**
|
538
|
+
* ( begin auto-generated from PVector_sub.xml )
|
539
|
+
*
|
540
|
+
* Subtracts x, y, and z components from a vector, subtracts one vector
|
541
|
+
* from another, or subtracts two independent vectors. The version of the
|
542
|
+
* method that subtracts two vectors is a static method and returns a
|
543
|
+
* PVector, the others have no return value -- they act directly on the
|
544
|
+
* vector. See the examples for more context.
|
545
|
+
*
|
546
|
+
* ( end auto-generated )
|
547
|
+
*
|
548
|
+
* @return
|
549
|
+
* @webref pvector:method
|
550
|
+
* @usage web_application
|
551
|
+
* @param v any variable of type PVector
|
552
|
+
* @brief Subtract x, y, and z components from a vector, one vector from another, or two independent vectors
|
553
|
+
*/
|
554
|
+
public PVector sub(PVector v) {
|
555
|
+
x -= v.x;
|
556
|
+
y -= v.y;
|
557
|
+
z -= v.z;
|
558
|
+
return this;
|
559
|
+
}
|
560
|
+
|
561
|
+
|
562
|
+
/**
|
563
|
+
* @param x the x component of the vector
|
564
|
+
* @param y the y component of the vector
|
565
|
+
* @return
|
566
|
+
*/
|
567
|
+
public PVector sub(float x, float y) {
|
568
|
+
this.x -= x;
|
569
|
+
this.y -= y;
|
570
|
+
return this;
|
571
|
+
}
|
572
|
+
|
573
|
+
|
574
|
+
/**
|
575
|
+
* @param x
|
576
|
+
* @param y
|
577
|
+
* @param z the z component of the vector
|
578
|
+
* @return
|
579
|
+
*/
|
580
|
+
public PVector sub(float x, float y, float z) {
|
581
|
+
this.x -= x;
|
582
|
+
this.y -= y;
|
583
|
+
this.z -= z;
|
584
|
+
return this;
|
585
|
+
}
|
586
|
+
|
587
|
+
|
588
|
+
/**
|
589
|
+
* Subtract one vector from another
|
590
|
+
* @param v1 the x, y, and z components of a PVector object
|
591
|
+
* @param v2 the x, y, and z components of a PVector object
|
592
|
+
* @return
|
593
|
+
*/
|
594
|
+
static public PVector sub(PVector v1, PVector v2) {
|
595
|
+
return sub(v1, v2, null);
|
596
|
+
}
|
597
|
+
|
598
|
+
|
599
|
+
/**
|
600
|
+
* Subtract one vector from another and store in another vector
|
601
|
+
* @param v1
|
602
|
+
* @param v2
|
603
|
+
* @param target PVector in which to store the result
|
604
|
+
* @return
|
605
|
+
*/
|
606
|
+
static public PVector sub(PVector v1, PVector v2, PVector target) {
|
607
|
+
if (target == null) {
|
608
|
+
target = new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
609
|
+
} else {
|
610
|
+
target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
611
|
+
}
|
612
|
+
return target;
|
613
|
+
}
|
614
|
+
|
615
|
+
|
616
|
+
/**
|
617
|
+
* ( begin auto-generated from PVector_mult.xml )
|
618
|
+
*
|
619
|
+
* Multiplies a vector by a scalar or multiplies one vector by another.
|
620
|
+
*
|
621
|
+
* ( end auto-generated )
|
622
|
+
*
|
623
|
+
* @return
|
624
|
+
* @webref pvector:method
|
625
|
+
* @usage web_application
|
626
|
+
* @brief Multiply a vector by a scalar
|
627
|
+
* @param n the number to multiply with the vector
|
628
|
+
*/
|
629
|
+
public PVector mult(float n) {
|
630
|
+
x *= n;
|
631
|
+
y *= n;
|
632
|
+
z *= n;
|
633
|
+
return this;
|
634
|
+
}
|
635
|
+
|
636
|
+
|
637
|
+
/**
|
638
|
+
* @param v the vector to multiply by the scalar
|
639
|
+
* @param n
|
640
|
+
* @return
|
641
|
+
*/
|
642
|
+
static public PVector mult(PVector v, float n) {
|
643
|
+
return mult(v, n, null);
|
644
|
+
}
|
645
|
+
|
646
|
+
|
647
|
+
/**
|
648
|
+
* Multiply a vector by a scalar, and write the result into a target PVector.
|
649
|
+
* @param v
|
650
|
+
* @param target PVector in which to store the result
|
651
|
+
* @param n
|
652
|
+
* @return
|
653
|
+
*/
|
654
|
+
static public PVector mult(PVector v, float n, PVector target) {
|
655
|
+
if (target == null) {
|
656
|
+
target = new PVector(v.x*n, v.y*n, v.z*n);
|
657
|
+
} else {
|
658
|
+
target.set(v.x*n, v.y*n, v.z*n);
|
659
|
+
}
|
660
|
+
return target;
|
661
|
+
}
|
662
|
+
|
663
|
+
|
664
|
+
/**
|
665
|
+
* ( begin auto-generated from PVector_div.xml )
|
666
|
+
*
|
667
|
+
* Divides a vector by a scalar or divides one vector by another.
|
668
|
+
*
|
669
|
+
* ( end auto-generated )
|
670
|
+
*
|
671
|
+
* @return
|
672
|
+
* @webref pvector:method
|
673
|
+
* @usage web_application
|
674
|
+
* @brief Divide a vector by a scalar
|
675
|
+
* @param n the number by which to divide the vector
|
676
|
+
*/
|
677
|
+
public PVector div(float n) {
|
678
|
+
x /= n;
|
679
|
+
y /= n;
|
680
|
+
z /= n;
|
681
|
+
return this;
|
682
|
+
}
|
683
|
+
|
684
|
+
|
685
|
+
/**
|
686
|
+
* Divide a vector by a scalar and return the result in a new vector.
|
687
|
+
* @param v the vector to divide by the scalar
|
688
|
+
* @param n
|
689
|
+
* @return a new vector that is v1 / n
|
690
|
+
*/
|
691
|
+
static public PVector div(PVector v, float n) {
|
692
|
+
return div(v, n, null);
|
693
|
+
}
|
694
|
+
|
695
|
+
|
696
|
+
/**
|
697
|
+
* Divide a vector by a scalar and store the result in another vector.
|
698
|
+
* @param v
|
699
|
+
* @param target PVector in which to store the result
|
700
|
+
* @param n
|
701
|
+
* @return
|
702
|
+
*/
|
703
|
+
static public PVector div(PVector v, float n, PVector target) {
|
704
|
+
if (target == null) {
|
705
|
+
target = new PVector(v.x/n, v.y/n, v.z/n);
|
706
|
+
} else {
|
707
|
+
target.set(v.x/n, v.y/n, v.z/n);
|
708
|
+
}
|
709
|
+
return target;
|
710
|
+
}
|
711
|
+
|
712
|
+
|
713
|
+
/**
|
714
|
+
* ( begin auto-generated from PVector_dist.xml )
|
715
|
+
*
|
716
|
+
* Calculates the Euclidean distance between two points (considering a
|
717
|
+
* point as a vector object).
|
718
|
+
*
|
719
|
+
* ( end auto-generated )
|
720
|
+
*
|
721
|
+
* @return
|
722
|
+
* @webref pvector:method
|
723
|
+
* @usage web_application
|
724
|
+
* @param v the x, y, and z coordinates of a PVector
|
725
|
+
* @brief Calculate the distance between two points
|
726
|
+
*/
|
727
|
+
public float dist(PVector v) {
|
728
|
+
float dx = x - v.x;
|
729
|
+
float dy = y - v.y;
|
730
|
+
float dz = z - v.z;
|
731
|
+
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
|
732
|
+
}
|
733
|
+
|
734
|
+
|
735
|
+
/**
|
736
|
+
* @param v1 any variable of type PVector
|
737
|
+
* @param v2 any variable of type PVector
|
738
|
+
* @return the Euclidean distance between v1 and v2
|
739
|
+
*/
|
740
|
+
static public float dist(PVector v1, PVector v2) {
|
741
|
+
float dx = v1.x - v2.x;
|
742
|
+
float dy = v1.y - v2.y;
|
743
|
+
float dz = v1.z - v2.z;
|
744
|
+
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
|
745
|
+
}
|
746
|
+
|
747
|
+
|
748
|
+
/**
|
749
|
+
* ( begin auto-generated from PVector_dot.xml )
|
750
|
+
*
|
751
|
+
* Calculates the dot product of two vectors.
|
752
|
+
*
|
753
|
+
* ( end auto-generated )
|
754
|
+
*
|
755
|
+
* @webref pvector:method
|
756
|
+
* @usage web_application
|
757
|
+
* @param v any variable of type PVector
|
758
|
+
* @return the dot product
|
759
|
+
* @brief Calculate the dot product of two vectors
|
760
|
+
*/
|
761
|
+
public float dot(PVector v) {
|
762
|
+
return x*v.x + y*v.y + z*v.z;
|
763
|
+
}
|
764
|
+
|
765
|
+
|
766
|
+
/**
|
767
|
+
* @param x x component of the vector
|
768
|
+
* @param y y component of the vector
|
769
|
+
* @param z z component of the vector
|
770
|
+
* @return
|
771
|
+
*/
|
772
|
+
public float dot(float x, float y, float z) {
|
773
|
+
return this.x*x + this.y*y + this.z*z;
|
774
|
+
}
|
775
|
+
|
776
|
+
|
777
|
+
/**
|
778
|
+
* @param v1 any variable of type PVector
|
779
|
+
* @param v2 any variable of type PVector
|
780
|
+
* @return
|
781
|
+
*/
|
782
|
+
static public float dot(PVector v1, PVector v2) {
|
783
|
+
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
|
784
|
+
}
|
785
|
+
|
786
|
+
|
787
|
+
/**
|
788
|
+
* ( begin auto-generated from PVector_cross.xml )
|
789
|
+
*
|
790
|
+
* Calculates and returns a vector composed of the cross product between
|
791
|
+
* two vectors.
|
792
|
+
*
|
793
|
+
* ( end auto-generated )
|
794
|
+
*
|
795
|
+
* @return
|
796
|
+
* @webref pvector:method
|
797
|
+
* @param v the vector to calculate the cross product
|
798
|
+
* @brief Calculate and return the cross product
|
799
|
+
*/
|
800
|
+
public PVector cross(PVector v) {
|
801
|
+
return cross(v, null);
|
802
|
+
}
|
803
|
+
|
804
|
+
|
805
|
+
/**
|
806
|
+
* @param v any variable of type PVector
|
807
|
+
* @param target PVector to store the result
|
808
|
+
* @return
|
809
|
+
*/
|
810
|
+
public PVector cross(PVector v, PVector target) {
|
811
|
+
float crossX = y * v.z - v.y * z;
|
812
|
+
float crossY = z * v.x - v.z * x;
|
813
|
+
float crossZ = x * v.y - v.x * y;
|
814
|
+
|
815
|
+
if (target == null) {
|
816
|
+
target = new PVector(crossX, crossY, crossZ);
|
817
|
+
} else {
|
818
|
+
target.set(crossX, crossY, crossZ);
|
819
|
+
}
|
820
|
+
return target;
|
821
|
+
}
|
822
|
+
|
823
|
+
|
824
|
+
/**
|
825
|
+
* @param v1 any variable of type PVector
|
826
|
+
* @param v2 any variable of type PVector
|
827
|
+
* @param target PVector to store the result
|
828
|
+
* @return
|
829
|
+
*/
|
830
|
+
static public PVector cross(PVector v1, PVector v2, PVector target) {
|
831
|
+
float crossX = v1.y * v2.z - v2.y * v1.z;
|
832
|
+
float crossY = v1.z * v2.x - v2.z * v1.x;
|
833
|
+
float crossZ = v1.x * v2.y - v2.x * v1.y;
|
834
|
+
|
835
|
+
if (target == null) {
|
836
|
+
target = new PVector(crossX, crossY, crossZ);
|
837
|
+
} else {
|
838
|
+
target.set(crossX, crossY, crossZ);
|
839
|
+
}
|
840
|
+
return target;
|
841
|
+
}
|
842
|
+
|
843
|
+
|
844
|
+
/**
|
845
|
+
* ( begin auto-generated from PVector_normalize.xml )
|
846
|
+
*
|
847
|
+
* Normalize the vector to length 1 (make it a unit vector).
|
848
|
+
*
|
849
|
+
* ( end auto-generated )
|
850
|
+
*
|
851
|
+
* @return
|
852
|
+
* @webref pvector:method
|
853
|
+
* @usage web_application
|
854
|
+
* @brief Normalize the vector to a length of 1
|
855
|
+
*/
|
856
|
+
public PVector normalize() {
|
857
|
+
float m = mag();
|
858
|
+
if (m != 0 && m != 1) {
|
859
|
+
div(m);
|
860
|
+
}
|
861
|
+
return this;
|
862
|
+
}
|
863
|
+
|
864
|
+
|
865
|
+
/**
|
866
|
+
* @param target Set to null to create a new vector
|
867
|
+
* @return a new vector (if target was null), or target
|
868
|
+
*/
|
869
|
+
public PVector normalize(PVector target) {
|
870
|
+
if (target == null) {
|
871
|
+
target = new PVector();
|
872
|
+
}
|
873
|
+
float m = mag();
|
874
|
+
if (m > 0) {
|
875
|
+
target.set(x/m, y/m, z/m);
|
876
|
+
} else {
|
877
|
+
target.set(x, y, z);
|
878
|
+
}
|
879
|
+
return target;
|
880
|
+
}
|
881
|
+
|
882
|
+
|
883
|
+
/**
|
884
|
+
* ( begin auto-generated from PVector_limit.xml )
|
885
|
+
*
|
886
|
+
* Limit the magnitude of this vector to the value used for the <b>max</b> parameter.
|
887
|
+
*
|
888
|
+
* ( end auto-generated )
|
889
|
+
*
|
890
|
+
* @return
|
891
|
+
* @webref pvector:method
|
892
|
+
* @usage web_application
|
893
|
+
* @param max the maximum magnitude for the vector
|
894
|
+
* @brief Limit the magnitude of the vector
|
895
|
+
*/
|
896
|
+
public PVector limit(float max) {
|
897
|
+
if (magSq() > max*max) {
|
898
|
+
normalize();
|
899
|
+
mult(max);
|
900
|
+
}
|
901
|
+
return this;
|
902
|
+
}
|
903
|
+
|
904
|
+
|
905
|
+
/**
|
906
|
+
* ( begin auto-generated from PVector_setMag.xml )
|
907
|
+
*
|
908
|
+
* Set the magnitude of this vector to the value used for the <b>len</b> parameter.
|
909
|
+
*
|
910
|
+
* ( end auto-generated )
|
911
|
+
*
|
912
|
+
* @return
|
913
|
+
* @webref pvector:method
|
914
|
+
* @usage web_application
|
915
|
+
* @param len the new length for this vector
|
916
|
+
* @brief Set the magnitude of the vector
|
917
|
+
*/
|
918
|
+
public PVector setMag(float len) {
|
919
|
+
normalize();
|
920
|
+
mult(len);
|
921
|
+
return this;
|
922
|
+
}
|
923
|
+
|
924
|
+
|
925
|
+
/**
|
926
|
+
* Sets the magnitude of this vector, storing the result in another vector.
|
927
|
+
* @param target Set to null to create a new vector
|
928
|
+
* @param len the new length for the new vector
|
929
|
+
* @return a new vector (if target was null), or target
|
930
|
+
*/
|
931
|
+
public PVector setMag(PVector target, float len) {
|
932
|
+
target = normalize(target);
|
933
|
+
target.mult(len);
|
934
|
+
return target;
|
935
|
+
}
|
936
|
+
|
937
|
+
|
938
|
+
/**
|
939
|
+
* ( begin auto-generated from PVector_setMag.xml )
|
940
|
+
*
|
941
|
+
* Calculate the angle of rotation for this vector (only 2D vectors)
|
942
|
+
*
|
943
|
+
* ( end auto-generated )
|
944
|
+
*
|
945
|
+
* @webref pvector:method
|
946
|
+
* @usage web_application
|
947
|
+
* @return the angle of rotation
|
948
|
+
* @brief Calculate the angle of rotation for this vector
|
949
|
+
*/
|
950
|
+
public float heading() {
|
951
|
+
float angle = (float) Math.atan2(y, x);
|
952
|
+
return angle;
|
953
|
+
}
|
954
|
+
|
955
|
+
/**
|
956
|
+
*
|
957
|
+
* @return
|
958
|
+
* @deprecated
|
959
|
+
*/
|
960
|
+
@Deprecated
|
961
|
+
public float heading2D() {
|
962
|
+
return heading();
|
963
|
+
}
|
964
|
+
|
965
|
+
|
966
|
+
/**
|
967
|
+
* ( begin auto-generated from PVector_rotate.xml )
|
968
|
+
*
|
969
|
+
* Rotate the vector by an angle (only 2D vectors), magnitude remains the same
|
970
|
+
*
|
971
|
+
* ( end auto-generated )
|
972
|
+
*
|
973
|
+
* @return
|
974
|
+
* @webref pvector:method
|
975
|
+
* @usage web_application
|
976
|
+
* @brief Rotate the vector by an angle (2D only)
|
977
|
+
* @param theta the angle of rotation
|
978
|
+
*/
|
979
|
+
public PVector rotate(float theta) {
|
980
|
+
float temp = x;
|
981
|
+
// Might need to check for rounding errors like with angleBetween function?
|
982
|
+
x = x*PApplet.cos(theta) - y*PApplet.sin(theta);
|
983
|
+
y = temp*PApplet.sin(theta) + y*PApplet.cos(theta);
|
984
|
+
return this;
|
985
|
+
}
|
986
|
+
|
987
|
+
|
988
|
+
/**
|
989
|
+
* ( begin auto-generated from PVector_rotate.xml )
|
990
|
+
*
|
991
|
+
* Linear interpolate the vector to another vector
|
992
|
+
*
|
993
|
+
* ( end auto-generated )
|
994
|
+
*
|
995
|
+
* @return
|
996
|
+
* @webref pvector:method
|
997
|
+
* @usage web_application
|
998
|
+
* @brief Linear interpolate the vector to another vector
|
999
|
+
* @param v the vector to lerp to
|
1000
|
+
* @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.
|
1001
|
+
* @see PApplet#lerp(float, float, float)
|
1002
|
+
*/
|
1003
|
+
public PVector lerp(PVector v, float amt) {
|
1004
|
+
x = PApplet.lerp(x, v.x, amt);
|
1005
|
+
y = PApplet.lerp(y, v.y, amt);
|
1006
|
+
z = PApplet.lerp(z, v.z, amt);
|
1007
|
+
return this;
|
1008
|
+
}
|
1009
|
+
|
1010
|
+
|
1011
|
+
/**
|
1012
|
+
* Linear interpolate between two vectors (returns a new PVector object)
|
1013
|
+
* @param v1 the vector to start from
|
1014
|
+
* @param v2 the vector to lerp to
|
1015
|
+
* @param amt
|
1016
|
+
* @return
|
1017
|
+
*/
|
1018
|
+
public static PVector lerp(PVector v1, PVector v2, float amt) {
|
1019
|
+
PVector v = v1.copy();
|
1020
|
+
v.lerp(v2, amt);
|
1021
|
+
return v;
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
|
1025
|
+
/**
|
1026
|
+
* Linear interpolate the vector to x,y,z values
|
1027
|
+
* @param x the x component to lerp to
|
1028
|
+
* @param y the y component to lerp to
|
1029
|
+
* @param z the z component to lerp to
|
1030
|
+
* @param amt
|
1031
|
+
* @return
|
1032
|
+
*/
|
1033
|
+
public PVector lerp(float x, float y, float z, float amt) {
|
1034
|
+
this.x = PApplet.lerp(this.x, x, amt);
|
1035
|
+
this.y = PApplet.lerp(this.y, y, amt);
|
1036
|
+
this.z = PApplet.lerp(this.z, z, amt);
|
1037
|
+
return this;
|
1038
|
+
}
|
1039
|
+
|
1040
|
+
|
1041
|
+
/**
|
1042
|
+
* ( begin auto-generated from PVector_angleBetween.xml )
|
1043
|
+
*
|
1044
|
+
* Calculates and returns the angle (in radians) between two vectors.
|
1045
|
+
*
|
1046
|
+
* ( end auto-generated )
|
1047
|
+
*
|
1048
|
+
* @return
|
1049
|
+
* @webref pvector:method
|
1050
|
+
* @usage web_application
|
1051
|
+
* @param v1 the x, y, and z components of a PVector
|
1052
|
+
* @param v2 the x, y, and z components of a PVector
|
1053
|
+
* @brief Calculate and return the angle between two vectors
|
1054
|
+
*/
|
1055
|
+
static public float angleBetween(PVector v1, PVector v2) {
|
1056
|
+
|
1057
|
+
// We get NaN if we pass in a zero vector which can cause problems
|
1058
|
+
// Zero seems like a reasonable angle between a (0,0,0) vector and something else
|
1059
|
+
if (v1.x == 0 && v1.y == 0 && v1.z == 0 ) return 0.0f;
|
1060
|
+
if (v2.x == 0 && v2.y == 0 && v2.z == 0 ) return 0.0f;
|
1061
|
+
|
1062
|
+
double dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
1063
|
+
double v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
|
1064
|
+
double v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
|
1065
|
+
// This should be a number between -1 and 1, since it's "normalized"
|
1066
|
+
double amt = dot / (v1mag * v2mag);
|
1067
|
+
// But if it's not due to rounding error, then we need to fix it
|
1068
|
+
// http://code.google.com/p/processing/issues/detail?id=340
|
1069
|
+
// Otherwise if outside the range, acos() will return NaN
|
1070
|
+
// http://www.cppreference.com/wiki/c/math/acos
|
1071
|
+
if (amt <= -1) {
|
1072
|
+
return PConstants.PI;
|
1073
|
+
} else if (amt >= 1) {
|
1074
|
+
// http://code.google.com/p/processing/issues/detail?id=435
|
1075
|
+
return 0;
|
1076
|
+
}
|
1077
|
+
return (float) Math.acos(amt);
|
1078
|
+
}
|
1079
|
+
|
1080
|
+
/**
|
1081
|
+
*
|
1082
|
+
* @return
|
1083
|
+
*/
|
1084
|
+
@Override
|
1085
|
+
public String toString() {
|
1086
|
+
return "[ " + x + ", " + y + ", " + z + " ]";
|
1087
|
+
}
|
1088
|
+
|
1089
|
+
|
1090
|
+
/**
|
1091
|
+
* ( begin auto-generated from PVector_array.xml )
|
1092
|
+
*
|
1093
|
+
* Return a representation of this vector as a float array. This is only
|
1094
|
+
* for temporary use. If used in any other fashion, the contents should be
|
1095
|
+
* copied by using the <b>PVector.get()</b> method to copy into your own array.
|
1096
|
+
*
|
1097
|
+
* ( end auto-generated )
|
1098
|
+
*
|
1099
|
+
* @return
|
1100
|
+
* @webref pvector:method
|
1101
|
+
* @usage: web_application
|
1102
|
+
* @brief Return a representation of the vector as a float array
|
1103
|
+
*/
|
1104
|
+
public float[] array() {
|
1105
|
+
if (array == null) {
|
1106
|
+
array = new float[3];
|
1107
|
+
}
|
1108
|
+
array[0] = x;
|
1109
|
+
array[1] = y;
|
1110
|
+
array[2] = z;
|
1111
|
+
return array;
|
1112
|
+
}
|
1113
|
+
|
1114
|
+
/**
|
1115
|
+
*
|
1116
|
+
* @param obj
|
1117
|
+
* @return
|
1118
|
+
*/
|
1119
|
+
@Override
|
1120
|
+
public boolean equals(Object obj) {
|
1121
|
+
if (!(obj instanceof PVector)) {
|
1122
|
+
return false;
|
1123
|
+
}
|
1124
|
+
final PVector p = (PVector) obj;
|
1125
|
+
return x == p.x && y == p.y && z == p.z;
|
1126
|
+
}
|
1127
|
+
|
1128
|
+
/**
|
1129
|
+
*
|
1130
|
+
* @return
|
1131
|
+
*/
|
1132
|
+
@Override
|
1133
|
+
public int hashCode() {
|
1134
|
+
int result = 1;
|
1135
|
+
result = 31 * result + Float.floatToIntBits(x);
|
1136
|
+
result = 31 * result + Float.floatToIntBits(y);
|
1137
|
+
result = 31 * result + Float.floatToIntBits(z);
|
1138
|
+
return result;
|
1139
|
+
}
|
1140
|
+
}
|