Advertisement
Idoor

Vector3D with comments

Sep 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package classes;
  2. import classes.Point3D;
  3.  
  4. public class Vector3D {
  5.     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 Vector3D(){ // конструктор по нулям
  14.         this.x = 0;
  15.         this.y = 0;
  16.         this.z = 0;
  17.     }
  18.  
  19.     public Vector3D(Point3D point1, Point3D point2){ // конструктор по двум точкам
  20.         this.x = point2.x - point1.x;
  21.         this.y = point2.y - point1.y;
  22.         this.z = point2.z - point1.z;
  23.     }
  24.  
  25.     public double Length(){
  26.         return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
  27.     } // длина вектора
  28.  
  29.     @Override
  30.     public boolean equals(Object o) { // снова сгенерировал проверку на равенство
  31.         if (this == o) return true;
  32.         if (o == null || getClass() != o.getClass()) return false;
  33.  
  34.         Vector3D vector3D = (Vector3D) o;
  35.  
  36.         if (Double.compare(vector3D.x, x) != 0) return false;
  37.         if (Double.compare(vector3D.y, y) != 0) return false;
  38.         return Double.compare(vector3D.z, z) == 0;
  39.     }
  40.  
  41.     public String toString() { // возвращает координаты в формате строки
  42.         return this.x +", " + this.y + ", " + this.z;
  43.     }
  44.  
  45.     //проверки
  46.  
  47.     public static void main(String args[]){
  48.         Point3D point_first = new Point3D(1, 1, 1);
  49.         Point3D point_second = new Point3D(2, 2, 2);
  50.  
  51.         Vector3D vec1 = new Vector3D(1, 2, 3);
  52.         Vector3D vec2 = new Vector3D(1, 2, 3);
  53.         Vector3D vec3 = new Vector3D();
  54.  
  55.         if(vec1 == vec3) {System.out.println("Вектора равны");}
  56.         else {System.out.println("Вектора не равны");}
  57.  
  58.         if(vec3 == vec3) {System.out.println("Вектор равен себе");}
  59.         else {System.out.println("Вектор не равен себе");}
  60.  
  61.         if(vec1.equals(vec2)) {System.out.println("Вектора равны");}
  62.         else {System.out.println("Вектора не равны");}
  63.  
  64.         System.out.println("Длина вектора = " +vec1.Length());
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement