class vector { float x,y,z; vector(float x_, float y_, float z_) { x = x_; y = y_; z = z_; } // element arithmetic vector plus(float f) {return new vector(x+f, y+f, z+f);} vector minus(float f) {return new vector(x-f, y-f, z-f);} vector times(float f) {return new vector(x*f, y*f, z*f);} vector dividedBy(float f) {return new vector(x/f, y/f, z/f);} vector plus(vector V) {return new vector(x+V.x, y+V.y, z+V.z);} vector minus(vector V) {return new vector(x-V.x, y-V.y, z-V.z);} vector times(vector V) {return new vector(x*V.x, y*V.y, z*V.z);} vector dividedBy(vector V) {return new vector(x/V.x, y/V.y, z/V.z);} // other math functions vector normalize() { if (magnitude()>0) { return this.dividedBy(magnitude()); } else { return new vector(0,0,0); // as opposed to nans } } float magnitude() { return sqrt(sq(x)+sq(y)+sq(z)); } float distanceTo(vector V) { return dist(x,y,z,V.x,V.y,V.z); } vector directionTo(vector V) { vector R = V.minus(this); return R.normalize(); } float angleWith(vector V) { float costheta = this.normalize().dot(V.normalize()); return acos(constrain(costheta,-1,1)); } float dot(vector V) { return x*V.x + y*V.y + z*V.z; } void readout() { println(" "+x+" "+y+" "+z); } }