Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package classes;
- import classes.Point3D;
- public class Vector3D {
- double x, y, z; // поля
- public Vector3D(double x, double y, double z){ //конструктор по координатам
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public Vector3D(){ // конструктор по нулям
- this.x = 0;
- this.y = 0;
- this.z = 0;
- }
- public Vector3D(Point3D point1, Point3D point2){ // конструктор по двум точкам
- this.x = point2.x - point1.x;
- this.y = point2.y - point1.y;
- this.z = point2.z - point1.z;
- }
- public double Length(){
- return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
- } // длина вектора
- @Override
- public boolean equals(Object o) { // снова сгенерировал проверку на равенство
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Vector3D vector3D = (Vector3D) o;
- if (Double.compare(vector3D.x, x) != 0) return false;
- if (Double.compare(vector3D.y, y) != 0) return false;
- return Double.compare(vector3D.z, z) == 0;
- }
- public String toString() { // возвращает координаты в формате строки
- return this.x +", " + this.y + ", " + this.z;
- }
- //проверки
- public static void main(String args[]){
- Point3D point_first = new Point3D(1, 1, 1);
- Point3D point_second = new Point3D(2, 2, 2);
- Vector3D vec1 = new Vector3D(1, 2, 3);
- Vector3D vec2 = new Vector3D(1, 2, 3);
- Vector3D vec3 = new Vector3D();
- if(vec1 == vec3) {System.out.println("Вектора равны");}
- else {System.out.println("Вектора не равны");}
- if(vec3 == vec3) {System.out.println("Вектор равен себе");}
- else {System.out.println("Вектор не равен себе");}
- if(vec1.equals(vec2)) {System.out.println("Вектора равны");}
- else {System.out.println("Вектора не равны");}
- System.out.println("Длина вектора = " +vec1.Length());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement