//FVector.pde by David Huebner (2005|06|05) //last changes (2005|06|21) //modify and use in any way //Vector class with N float elements. Built for Processing 0.90. //To get it working in Java you need the core.jar library from processing or delete/change the methods using matrices. //This vector class ist not limited to a 3D vector, although some of the methods only work with 3 dimensions. //Most methods have a second variant. //The normal one like multiply() returns a new vector with the calculations applied to. //The one with "Me" in its name like multiplyMe() applies the calculations to this vector. //Please report any bugs and additions to David@millbridge.de import processing.core.*; // for class PMatrix public class FVector { public static final float TOL = 0.00001f; private int N; //the number of elements of this vector public float e[]; //the array which contains all the elements of this vector //constructor for creating a null-vector with n elements public FVector(int n) { N = n; e = new float[N]; for( int i=0; i 2) { double cosval = Math.cos(val); double sinval = Math.sin(val); double tmp1 = e[1]*cosval - e[2]*sinval; double tmp2 = e[1]*sinval + e[2]*cosval; result.e[1] = (float)tmp1; result.e[2] = (float)tmp2; } else System.err.println("FVector.rotateX(): FVector is not in R3 or higher"); return result; } //rotates this vector around the x-axis by a given float value val (in radians) public void rotateMeX(float val) { if (N > 2) { double cosval = Math.cos(val); double sinval = Math.sin(val); double tmp1 = e[1]*cosval - e[2]*sinval; double tmp2 = e[1]*sinval + e[2]*cosval; e[1] = (float)tmp1; e[2] = (float)tmp2; } else System.err.println("FVector.rotateMeX(): FVector is not in R3 or higher"); } //returns a new vector as a result of rotating this vector around the y-axis by a given float value val (in radians) public FVector rotateY(float val) { FVector result = new FVector(this); if (N > 2) { double cosval = Math.cos(val); double sinval = Math.sin(val); double tmp1 = e[0]*cosval - e[2]*sinval; double tmp2 = e[0]*sinval + e[2]*cosval; result.e[0] = (float)tmp1; result.e[2] = (float)tmp2; } else System.err.println("FVector.rotateY(): FVector is not in R3 or higher"); return result; } //rotates this vector around the y-axis by a given float value val (in radians) public void rotateMeY(float val) { if (N > 2) { double cosval = Math.cos(val); double sinval = Math.sin(val); double tmp1 = e[0]*cosval - e[2]*sinval; double tmp2 = e[0]*sinval + e[2]*cosval; e[0] = (float)tmp1; e[2] = (float)tmp2; } else System.err.println("FVector.rotateMeY(): FVector is not in R3 or higher"); } //returns a new vector as a result of rotating this vector around the z-axis by a given float value val (in radians) //can be used for 2D Vectors too public FVector rotateZ(float val) { FVector result = new FVector(this); if (N > 1) { double cosval = Math.cos(val); double sinval = Math.sin(val); double tmp1 = e[0]*cosval - e[1]*sinval; double tmp2 = e[0]*sinval + e[1]*cosval; result.e[0] = (float)tmp1; result.e[1] = (float)tmp2; } else System.err.println("FVector.rotateMeZ(): FVector is not in R2 or higher"); return result; } //rotates this vector around the z-axis by a given float value val (in radians) //can be used for 2D Vectors too public void rotateMeZ(float val) { if (N > 1) { double cosval = Math.cos(val); double sinval = Math.sin(val); double tmp1 = e[0]*cosval - e[1]*sinval; double tmp2 = e[0]*sinval + e[1]*cosval; e[0] = (float)tmp1; e[1] = (float)tmp2; } else System.err.println("FVector.rotateMeZ(): FVector is not in R2 or higher"); } //rotates this vector around a given axis v2 by a given float value val (in radiens) public FVector rotateAxis(float val, FVector v2) { FVector result = new FVector(this); if (N == 3 && v2.N == 3) { PMatrix rotateMatrix = new PMatrix(); rotateMatrix.rotate(val, v2.e[0], v2.e[1], v2.e[2]); float in[] = {e[0], e[1], e[2], 0}; float out[] = {0, 0, 0, 0}; rotateMatrix.mult(in,out); result.e[0] = out[0]; result.e[1] = out[1]; result.e[2] = out[2]; } else System.err.println("FVector.rotateAxis(): FVectors are not both in R3"); return result; } //returns a new vector as a result of rotating this vector around a given axis v2 by a given float value val (in radiens) public void rotateAxisMe(float val, FVector v2) { if (N == 3 && v2.N == 3) { PMatrix rotateMatrix = new PMatrix(); rotateMatrix.rotate(val, v2.e[0], v2.e[1], v2.e[2]); float in[] = {e[0], e[1], e[2], 0}; float out[] = {0, 0, 0, 0}; rotateMatrix.mult(in,out); e[0] = out[0]; e[1] = out[1]; e[2] = out[2]; } else System.err.println("FVector.rotateAxisMe(): FVectors are not both in R3"); } //returns the value of the 1st element of this vector public float getX() { return e[0]; } //returns the value of the 2nd element of this vector public float getY() { return e[1]; } //returns the value of the 3rd element of this vector public float getZ() { return e[2]; } //sets the 1st element to a given value newX public void setX(float newX) { e[0] = newX; } //sets the 2nd element to a given value newY public void setY(float newY) { e[1] = newY; } //sets the 3rd element to a given value newZ public void setZ(float newZ) { e[2] = newZ; } //changes the elements of this vector to the values of another vector public void set(FVector v2) { for ( int i=0; i 2) { e[0] = x; e[1] = y; e[2] = z; } else System.err.println("FVector.set(): FVector's dimension is too small"); } //changes the elements of this vector to the values of a given float array public void set(float x[]) { for ( int i=0; i