Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package azaza;
- public class Vector3D implements IVector{
- private double x, y, z;
- public Vector3D(double x, double y, double z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public double getX() {return x;}
- public double getY() {return y;}
- public double getZ() {return z;}
- //размерность пространства
- public int dimension() {
- return 3;
- }
- public double getComponent(int i) {
- switch (i) {
- case 0:
- return x;
- case 1:
- return y;
- case 2:
- return z;
- default:
- throw new IllegalArgumentException();
- }
- }
- //Скалярное произведение
- public double scalar(IVector v) {
- Vector3D vector3D = (Vector3D) v;
- return (x * vector3D.x) + (y * vector3D.y) + (z * vector3D.z);
- }
- //Длина вектора
- public double length() {
- return Math.sqrt(x*x + y*y + z*z);
- }
- //Умножение вектора на число
- public Vector3D multiply(double number) {
- return new Vector3D(x * number,y * number,z * number);
- }
- //Сложение векторов
- public Vector3D add(Vector3D v) {
- return new Vector3D(x + v.getX(),y + v.getY(),z + v.getZ());
- }
- //Вычитание векторов
- public Vector3D sub(Vector3D v) {
- return new Vector3D(x - v.getX(),y - v.getY(),z - v.getZ());
- }
- //Переопределяем методы сложения и вычитания
- @Override
- public IVector add(IVector v) {
- return new Vector3D(x + v.getComponent(0),y + v.getComponent(1),z + v.getComponent(2));
- }
- @Override
- public IVector sub(IVector v) {
- return new Vector3D(x - v.getComponent(0),y - v.getComponent(1),z - v.getComponent(2));
- }
- //Определяем метод сравнения
- @Override
- public boolean equals(Object obj) {
- //Объекты должны быть одного типа и не null
- if (obj == null || obj.getClass() != this.getClass()) {
- return false;
- }
- Vector3D v = (Vector3D) obj;
- return (Double.compare(v.z, z) == 0)
- &&(Double.compare(v.x, x) == 0)
- &&(Double.compare(v.y, y) == 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment