Vector2dInt.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Vector2dInt {
  11. private int x;
  12. private int y;
  13. public Vector2dInt()
  14. {
  15. x = y = 0;
  16. }
  17. public Vector2dInt(int x, int y)
  18. {
  19. setX(x);
  20. setY(y);
  21. }
  22. public void setX(int x) {
  23. this.x = x;
  24. }
  25. public int getX() {
  26. return x;
  27. }
  28. public void setY(int y) {
  29. this.y = y;
  30. }
  31. public int getY() {
  32. return y;
  33. }
  34. public void set(int x, int y)
  35. {
  36. setX(x);
  37. setY(y);
  38. }
  39. public void set(Vector2dInt other)
  40. {
  41. setX(other.x);
  42. setY(other.y);
  43. }
  44. public float dot(Vector2dInt other)
  45. {
  46. float result = 0.0f;
  47. result = x * other.getX() + y * other.getY();
  48. return result;
  49. }
  50. public float getLength()
  51. {
  52. return (float)Math.sqrt(x * x + y * y);
  53. }
  54. /**
  55. * Return the Distance to a other Position.
  56. * @param other the other Position.
  57. * @return distance to the Position.
  58. */
  59. public float getDistance(Vector2dInt other)
  60. {
  61. return (float) Math.sqrt(getSquaredDistance(other));
  62. }
  63. /**
  64. * Return the Distance squared to a other Position.
  65. * Faster then Distance because no Sqrt() needed.
  66. * @param other the other Position.
  67. * @return squared distance to the Position
  68. */
  69. public float getSquaredDistance(Vector2dInt other)
  70. {
  71. int xDistance = other.getX() - x;
  72. int yDistance = other.getY() - y;
  73. return xDistance * xDistance + yDistance * yDistance;
  74. }
  75. public Vector2dInt add(Vector2dInt other)
  76. {
  77. Vector2dInt result = new Vector2dInt();
  78. result.setX(x + other.getX());
  79. result.setY(y + other.getY());
  80. return result;
  81. }
  82. public Vector2dInt subtract(Vector2dInt other)
  83. {
  84. Vector2dInt result = new Vector2dInt();
  85. result.setX(x - other.getX());
  86. result.setY(y - other.getY());
  87. return result;
  88. }
  89. public Vector2dInt multiply(float scaleFactor)
  90. {
  91. Vector2dInt result = new Vector2dInt();
  92. result.setX((int)(x * scaleFactor));
  93. result.setY((int)(y * scaleFactor));
  94. return result;
  95. }
  96. public Vector2dInt divide(float divideFactor)
  97. {
  98. return this.multiply(1.0f/divideFactor);
  99. }
  100. /**
  101. * Clamp the X Value two a upper or lower bound
  102. * @param min lower bound
  103. * @param max upper bound
  104. */
  105. public void clampX(int min, int max)
  106. {
  107. if(x < min) setX(min);
  108. else if(x > max) setX(max);
  109. }
  110. /**
  111. * Clamp the Y Value two a upper or lower bound
  112. * @param min lower bound
  113. * @param max upper bound
  114. */
  115. public void clampY(int min, int max)
  116. {
  117. if(y < min) setY(min);
  118. else if(y > max) setY(max);
  119. }
  120. public String toString()
  121. {
  122. return "X: " + x + " Y: " + y;
  123. }
  124. public Vector2dInt clone() {
  125. return new Vector2dInt(x, y);
  126. }
  127. }