Lusien_Lashans

Vector3D

Dec 1st, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package azaza;
  2.  
  3. public class Vector3D implements IVector{
  4.  
  5.     private double x, y, z;
  6.  
  7.     public Vector3D(double x, double y, double z) {
  8.         this.x = x;
  9.         this.y = y;
  10.         this.z = z;
  11.     }
  12.  
  13.     public double getX() {return x;}
  14.  
  15.     public double getY() {return y;}
  16.  
  17.     public double getZ() {return z;}
  18.  
  19.     //размерность пространства
  20.     public int dimension() {
  21.         return 3;
  22.     }
  23.  
  24.     public double getComponent(int i) {
  25.         switch (i) {
  26.             case 0:
  27.                 return x;
  28.             case 1:
  29.                 return y;
  30.             case 2:
  31.                 return z;
  32.             default:
  33.                 throw new IllegalArgumentException();
  34.         }
  35.     }
  36.  
  37.     //Скалярное произведение
  38.     public double scalar(IVector v) {
  39.         Vector3D vector3D = (Vector3D) v;
  40.         return (x * vector3D.x) + (y * vector3D.y) + (z * vector3D.z);
  41.     }
  42.  
  43.     //Длина вектора
  44.     public double length() {
  45.         return Math.sqrt(x*x + y*y + z*z);
  46.     }
  47.  
  48.     //Умножение вектора на число
  49.     public Vector3D multiply(double number) {
  50.         return new Vector3D(x * number,y * number,z * number);
  51.     }
  52.  
  53.     //Сложение векторов
  54.     public Vector3D add(Vector3D v) {
  55.  
  56.         return new Vector3D(x + v.getX(),y + v.getY(),z + v.getZ());
  57.     }
  58.     //Вычитание векторов
  59.     public Vector3D sub(Vector3D v) {
  60.  
  61.         return new Vector3D(x - v.getX(),y - v.getY(),z - v.getZ());
  62.     }
  63.  
  64.     //Переопределяем методы сложения и вычитания
  65.     @Override
  66.     public IVector add(IVector v) {
  67.         return new Vector3D(x + v.getComponent(0),y + v.getComponent(1),z + v.getComponent(2));
  68.     }
  69.  
  70.     @Override
  71.     public IVector sub(IVector v) {
  72.         return new Vector3D(x - v.getComponent(0),y - v.getComponent(1),z - v.getComponent(2));
  73.     }
  74.  
  75.     //Определяем метод сравнения
  76.     @Override
  77.     public boolean equals(Object obj) {
  78.         //Объекты должны быть одного типа и не null
  79.         if (obj == null || obj.getClass() != this.getClass()) {
  80.             return false;
  81.         }
  82.  
  83.         Vector3D v = (Vector3D) obj;
  84.  
  85.         return (Double.compare(v.z, z) == 0)
  86.         &&(Double.compare(v.x, x) == 0)
  87.         &&(Double.compare(v.y, y) == 0);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment