Vector2Int.java 2.6 KB

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