Advertisement
calcpage

LACS09_Vector.java

Jun 21st, 2012
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.80 KB | None | 0 0
  1. //Vector.java       MrG 2012.0621
  2. public class Vector
  3. {
  4.     private double x;
  5.     private double y;
  6.  
  7.     public Vector(double x, double y)
  8.     {
  9.         this.x = x;
  10.         this.y = y;
  11.     }
  12.  
  13.     public double getX()
  14.     {
  15.         return x;
  16.     }
  17.  
  18.     public double getY()
  19.     {
  20.         return y;
  21.     }
  22.  
  23.     public String toString()
  24.     {
  25.         return "<" + x + ", " + y + ">";
  26.     }
  27.  
  28.     public Vector dilate(double size)
  29.     {
  30.         return new Vector(x*size,y*size);
  31.     }
  32.  
  33.     public Vector sum(Vector other)
  34.     {
  35.         double tempX = this.x+other.getX();
  36.         double tempY = this.y+other.getY();
  37.         return new Vector(tempX,tempY);
  38.     }
  39.  
  40.     public Vector diff(Vector other)
  41.     {
  42.         return this.sum(other.dilate(-1));
  43.     }
  44.  
  45.     public double dot(Vector other)
  46.     {
  47.         return this.x*other.getX()+this.y*other.getY();
  48.     }
  49.  
  50.     public double abs()
  51.     {
  52.         return Math.sqrt(this.dot(this));
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement