package processing.core;
public float x;
public float y;
public float z;
protected float[] array;
}
public PVector(
float x,
float y,
float z) {
this.x = x;
this.y = y;
this.z = z;
}
this.x = x;
this.y = y;
this.z = 0;
}
public void set(
float x,
float y,
float z) {
this.x = x;
this.y = y;
this.z = z;
}
public void set(PVector v) {
x = v.x;
y = v.y;
z = v.z;
}
public void set(
float[] source) {
if (source.length >= 2) {
x = source[0];
y = source[1];
}
if (source.length >= 3) {
z = source[2];
}
}
return new PVector(x, y, z);
}
public float[]
get(
float[] target) {
if (target == null) {
return new float[] { x, y, z };
}
if (target.length >= 2) {
target[0] = x;
target[1] = y;
}
if (target.length >= 3) {
target[2] = z;
}
return target;
}
return (float) Math.sqrt(x*x + y*y + z*z);
}
public void add(PVector v) {
x += v.x;
y += v.y;
z += v.z;
}
public void add(
float x,
float y,
float z) {
this.x += x;
this.y += y;
this.z += z;
}
static public PVector
add(PVector v1, PVector v2) {
return add(v1, v2, null);
}
static public PVector
add(PVector v1, PVector v2, PVector target) {
if (target == null) {
target = new PVector(v1.x + v2.x,v1.y + v2.y, v1.z + v2.z);
} else {
target.set(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
return target;
}
public void sub(PVector v) {
x -= v.x;
y -= v.y;
z -= v.z;
}
public void sub(
float x,
float y,
float z) {
this.x -= x;
this.y -= y;
this.z -= z;
}
static public PVector
sub(PVector v1, PVector v2) {
return sub(v1, v2, null);
}
static public PVector
sub(PVector v1, PVector v2, PVector target) {
if (target == null) {
target = new PVector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
} else {
target.set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
return target;
}
public void mult(
float n) {
x *= n;
y *= n;
z *= n;
}
static public PVector
mult(PVector v,
float n) {
return mult(v, n, null);
}
static public PVector
mult(PVector v,
float n, PVector target) {
if (target == null) {
target = new PVector(v.x*n, v.y*n, v.z*n);
} else {
target.set(v.x*n, v.y*n, v.z*n);
}
return target;
}
public void mult(PVector v) {
x *= v.x;
y *= v.y;
z *= v.z;
}
static public PVector
mult(PVector v1, PVector v2) {
return mult(v1, v2, null);
}
static public PVector
mult(PVector v1, PVector v2, PVector target) {
if (target == null) {
target = new PVector(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
} else {
target.set(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
}
return target;
}
public void div(
float n) {
x /= n;
y /= n;
z /= n;
}
static public PVector
div(PVector v,
float n) {
return div(v, n, null);
}
static public PVector
div(PVector v,
float n, PVector target) {
if (target == null) {
target = new PVector(v.x/n, v.y/n, v.z/n);
} else {
target.set(v.x/n, v.y/n, v.z/n);
}
return target;
}
public void div(PVector v) {
x /= v.x;
y /= v.y;
z /= v.z;
}
static public PVector
div(PVector v1, PVector v2) {
return div(v1, v2, null);
}
static public PVector
div(PVector v1, PVector v2, PVector target) {
if (target == null) {
target = new PVector(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
} else {
target.set(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
}
return target;
}
public float dist(PVector v) {
float dx = x - v.x;
float dy = y - v.y;
float dz = z - v.z;
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
}
static public float dist(PVector v1, PVector v2) {
float dx = v1.x - v2.x;
float dy = v1.y - v2.y;
float dz = v1.z - v2.z;
return (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
}
public float dot(PVector v) {
return x*v.x + y*v.y + z*v.z;
}
public float dot(
float x,
float y,
float z) {
return this.x*x + this.y*y + this.z*z;
}
static public float dot(PVector v1, PVector v2) {
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
public PVector
cross(PVector v) {
return cross(v, null);
}
public PVector
cross(PVector v, PVector target) {
float crossX = y * v.z - v.y * z;
float crossY = z * v.x - v.z * x;
float crossZ = x * v.y - v.x * y;
if (target == null) {
target = new PVector(crossX, crossY, crossZ);
} else {
target.set(crossX, crossY, crossZ);
}
return target;
}
static public PVector
cross(PVector v1, PVector v2, PVector target) {
float crossX = v1.y * v2.z - v2.y * v1.z;
float crossY = v1.z * v2.x - v2.z * v1.x;
float crossZ = v1.x * v2.y - v2.x * v1.y;
if (target == null) {
target = new PVector(crossX, crossY, crossZ);
} else {
target.set(crossX, crossY, crossZ);
}
return target;
}
float m = mag();
if (m != 0 && m != 1) {
div(m);
}
}
if (target == null) {
target = new PVector();
}
float m = mag();
if (m > 0) {
target.set(x/m, y/m, z/m);
} else {
target.set(x, y, z);
}
return target;
}
public void limit(
float max) {
if (mag() > max) {
normalize();
mult(max);
}
}
float angle = (float) Math.atan2(-y, x);
return -1*angle;
}
double dot = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
double v1mag = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
double v2mag = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
return (float) Math.acos(dot / (v1mag * v2mag));
}
return "[ " + x + ", " + y + ", " + z + " ]";
}
if (array == null) {
array = new float[3];
}
array[0] = x;
array[1] = y;
array[2] = z;
return array;
}
}