Vector2Int.java 2.6 KB

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