Advertisement
Lusien_Lashans

Vector2D

Dec 1st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package azaza;
  2.  
  3. public class Vector2D implements IVector {
  4.     private final double x,y;
  5.  
  6.     public Vector2D(double x, double y) {
  7.         this.x = x;
  8.         this.y = y;
  9.     }
  10.  
  11.     @Override
  12.     public int dimension() {
  13.         return 2;
  14.     }
  15.  
  16.     @Override
  17.     public double getComponent(int i) {
  18.         switch (i) {
  19.             case 0:
  20.                 return x;
  21.             case 1:
  22.                 return y;
  23.             default:
  24.                 throw new IllegalArgumentException();
  25.         }
  26.     }
  27.  
  28.     @Override
  29.     public double scalar(IVector v) {
  30.         return x*x + y*y;
  31.     }
  32.  
  33.     @Override
  34.     public double length() {
  35.         return Math.sqrt(x * x + y * y);
  36.     }
  37.  
  38.     @Override
  39.     public IVector multiply(double number) {
  40.         return new Vector2D(number * x, number * y);
  41.     }
  42.  
  43.     @Override
  44.     public IVector add(IVector v) {
  45.         return new Vector2D(x + v.getComponent(0), y + v.getComponent(1));
  46.  
  47.     }
  48.  
  49.     @Override
  50.     public IVector sub(IVector v) {
  51.         return new Vector2D(x - v.getComponent(0), y - v.getComponent(1));
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement