Vec2i.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package holeg.utility.math.vector;
  2. import holeg.utility.math.Maths;
  3. import java.awt.Point;
  4. /**
  5. * A class representing a 2 dimensional vector with integer entries.
  6. */
  7. public class Vec2i {
  8. /**
  9. * The first entry.
  10. */
  11. private int x;
  12. /**
  13. * The second entry.
  14. */
  15. private int y;
  16. /**
  17. * Constructs a vector and initialized it with zeros.
  18. */
  19. public Vec2i() {
  20. x = y = 0;
  21. }
  22. /**
  23. * Constructs a vector with x and y.
  24. *
  25. * @param x the first entry
  26. * @param y the second entry
  27. */
  28. public Vec2i(int x, int y) {
  29. this.x = x;
  30. this.y = y;
  31. }
  32. /**
  33. * Creates a copy of another vector.
  34. *
  35. * @param other the other to copy the entries from
  36. */
  37. public Vec2i(Vec2i other) {
  38. x = other.x;
  39. y = other.y;
  40. }
  41. /**
  42. * Creates a vector from a point.
  43. *
  44. * @param point the point
  45. */
  46. public Vec2i(Point point) {
  47. x = point.x;
  48. y = point.y;
  49. }
  50. public int getX() {
  51. return x;
  52. }
  53. public void setX(int x) {
  54. this.x = x;
  55. }
  56. public int getY() {
  57. return y;
  58. }
  59. public void setY(int y) {
  60. this.y = y;
  61. }
  62. public void set(int x, int y) {
  63. this.x = x;
  64. this.y = y;
  65. }
  66. public void set(Vec2i other) {
  67. x = other.x;
  68. y = other.y;
  69. }
  70. /**
  71. * Creates the dot product with another vector.
  72. *
  73. * @param other the other vector
  74. * @return the dot product
  75. */
  76. public float dot(Vec2i other) {
  77. return (float) (x * other.getX() + y * other.getY());
  78. }
  79. /**
  80. * Returns the euclidean distance to zero.
  81. *
  82. * @return distance to zero
  83. */
  84. public float getLength() {
  85. return (float) Math.sqrt(x * x + y * y);
  86. }
  87. /**
  88. * Return the Distance to a other vector.
  89. *
  90. * @param other the other vector.
  91. * @return distance to the vector.
  92. */
  93. public float getDistance(Vec2i other) {
  94. return (float) Math.sqrt(getSquaredDistance(other));
  95. }
  96. /**
  97. * Return the squared distance to a other Position. Faster than {@link #getDistance(Vec2i)}
  98. * because no {@link Math#sqrt(double)} needed.
  99. *
  100. * @param other the other Position.
  101. * @return squared distance to the Position
  102. */
  103. public float getSquaredDistance(Vec2i other) {
  104. int xDistance = other.getX() - x;
  105. int yDistance = other.getY() - y;
  106. return xDistance * xDistance + yDistance * yDistance;
  107. }
  108. /**
  109. * Returns a new vector that is the sum of this vector and another Vector.
  110. *
  111. * @param other the other vector
  112. * @return the sum
  113. */
  114. public Vec2i add(Vec2i other) {
  115. Vec2i result = new Vec2i();
  116. result.setX(x + other.getX());
  117. result.setY(y + other.getY());
  118. return result;
  119. }
  120. /**
  121. * Add the entries of another Vector to this vector.
  122. *
  123. * @param other the other vector
  124. */
  125. public void addAssign(Vec2i other) {
  126. x += other.getX();
  127. y += other.getY();
  128. }
  129. /**
  130. * Returns a new vector that is the subtraction of this vector and another Vector.
  131. *
  132. * @param other the other vector
  133. * @return the subtraction
  134. */
  135. public Vec2i subtract(Vec2i other) {
  136. Vec2i result = new Vec2i();
  137. result.setX(x - other.getX());
  138. result.setY(y - other.getY());
  139. return result;
  140. }
  141. /**
  142. * Subtract the entries of another Vector to this vector.
  143. *
  144. * @param other the other vector
  145. */
  146. public void subtractAssign(Vec2i other) {
  147. x -= other.getX();
  148. y -= other.getY();
  149. }
  150. /**
  151. * Returns a new vector which entries are multiplied times a scaled factor.
  152. * @param scaleFactor the scale factor
  153. * @return the multiplication from this vector with a scale
  154. */
  155. public Vec2i multiply(float scaleFactor) {
  156. Vec2i result = new Vec2i();
  157. result.setX((int) (x * scaleFactor));
  158. result.setY((int) (y * scaleFactor));
  159. return result;
  160. }
  161. /**
  162. * Multiply this vector entries time a scale factor.
  163. * @param scaleFactor the scale factor
  164. */
  165. public void multiplyAssign(float scaleFactor) {
  166. x *= scaleFactor;
  167. y *= scaleFactor;
  168. }
  169. /** Returns a new vector which entries are divided with a divide factor.
  170. * @param divideFactor the divide factor
  171. * @return the division from this vector and a divide factor
  172. */
  173. public Vec2i divide(float divideFactor) {
  174. return this.multiply(1.0f / divideFactor);
  175. }
  176. /**
  177. * Divide this vector entries with a divide factor.
  178. * @param divideFactor the divide factor
  179. */
  180. public void divideAssign(float divideFactor) {
  181. multiplyAssign(1.0f / divideFactor);
  182. }
  183. /**
  184. * Clamp the X Value two a upper or lower bound
  185. *
  186. * @param min lower bound
  187. * @param max upper bound
  188. */
  189. public void clampX(int min, int max) {
  190. x = Maths.clamp(x, min, max);
  191. }
  192. /**
  193. * Clamp the Y Value two a upper or lower bound
  194. *
  195. * @param min lower bound
  196. * @param max upper bound
  197. */
  198. public void clampY(int min, int max) {
  199. y = Maths.clamp(y, min, max);
  200. }
  201. @Override
  202. public String toString() {
  203. return "Vec2i(" + x + "," + y + ")";
  204. }
  205. }