Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.hugmanrique.vectorgrouper;
- import lombok.Getter;
- import lombok.RequiredArgsConstructor;
- /**
- * @author Hugo Manrique
- * @since 19/07/2018
- */
- @Getter
- @RequiredArgsConstructor
- public class Vector {
- private final double x;
- private final double y;
- private final double z;
- public Vector(int x, int y, int z) {
- this((double) x, (double) y, (double) z);
- }
- /**
- * Calculates the squared distance using the Pythagorean theorem
- */
- public double distanceSquared(Vector other) {
- double xDiff = x - other.getX();
- double yDiff = y - other.getY();
- double zDiff = z - other.getZ();
- return xDiff * xDiff + yDiff * yDiff + zDiff * zDiff;
- }
- public double distance(Vector other) {
- return Math.sqrt(distanceSquared(other));
- }
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof Vector)) {
- return false;
- }
- Vector other = (Vector) obj;
- return x == other.getX() && y == other.getY() && z == other.getZ();
- }
- @Override
- public String toString() {
- return "Vector(x=" + x + ", y=" + y + ", z=" + z + ")";
- }
- }
Add Comment
Please, Sign In to add comment