Guest User

Untitled

a guest
Feb 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package MyGeometryLib;
  2.  
  3. import org.jetbrains.annotations.Contract;
  4.  
  5. public class Segment {
  6. private Vector begin;
  7. private Vector end;
  8.  
  9. Segment(Vector begin, Vector end) {
  10. this.begin = begin;
  11. this.end = end;
  12. }
  13.  
  14. @Contract(pure = true)
  15. private Vector getStart() {
  16. return begin;
  17. }
  18.  
  19. @Contract(pure = true)
  20. private Vector getEnd() {
  21. return end;
  22. }
  23.  
  24. double length() {
  25. return (this.end.sub(this.begin)).len();
  26. }
  27.  
  28. double distanceTo(Vector point) {
  29. double lengthFirstSide = (point.sub(begin)).len();
  30. double lengthSecondeSide = (point.sub(end)).len();
  31. double lengthLastSide = (end.sub(begin)).len();
  32.  
  33. if (lengthLastSide == 0.0) {
  34. return (end.sub(begin)).sub(point).len();
  35. }
  36.  
  37. double halfPerimeter = (lengthFirstSide + lengthSecondeSide + lengthLastSide) / 2;
  38. return (2*Math.sqrt(halfPerimeter*(halfPerimeter - lengthFirstSide)*(halfPerimeter - lengthSecondeSide)*(halfPerimeter - lengthLastSide))/lengthLastSide);
  39. }
  40.  
  41. @Override
  42. public boolean equals(Object obj) {
  43. if (obj == this) {
  44. return true;
  45. }
  46.  
  47. if (obj == null || obj.getClass() != this.getClass()) {
  48. return false;
  49. }
  50.  
  51. Segment segment = (Segment) obj;
  52.  
  53. return this.begin.equals(segment.begin) &&
  54. this.end.equals(segment.end) &&
  55. (this.end.sub(this.begin)).len() == segment.length();
  56. }
  57. }
Add Comment
Please, Sign In to add comment