sync PVector plus the processing.data package (#47 and #42) · codeanticode/processing-android@d39037c · GitHub
Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit d39037c

Browse files
committed
sync PVector plus the processing.data package (processing#47 and processing#42)
1 parent 7619b90 commit d39037c

14 files changed

Lines changed: 806 additions & 388 deletions

core/src/processing/core/PApplet.java

Lines changed: 3 additions & 2 deletions

core/src/processing/core/PVector.java

Lines changed: 35 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,10 @@ public PVector(float x, float y) {
149149
this.z = 0;
150150
}
151151

152-
153152
/**
154153
* ( begin auto-generated from PVector_set.xml )
155154
*
156-
* Sets the x, y, and z component of the vector using three separate
155+
* Sets the x, y, and z component of the vector using two or three separate
157156
* variables, the data from a PVector, or the values from a float array.
158157
*
159158
* ( end auto-generated )
@@ -170,6 +169,17 @@ public void set(float x, float y, float z) {
170169
this.z = z;
171170
}
172171

172+
/**
173+
*
174+
* @webref pvector:method
175+
* @param x the x component of the vector
176+
* @param y the y component of the vector
177+
* @brief Set the x, y components of the vector
178+
*/
179+
public void set(float x, float y) {
180+
this.x = x;
181+
this.y = y;
182+
}
173183

174184
/**
175185
* @param v any variable of type PVector
@@ -207,6 +217,7 @@ public void set(float[] source) {
207217
* @usage web_application
208218
* @return the random PVector
209219
* @brief Make a new 2D unit vector with a random direction.
220+
* @see PVector#random3D()
210221
*/
211222
static public PVector random2D() {
212223
return random2D(null,null);
@@ -233,8 +244,6 @@ static public PVector random2D(PVector target) {
233244

234245
/**
235246
* Make a new 2D unit vector with a random direction
236-
* @param parent current PApplet instance
237-
* @param target the target vector (if null, a new vector will be created)
238247
* @return the random PVector
239248
*/
240249
static public PVector random2D(PVector target, PApplet parent) {
@@ -253,6 +262,7 @@ static public PVector random2D(PVector target, PApplet parent) {
253262
* @usage web_application
254263
* @return the random PVector
255264
* @brief Make a new 3D unit vector with a random direction.
265+
* @see PVector#random2D()
256266
*/
257267
static public PVector random3D() {
258268
return random3D(null,null);
@@ -279,8 +289,6 @@ static public PVector random3D(PVector target) {
279289

280290
/**
281291
* Make a new 3D unit vector with a random direction
282-
* @param target the target vector (if null, a new vector will be created)
283-
* @param parent current PApplet instance
284292
* @return the random PVector
285293
*/
286294
static public PVector random3D(PVector target, PApplet parent) {
@@ -324,7 +332,7 @@ static public PVector fromAngle(float angle) {
324332

325333
/**
326334
* Make a new 2D unit vector from an angle
327-
* @param angle the angle
335+
*
328336
* @param target the target vector (if null, a new vector will be created)
329337
* @return the PVector
330338
*/
@@ -382,6 +390,7 @@ public float[] get(float[] target) {
382390
* @usage web_application
383391
* @brief Calculate the magnitude of the vector
384392
* @return magnitude (length) of the vector
393+
* @see PVector#magSq()
385394
*/
386395
public float mag() {
387396
return (float) Math.sqrt(x*x + y*y + z*z);
@@ -399,9 +408,9 @@ public float mag() {
399408
*
400409
* @webref pvector:method
401410
* @usage web_application
402-
* @brief Calculate the magnitude of the vector
411+
* @brief Calculate the magnitude of the vector, squared
403412
* @return squared magnitude of the vector
404-
*
413+
* @see PVector#mag()
405414
*/
406415
public float magSq() {
407416
return (x*x + y*y + z*z);
@@ -512,7 +521,7 @@ static public PVector sub(PVector v1, PVector v2) {
512521
* Subtract one vector from another and store in another vector
513522
* @param v1 the x, y, and z components of a PVector object
514523
* @param v2 the x, y, and z components of a PVector object
515-
* @param target PVector to store the result
524+
* @param target PVector in which to store the result
516525
*/
517526
static public PVector sub(PVector v1, PVector v2, PVector target) {
518527
if (target == null) {
@@ -533,8 +542,8 @@ static public PVector sub(PVector v1, PVector v2, PVector target) {
533542
*
534543
* @webref pvector:method
535544
* @usage web_application
545+
* @brief Multiply a vector by a scalar
536546
* @param n the number to multiply with the vector
537-
* @brief Multiply a vector by a scalar or one vector by another
538547
*/
539548
public void mult(float n) {
540549
x *= n;
@@ -553,7 +562,7 @@ static public PVector mult(PVector v, float n) {
553562

554563
/**
555564
* Multiply a vector by a scalar, and write the result into a target PVector.
556-
* @param target PVector to store the result
565+
* @param target PVector in which to store the result
557566
*/
558567
static public PVector mult(PVector v, float n, PVector target) {
559568
if (target == null) {
@@ -564,30 +573,6 @@ static public PVector mult(PVector v, float n, PVector target) {
564573
return target;
565574
}
566575

567-
public void mult(PVector v) {
568-
x *= v.x;
569-
y *= v.y;
570-
z *= v.z;
571-
}
572-
573-
574-
/**
575-
* @param v1 the x, y, and z components of a PVector
576-
* @param v2 the x, y, and z components of a PVector
577-
*/
578-
static public PVector mult(PVector v1, PVector v2) {
579-
return mult(v1, v2, null);
580-
}
581-
582-
583-
static public PVector mult(PVector v1, PVector v2, PVector target) {
584-
if (target == null) {
585-
target = new PVector(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
586-
} else {
587-
target.set(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
588-
}
589-
return target;
590-
}
591576

592577

593578
/**
@@ -599,8 +584,8 @@ static public PVector mult(PVector v1, PVector v2, PVector target) {
599584
*
600585
* @webref pvector:method
601586
* @usage web_application
602-
* @param n the value to divide by
603-
* @brief Divide a vector by a scalar or one vector by another
587+
* @brief Divide a vector by a scalar
588+
* @param n the number by which to divide the vector
604589
*/
605590
public void div(float n) {
606591
x /= n;
@@ -611,8 +596,7 @@ public void div(float n) {
611596

612597
/**
613598
* Divide a vector by a scalar and return the result in a new vector.
614-
* @param v any variable of type PVector
615-
* @param n the number to divide with the vector
599+
* @param v the vector to divide by the scalar
616600
* @return a new vector that is v1 / n
617601
*/
618602
static public PVector div(PVector v, float n) {
@@ -621,9 +605,7 @@ static public PVector div(PVector v, float n) {
621605

622606
/**
623607
* Divide a vector by a scalar and store the result in another vector.
624-
* @param v any variable of type PVector
625-
* @param n the number to divide with the vector
626-
* @param target PVector to store the result
608+
* @param target PVector in which to store the result
627609
*/
628610
static public PVector div(PVector v, float n, PVector target) {
629611
if (target == null) {
@@ -635,34 +617,6 @@ static public PVector div(PVector v, float n, PVector target) {
635617
}
636618

637619

638-
/**
639-
* Divide each element of one vector by the elements of another vector.
640-
*/
641-
public void div(PVector v) {
642-
x /= v.x;
643-
y /= v.y;
644-
z /= v.z;
645-
}
646-
647-
648-
/**
649-
* Divide each element of one vector by the individual elements of another
650-
* vector, and return the result as a new PVector.
651-
*/
652-
static public PVector div(PVector v1, PVector v2) {
653-
return div(v1, v2, null);
654-
}
655-
656-
static public PVector div(PVector v1, PVector v2, PVector target) {
657-
if (target == null) {
658-
target = new PVector(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
659-
} else {
660-
target.set(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
661-
}
662-
return target;
663-
}
664-
665-
666620
/**
667621
* ( begin auto-generated from PVector_dist.xml )
668622
*
@@ -880,7 +834,7 @@ public PVector setMag(PVector target, float len) {
880834
* @webref pvector:method
881835
* @usage web_application
882836
* @return the angle of rotation
883-
* @brief SCalculate the angle of rotation for this vector
837+
* @brief Calculate the angle of rotation for this vector
884838
*/
885839
public float heading() {
886840
float angle = (float) Math.atan2(-y, x);
@@ -925,8 +879,7 @@ public void rotate(float theta) {
925879
* @usage web_application
926880
* @brief Linear interpolate the vector to another vector
927881
* @param v the vector to lerp to
928-
* @param amt The amt parameter is the amount to interpolate between the two vectors where 1.0 equal to the new vector
929-
* 0.1 is very near the new vector, 0.5 is half-way in between.
882+
* @param amt The amount of interpolation; some value between 0.0 (old vector) and 1.0 (new vector). 0.1 is very near the new vector. 0.5 is halfway in between.
930883
*/
931884
public void lerp(PVector v, float amt) {
932885
x = PApplet.lerp(x,v.x,amt);
@@ -936,11 +889,8 @@ public void lerp(PVector v, float amt) {
936889

937890
/**
938891
* Linear interpolate between two vectors (returns a new PVector object)
939-
* @param v1 the vector
892+
* @param v1 the vector to start from
940893
* @param v2 the vector to lerp to
941-
* @param amt The amt parameter is the amount to interpolate between the two vectors where 1.0 equal to the new vector
942-
* 0.1 is very near the new vector, 0.5 is half-way in between.
943-
* @return the resulting lerped PVector
944894
*/
945895
public static PVector lerp(PVector v1, PVector v2, float amt) {
946896
PVector v = v1.get();
@@ -953,8 +903,6 @@ public static PVector lerp(PVector v1, PVector v2, float amt) {
953903
* @param x the x component to lerp to
954904
* @param y the y component to lerp to
955905
* @param z the z component to lerp to
956-
* @param amt The amt parameter is the amount to interpolate between the two vectors where 1.0 equal to the new vector
957-
* 0.1 is very near the new vector, 0.5 is half-way in between.
958906
*/
959907
public void lerp(float x, float y, float z, float amt) {
960908
this.x = PApplet.lerp(this.x,x,amt);
@@ -976,6 +924,12 @@ public void lerp(float x, float y, float z, float amt) {
976924
* @brief Calculate and return the angle between two vectors
977925
*/
978926
static public float angleBetween(PVector v1, PVector v2) {
927+
928+
// We get NaN if we pass in a zero vector which can cause problems
929+
// Zero seems like a reasonable angle between a (0,0) vector and something else
930+
if (v1.x == 0 && v1.y == 0) return 0.0f;
931+
if (v2.x == 0 && v2.y == 0) return 0.0f;
932+
979933
double dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
980934
double v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
981935
double v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
@@ -1040,4 +994,4 @@ public int hashCode() {
1040994
result = 31 * result + Float.floatToIntBits(z);
1041995
return result;
1042996
}
1043-
}
997+
}

core/src/processing/data/FloatDict.java

Lines changed: 32 additions & 1 deletion

0 commit comments

Comments
 (0)