Guest User

Untitled

a guest
Jul 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. package me.hugmanrique.vectorgrouper;
  2.  
  3. import lombok.Getter;
  4. import lombok.RequiredArgsConstructor;
  5.  
  6. /**
  7. * @author Hugo Manrique
  8. * @since 19/07/2018
  9. */
  10. @Getter
  11. @RequiredArgsConstructor
  12. public class Vector {
  13. private final double x;
  14. private final double y;
  15. private final double z;
  16.  
  17. public Vector(int x, int y, int z) {
  18. this((double) x, (double) y, (double) z);
  19. }
  20.  
  21. /**
  22. * Calculates the squared distance using the Pythagorean theorem
  23. */
  24. public double distanceSquared(Vector other) {
  25. double xDiff = x - other.getX();
  26. double yDiff = y - other.getY();
  27. double zDiff = z - other.getZ();
  28.  
  29. return xDiff * xDiff + yDiff * yDiff + zDiff * zDiff;
  30. }
  31.  
  32. public double distance(Vector other) {
  33. return Math.sqrt(distanceSquared(other));
  34. }
  35.  
  36. @Override
  37. public boolean equals(Object obj) {
  38. if (!(obj instanceof Vector)) {
  39. return false;
  40. }
  41.  
  42. Vector other = (Vector) obj;
  43.  
  44. return x == other.getX() && y == other.getY() && z == other.getZ();
  45. }
  46.  
  47. @Override
  48. public String toString() {
  49. return "Vector(x=" + x + ", y=" + y + ", z=" + z + ")";
  50. }
  51. }
Add Comment
Please, Sign In to add comment