Advertisement
Guest User

Vector2.class

a guest
Oct 8th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. public class Vector2
  2. {
  3.     private double x, y, length;
  4.        
  5.     /**
  6.      * @return A new Vector2
  7.      * @param x = HorizontalLength; y = Vertical Length
  8.      */
  9.     public Vector2(double x, double y)
  10.     {
  11.         this.x = x;
  12.         this.y = y;
  13.         this.length = length(this);
  14.     }
  15.    
  16.     public double getX(){ return x;}
  17.     public double getY(){ return y;}
  18.     public double getLength(){ return length;}
  19.    
  20.     public void setX(double x){ this.x = x;}
  21.     public void setY(double y){ this.y = y;}
  22.     public void setLength(double length){ this.length = length;}
  23.    
  24.     /**
  25.      * @return The sum of both Vector2
  26.      */
  27.     public Vector2 add(Vector2 vec1, Vector2 vec2)
  28.     {
  29.         Vector2 sum = new Vector2((vec1.x + vec2.x), (vec1.y + vec2.y));
  30.        
  31.         return sum;
  32.     }
  33.    
  34.     /**
  35.      * @return The subtraction of both Vector2
  36.      */
  37.     public Vector2 subtract(Vector2 vec1, Vector2 vec2)
  38.     {
  39.         Vector2 sub = new Vector2((vec1.x - vec2.x), (vec1.y - vec2.y));
  40.        
  41.         return sub;
  42.     }
  43.    
  44.     /**
  45.      * @return The distance of both Vector2
  46.      */
  47.     public double distance(Vector2 vec1, Vector2 vec2)
  48.     {
  49.         double distance = length(subtract(vec1, vec2));
  50.        
  51.         return distance;
  52.     }
  53.    
  54.     /**
  55.      * @return The product of a Vector2 and a factor as a Vector2
  56.      */
  57.     public Vector2 scalarm(Vector2 vec, double factor)
  58.     {
  59.         Vector2 restult = new Vector2(vec.x * factor, vec.y * factor);
  60.        
  61.         return restult;
  62.     }
  63.    
  64.     /**
  65.      * @return The scalar product of both Vector2
  66.      */
  67.     public double scalarp(Vector2 vec1, Vector2 vec2)
  68.     {
  69.         double scalar = (vec1.x * vec2.x) + (vec1.y * vec2.y);
  70.        
  71.         return scalar;
  72.     }
  73.      
  74.     /**
  75.      * @return The length of the Vector2
  76.      */
  77.     public double length(Vector2 vec)
  78.     {
  79.         double length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  80.        
  81.         return length;
  82.     }
  83.    
  84.     /**
  85.      * @return The length of the Vector2
  86.      */
  87.     public double squaredLength(Vector2 vec)
  88.     {
  89.         double squaredLength = Math.pow(x, 2) + Math.pow(y, 2);
  90.        
  91.         return squaredLength;
  92.     }
  93.    
  94.     /**
  95.      * @return A Vector that was divided by its length
  96.      */
  97.     public Vector2 normalize(Vector2 vec)
  98.     {
  99.         Vector2 norm = new Vector2(vec.x * (1/vec.length), vec.y * (1/vec.length));
  100.        
  101.         return norm;
  102.     }
  103.    
  104.     /**
  105.      * @return The angle between two Vector2 in radians
  106.      */
  107.     public double angle(Vector2 vec1, Vector2 vec2)
  108.     {
  109.         vec1 = normalize(vec1);
  110.         vec2 = normalize(vec2);
  111.        
  112.         double angle = Math.acos(vec1.x * vec2.x + vec1.y * vec2.y);
  113.        
  114.         return angle;
  115.     }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement