* ( begin auto-generated from PVector_normalize.xml )
*
* Normalize the vector to length 1 (make it a unit vector).
*
* ( end auto-generated )
*
* @webref pvector:method
* @usage web_application
* @brief Normalize the vector to a length of 1
*/
publicPVectornormalize() {
floatm = mag();
if (m != 0 && m != 1) {
div(m);
}
returnthis;
}
/**
* @param target Set to null to create a new vector
* @return a new vector (if target was null), or target
*/
publicPVectornormalize(PVectortarget) {
if (target == null) {
target = newPVector();
}
floatm = mag();
if (m > 0) {
target.set(x/m, y/m, z/m);
} else {
target.set(x, y, z);
}
returntarget;
}
/**
* ( begin auto-generated from PVector_limit.xml )
*
* Limit the magnitude of this vector to the value used for the <b>max</b> parameter.
*
* ( end auto-generated )
*
* @webref pvector:method
* @usage web_application
* @param max the maximum magnitude for the vector
* @brief Limit the magnitude of the vector
*/
publicPVectorlimit(floatmax) {
if (magSq() > max*max) {
normalize();
mult(max);
}
returnthis;
}
/**
* ( begin auto-generated from PVector_setMag.xml )
*
* Set the magnitude of this vector to the value used for the <b>len</b> parameter.
*
* ( end auto-generated )
*
* @webref pvector:method
* @usage web_application
* @param len the new length for this vector
* @brief Set the magnitude of the vector
*/
publicPVectorsetMag(floatlen) {
normalize();
mult(len);
returnthis;
}
/**
* Sets the magnitude of this vector, storing the result in another vector.
* @param target Set to null to create a new vector
* @param len the new length for the new vector
* @return a new vector (if target was null), or target
*/
publicPVectorsetMag(PVectortarget, floatlen) {
target = normalize(target);
target.mult(len);
returntarget;
}
/**
* ( begin auto-generated from PVector_setMag.xml )
*
* Calculate the angle of rotation for this vector (only 2D vectors)
*
* ( end auto-generated )
*
* @webref pvector:method
* @usage web_application
* @return the angle of rotation
* @brief Calculate the angle of rotation for this vector
*/
publicfloatheading() {
floatangle = (float) Math.atan2(y, x);
returnangle;
}
@Deprecated
publicfloatheading2D() {
returnheading();
}
/**
* ( begin auto-generated from PVector_rotate.xml )
*
* Rotate the vector by an angle (only 2D vectors), magnitude remains the same
*
* ( end auto-generated )
*
* @webref pvector:method
* @usage web_application
* @brief Rotate the vector by an angle (2D only)
* @param theta the angle of rotation
*/
publicPVectorrotate(floattheta) {
floattemp = x;
// Might need to check for rounding errors like with angleBetween function?
x = x*PApplet.cos(theta) - y*PApplet.sin(theta);
y = temp*PApplet.sin(theta) + y*PApplet.cos(theta);
returnthis;
}
/**
* ( begin auto-generated from PVector_rotate.xml )
*
* Linear interpolate the vector to another vector
*
* ( end auto-generated )
*
* @webref pvector:method
* @usage web_application
* @brief Linear interpolate the vector to another vector
* @param v the vector to lerp to
* @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.
* @see PApplet#lerp(float, float, float)
*/
publicPVectorlerp(PVectorv, floatamt) {
x = PApplet.lerp(x, v.x, amt);
y = PApplet.lerp(y, v.y, amt);
z = PApplet.lerp(z, v.z, amt);
returnthis;
}
/**
* Linear interpolate between two vectors (returns a new PVector object)