Position.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package classes;
  2. import java.awt.Point;
  3. /**
  4. * Coordinates of an Object on the canvas with a (int) x-coord and a (int).
  5. * y-coord
  6. *
  7. * @author Gruppe14
  8. *
  9. */
  10. public class Position {
  11. /** Coordinate*/
  12. public int x,y;
  13. /**
  14. * Constructor with an positive x and y coord on the canvas.
  15. *
  16. * @param x Coord
  17. * @param y Coord
  18. */
  19. public Position(int x, int y) {
  20. this.x = x;
  21. this.y = y;
  22. }
  23. /**
  24. * Convert a Point to a Position;
  25. * @param p Point
  26. */
  27. public Position(Point p)
  28. {
  29. this.x = (int)p.getX();
  30. this.y = (int)p.getY();
  31. }
  32. /**
  33. * Default constructor without defined position.
  34. */
  35. public Position() {
  36. this.x = -1;
  37. this.y = -1;
  38. }
  39. /**
  40. * Return the Distance squared to a other Position.
  41. * Faster then Distance because no Sqrt() needed.
  42. * @param other the other Position.
  43. * @return squared distance to the Position
  44. */
  45. public double squareDistance(Position other)
  46. {
  47. //The Distance in X
  48. double xDis = x - other.x;
  49. //The Distance in Y
  50. double yDis = y - other.y;
  51. return xDis * xDis + yDis * yDis;
  52. }
  53. /**
  54. * Return the Distance to a other Position.
  55. * @param other the other Position.
  56. * @return distance to the Position.
  57. */
  58. public double Distance(Position other)
  59. {
  60. return Math.sqrt(squareDistance(other));
  61. }
  62. /**
  63. * Clamp the X Value two a upper or lower bound
  64. * @param min lower bound
  65. * @param max upper bound
  66. */
  67. public void clampX(int min, int max)
  68. {
  69. if(x < min) x = min;
  70. if(x > max) x = max;
  71. }
  72. /**
  73. * Clamp the Y Value two a upper or lower bound
  74. * @param min lower bound
  75. * @param max upper bound
  76. */
  77. public void clampY(int min, int max)
  78. {
  79. if(y < min) y = min;
  80. if(y > max) y = max;
  81. }
  82. /**
  83. * Returns a String that represents the value of this Position.
  84. * @return a string representation of this Position.
  85. */
  86. @Override
  87. public String toString() {
  88. return "Position[x=" + x + ",y="+ y +"]";
  89. }
  90. }