/* float x, y, z,l; vector3D(vector3D v1){ //copy of a vector vector3D(float xx, float yy, float zz){ //vector from scratch = void assign(float xx, float yy, float zz){ // reassign existing vector void assign(vector3D v1){ // reassign existing vector || float length(){ //length of vector float sqlength(){ // squared length of vector void norm(){ //normalize vector static vector3D unit(vector3D v1){//return unit vector along v1 vector3D unit(){// return unit vector along this vector + void add(vector3D v1){ //add v1 to this vector static vector3D add(vector3D v1, vector3D v2){ //return vector v1+v2 - void sub(vector3D v1){ //subtract v1 from this vector static vector3D sub(vector3D v1, vector3D v2){ //return vector v1-v2 * void mul(float s){ //multiply this vector by s static vector3D mul(float s, vector3D v1){ //return vector s*v1 / void div(float s){ // divide this vector by s static vector3D div(vector3D v1,float s){ //return vector v1/s x static vector3D cross(vector3D v1, vector3D v2){//return crossproduct v1 x v2 void cross(vector3D v1){// make crossproduct of this vector and v1 . static float dot(vector3D v1, vector3D v2){ //return v1.v2 float dot(vector3D v1){ //return dot product of this vector and v1 static vector3D onto(vector3D v1, vector3D v2){//return v1 projected onto v2 void onto(vector3D v1){//project this vector on v1 static vector3D pointto(vector3D v1, vector3D v2){//return v1 rotated to v2 void pointto(vector3D v1){//rotate this vector to v1 static vector3D rotate(vector3D v1, vector3D v2, float angle){ static float cosAngle(vector3D v1, vector3D v2){// cosinus of angle between v1 and v2 float cosAngle(vector3D v1){// cosinus of angle between this vector and v1 static float angle(vector3D v1, vector3D v2){// angle between v1 and v2 float angle(vector3D v1){// angle between this vector and v1 static float dist2(vector3D v1, vector3D v2){// squared distance between v1 and v2 float dist2(vector3D v1){// squared distance between this vector and v1 static float dist(vector3D v1, vector3D v2){// distance between v1 and v2 float dist(vector3D v1){// distance between this vector and v1 void mirrorXY(){//mirror through XY plane static vector3D mirrorXY(vector3D v1){//mirror through XY plane void mirrorYZ(){//mirror through YZ plane static vector3D mirrorYZ(vector3D v1){//mirror through YZ plane void mirrorXZ(){//mirror through XZ plane static vector3D mirrorXZ(vector3D v1){//mirror through XZ plane void mirrorX(){//mirror around X axis static vector3D mirrorX(vector3D v1){//mirror around X axis void mirrorY(){//mirror around Y axis static vector3D mirrorY(vector3D v1){//mirror around Y axis void mirrorZ(){//mirror around Z axis static vector3D mirrorZ(vector3D v1){//mirror around Z axis void flip(){//reverse vector static vector3D flip(vector3D v1){//return reverse vector void projectXY(float zz){// project on XY plane with Z coordinate zz static vector3D projectXY(vector3D v1, float zz){// project on XY plane with Z coordinate zz void projectYZ(float xx){// project on YZ plane with X coordinate xx static vector3D projecYZ(vector3D v1, float xx){// project on YZ plane with X coordinate xx void projectXZ(float yy){// project on XZ plane with Y coordinate yy static vector3D projectXZ(vector3D v1, float yy){// project on XZ plane with Y coordinate yy void projectX(float yy,float zz){// project on X axis with coordinates yy and zz static vector3D projectX(vector3D v1, float yy,float zz){// project on X axis with coordinates yy and zz void projectY(float xx,float zz){// project on Y axis with coordinates xx and zz static vector3D projectY(vector3D v1, float xx,float zz){// project on Y axis with coordinates xx and zz void projectZ(float xx,float yy){// project on Z axis with coordinates xx and yy static vector3D projectZ(vector3D v1, float xx,float yy){// project on Z axis with coordinates xx and yy String output(){ */ static class vector3D{// basic 3d vector mathematics class float x, y, z,l; vector3D(){ } vector3D(vector3D v1){ //copy of a vector x = v1.x; y = v1.y; z = v1.z; l=length(); } vector3D(float xx, float yy, float zz){ //vector from scratch x = xx; y = yy; z = zz; l=length(); } void assign(float xx, float yy, float zz){ // reassign existing vector x = xx; y = yy; z = zz; l=length(); } void assign(vector3D v1){ // reassign existing vector x = v1.x; y = v1.y; z = v1.z; l=length(); } float length(){ //length of vector return ((float)Math.sqrt(x*x + y*y + z*z)); } float sqlength(){ // squared length of vector return (x * x + y * y + z * z); } void norm(){ //normalize vector if (l!=0.0){ x /= l; y /= l; z /= l; l = 1.0;} } static vector3D unit(vector3D v1){//return unit vector along v1 vector3D result =new vector3D(v1); result.norm(); return result; } vector3D unit(){// return unit vector along this vector vector3D result =new vector3D(this); result.norm(); return result; } void add(vector3D v1){ //add v1 to this vector x += v1.x; y += v1.y; z += v1.z; l=length(); } static vector3D add(vector3D v1, vector3D v2){ //return vector v1+v2 vector3D result =new vector3D(v1); result.add(v2); return result; } void sub(vector3D v1){ //subtract v1 from this vector x -= v1.x; y -= v1.y; z -= v1.z; l=length(); } static vector3D sub(vector3D v1, vector3D v2){ //return vector v1-v2 vector3D result =new vector3D(v1); result.sub(v2); return result; } void mul(float s){ //multiply this vector by s x *= s; y *= s; z *= s; l *= s; } static vector3D mul(float s, vector3D v1){ //return vector s*v1 vector3D result =new vector3D(v1); result.mul(s); return result; } void div(float s){ // divide this vector by s x /= s; y /= s; z /= s; l /= s; } static vector3D div(vector3D v1,float s){ //return vector v1/s vector3D result =new vector3D(v1); result.div(s); return result; } static vector3D cross(vector3D v1, vector3D v2){//return crossproduct v1 x v2 vector3D result = new vector3D(0,0,0); result.x = v1.y * v2.z - v1.z * v2.y; result.y = - v1.x * v2.z+ v1.z * v2.x ; result.z = v1.x * v2.y - v1.y * v2.x; result.l=result.length(); return result; } void cross(vector3D v1){// make crossproduct of this vector and v1 vector3D result = new vector3D(0,0,0); result.x = y * v1.z - z *v1.y; result.y = - x * v1.z+ z * v1.x ; result.z = x * v1.y - y * v1.x; assign(result); } static float dot(vector3D v1, vector3D v2){ //return v1.v2 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } float dot(vector3D v1){ //return dot product of this vector and v1 return v1.x * x + v1.y * y + v1.z * z; } static vector3D onto(vector3D v1, vector3D v2){//return v1 projected onto v2 vector3D result=new vector3D(mul(cosAngle(v1,v2),v1)); result.assign(mul(result.l, unit(v2))); return result; } void onto(vector3D v1){//project this vector on v1 mul(cosAngle(v1)); assign(mul(l, unit(v1))); } static vector3D pointto(vector3D v1, vector3D v2){//return v1 rotated to v2 return (mul(v1.length(),unit(v2))); } void pointto(vector3D v1){//rotate this vector to v1 float ll=l; norm(); assign(mul(ll, unit(v1))); } static vector3D rotate(vector3D v1, vector3D v2, float angle){ vector3D axis=new vector3D(unit(v2)); vector3D result=new vector3D(mul((float)(1.0-Math.cos(angle)),mul(dot(v1,axis),axis))); result.add(mul((float)Math.cos(angle),v1)); result.add(mul((float)Math.sin(angle),cross(axis,v1))); return result; } static float cosAngle(vector3D v1, vector3D v2){// cosinus of angle between v1 and v2 return dot(v1,v2)/(v1.l*v2.l); } float cosAngle(vector3D v1){// cosinus of angle between this vector and v1 return dot(v1)/(l*v1.l); } static float angle(vector3D v1, vector3D v2){// angle between v1 and v2 return (float) Math.acos(cosAngle(v1,v2)); } float angle(vector3D v1){// angle between this vector and v1 return (float) Math.acos(cosAngle(v1)); } static float dist2(vector3D v1, vector3D v2){// squared distance between v1 and v2 return (v1.x-v2.x)*(v1.x-v2.x)+(v1.y-v2.y)*(v1.y-v2.y)+(v1.z-v2.z)*(v1.z-v2.z); } float dist2(vector3D v1){// squared distance between this vector and v1 return (x-v1.x)*(x-v1.x)+(y-v1.y)*(y-v1.y)+(z-v1.z)*(z-v1.z); } static float dist(vector3D v1, vector3D v2){// distance between v1 and v2 return ((float) Math.sqrt(dist2(v1,v2))); } float dist(vector3D v1){// distance between this vector and v1 return ((float) Math.sqrt(dist2(v1))); } void mirrorXY(){//mirror through XY plane z*=-1.0; } static vector3D mirrorXY(vector3D v1){//mirror through XY plane vector3D result = new vector3D(v1); result.z*=-1.0; return result; } void mirrorYZ(){//mirror through YZ plane x*=-1.0; } static vector3D mirrorYZ(vector3D v1){//mirror through YZ plane vector3D result = new vector3D(v1); result.x*=-1.0; return result; } void mirrorXZ(){//mirror through XZ plane y*=-1.0; } static vector3D mirrorXZ(vector3D v1){//mirror through XZ plane vector3D result = new vector3D(v1); result.y*=-1.0; return result; } void mirrorX(){//mirror around X axis y*=-1.0; z*=-1.0; } static vector3D mirrorX(vector3D v1){//mirror around X axis vector3D result = new vector3D(v1); result.y*=-1.0; result.z*=-1.0; return result; } void mirrorY(){//mirror around Y axis x*=-1.0; z*=-1.0; } static vector3D mirrorY(vector3D v1){//mirror around Y axis vector3D result = new vector3D(v1); result.x*=-1.0; result.z*=-1.0; return result; } void mirrorZ(){//mirror around Z axis x*=-1.0; y*=-1.0; } static vector3D mirrorZ(vector3D v1){//mirror around Z axis vector3D result = new vector3D(v1); result.x*=-1.0; result.y*=-1.0; return result; } void flip(){//reverse vector x*=-1.0; y*=-1.0; z*=-1.0; } static vector3D flip(vector3D v1){//return reverse vector vector3D result = new vector3D(v1); result.x*=-1.0; result.y*=-1.0; result.z*=-1.0; return result; } void projectXY(float zz){// project on XY plane with Z coordinate zz z=zz; l=length(); } static vector3D projectXY(vector3D v1, float zz){// project on XY plane with Z coordinate zz vector3D result = new vector3D(v1); result.z=zz; result.l=result.length(); return result; } void projectYZ(float xx){// project on YZ plane with X coordinate xx x=xx; l=length(); } static vector3D projecYZ(vector3D v1, float xx){// project on YZ plane with X coordinate xx vector3D result = new vector3D(v1); result.x=xx; result.l=result.length(); return result; } void projectXZ(float yy){// project on XZ plane with Y coordinate yy y=yy; l=length(); } static vector3D projectXZ(vector3D v1, float yy){// project on XZ plane with Y coordinate yy vector3D result = new vector3D(v1); result.y=yy; result.l=result.length(); return result; } void projectX(float yy,float zz){// project on X axis with coordinates yy and zz y=yy; z=zz; l=length(); } static vector3D projectX(vector3D v1, float yy,float zz){// project on X axis with coordinates yy and zz vector3D result = new vector3D(v1); result.y=yy; result.z=zz; result.l=result.length(); return result; } void projectY(float xx,float zz){// project on Y axis with coordinates xx and zz x=xx; z=zz; l=length(); } static vector3D projectY(vector3D v1, float xx,float zz){// project on Y axis with coordinates xx and zz vector3D result = new vector3D(v1); result.x=xx; result.z=zz; result.l=result.length(); return result; } void projectZ(float xx,float yy){// project on Z axis with coordinates xx and yy x=xx; y=yy; l=length(); } static vector3D projectZ(vector3D v1, float xx,float yy){// project on Z axis with coordinates xx and yy vector3D result = new vector3D(v1); result.x=xx; result.y=yy; result.l=result.length(); return result; } String output(){ return( "x: " + x +" y: " + y +" z: " + z); } }