Idoor

Vector3D

Sep 21st, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 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.     public static void main(String args[]){
  46.         Point3D point_first = new Point3D(1, 1, 1);
  47.         Point3D point_second = new Point3D(2, 2, 2);
  48.  
  49.         Vector3D vec1 = new Vector3D(1, 2, 3);
  50.         Vector3D vec2 = new Vector3D(1, 2, 3);
  51.         Vector3D vec3 = new Vector3D();
  52.  
  53.         if(vec1 == vec3) {System.out.println("Вектора равны");}
  54.         else {System.out.println("Вектора не равны");}
  55.  
  56.         if(vec3 == vec3) {System.out.println("Вектор равен себе");}
  57.         else {System.out.println("Вектор не равен себе");}
  58.  
  59.         if(vec1.equals(vec2)) {System.out.println("Вектора равны");}
  60.         else {System.out.println("Вектора не равны");}
  61.  
  62.         System.out.println("Длина вектора = " +vec1.Length());
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment