Guest User

Vector

a guest
Jul 4th, 2015
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. public class Vec {
  2.         public final static Vec X_AXIS=new Vec(1,0,0);
  3.         public final static Vec Y_AXIS=new Vec(0,1,0);
  4.         public final static Vec Z_AXIS=new Vec(0,0,1);
  5.         public final static Vec ORIGIN=new Vec(0,0,0);
  6.  
  7.         public final double x,y,z;
  8.        
  9.         public Vec(double x, double y, double z) {
  10.             this.x=x;
  11.             this.y=y;
  12.             this.z=z;
  13.         }
  14.        
  15.         public double dotProduct(Vec v) {
  16.             return x*v.x+y*v.y+z*v.z;
  17.         }
  18.        
  19.         public Vec crossProduct(Vec v) {
  20.             double rx=y*v.z-z*v.y;
  21.             double ry=z*v.x-x*v.z;
  22.             double rz=x*v.y-y*v.x;
  23.             return new Vec(rx, ry, rz);
  24.         }
  25.        
  26.         public Vec newLength(double newLength) {
  27.             double length=getLength();
  28.             if(length==newLength) return this;
  29.             if(length==0) return X_AXIS.newLength(newLength);
  30.             return new Vec(x*newLength/length, y*newLength/length, z*newLength/length);
  31.         }
  32.        
  33.        
  34.         public Vec rotationAxis() {
  35.             return rotationAxis(X_AXIS);
  36.         }
  37.        
  38.        
  39.         //The rotation axis to rotate v onto this
  40.         public Vec rotationAxis(Vec v) {
  41.             return normalizedCrossProduct(v);
  42.         }
  43.        
  44.         public Vec normalizedCrossProduct(Vec v) {
  45.             Vec r=crossProduct(v);
  46.             if(r.getLength()<0.0001) {
  47.                 r=crossProduct(X_AXIS);
  48.             }
  49.             if(r.getLength()<0.0001) {
  50.                 r=crossProduct(Y_AXIS);
  51.             }
  52.             if(r.getLength()<0.0001) {
  53.                 return X_AXIS;
  54.             }
  55.             return r.normalize();
  56.         }
  57.        
  58.         public double angle() {
  59.             return angleTo(X_AXIS);
  60.         }
  61.        
  62.         public double angleTo(Vec v) {
  63.             double cosTheta=dotProduct(v)/(getLength()*v.getLength());
  64.            
  65.             return Math.acos(cosTheta);
  66.         }
  67.        
  68.         public double getLength() {
  69.             return Math.sqrt(x*x+y*y+z*z);
  70.         }        
  71.        
  72.        
  73.         public Vec rotate(Matrix m) {
  74.             return m.mul(this);
  75.         }
  76.        
  77.         public Vec normalize() {
  78.             double length=getLength();
  79.            
  80.             if(length==1) return this;
  81.             if(length==0) return X_AXIS;
  82.             return new Vec(x/length, y/length, z/length);
  83.         }
  84.        
  85.         public Vec addX(double a) {
  86.             return new Vec(x+a, y, z);
  87.         }
  88.        
  89.         public Vec addY(double a) {
  90.             return new Vec(x, y+a, z);
  91.         }
  92.        
  93.         public Vec addZ(double a) {
  94.             return new Vec(x, y, z+a);
  95.         }
  96.        
  97.         public Vec add(Vec v) {
  98.             return new Vec(x+v.x, y+v.y, z+v.z);
  99.         }
  100.                
  101.         public Vec sub(Vec v) {
  102.             return new Vec(x-v.x, y-v.y, z-v.z);
  103.         }
  104.        
  105.         public Vec mul(double m) {
  106.             return new Vec(x*m, y*m, z*m);
  107.         }
  108.        
  109.         public Vec div(double d) {
  110.             return new Vec(x/d, y/d, z/d);
  111.         }
  112.        
  113.         public Vec neg() {
  114.             return new Vec(-x, -y, -z);
  115.         }
  116.        
  117.         public Vec translate(double x, double y, double z) {
  118.             return new Vec(this.x-x, this.y-y, this.z-z);
  119.         }
  120.        
  121.         public Vec translate(Vec v) {
  122.             return sub(v);
  123.         }
  124.        
  125.         public Vec translateBack(Vec v) {
  126.             return add(v);
  127.         }
  128.        
  129.         @Override
  130.         public boolean equals(Object o) {
  131.             Vec v=(Vec)o;
  132.             return v==this || this.sub(v).getLength() == 0;
  133.         }
  134.        
  135.         public String toString() {
  136.         return String.format(java.util.Locale.ENGLISH,
  137.             "(%.3f, %.3f, %.3f)", x, y, z);
  138.         }
  139.        
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment