Vec2i.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /**
  63. * Set the entries of the vector
  64. * @param x first entry
  65. * @param y second entry
  66. */
  67. public void set(int x, int y) {
  68. this.x = x;
  69. this.y = y;
  70. }
  71. /**
  72. * Copies the entries from another vector
  73. * @param other another Vector
  74. */
  75. public void set(Vec2i other) {
  76. x = other.x;
  77. y = other.y;
  78. }
  79. /**
  80. * Creates the dot product with another vector.
  81. *
  82. * @param other the other vector
  83. * @return the dot product
  84. */
  85. public float dot(Vec2i other) {
  86. return (float) (x * other.getX() + y * other.getY());
  87. }
  88. /**
  89. * Returns the euclidean distance to zero.
  90. *
  91. * @return distance to zero
  92. */
  93. public float getLength() {
  94. return (float) Math.sqrt(x * x + y * y);
  95. }
  96. /**
  97. * Return the Distance to a other vector.
  98. *
  99. * @param other the other vector.
  100. * @return distance to the vector.
  101. */
  102. public float getDistance(Vec2i other) {
  103. return (float) Math.sqrt(getSquaredDistance(other));
  104. }
  105. /**
  106. * Return the squared distance to a other Position. Faster than {@link #getDistance(Vec2i)}
  107. * because no {@link Math#sqrt(double)} needed.
  108. *
  109. * @param other the other Position.
  110. * @return squared distance to the Position
  111. */
  112. public float getSquaredDistance(Vec2i other) {
  113. int xDistance = other.getX() - x;
  114. int yDistance = other.getY() - y;
  115. return xDistance * xDistance + yDistance * yDistance;
  116. }
  117. /**
  118. * Returns a new vector that is the sum of this vector and another Vector.
  119. *
  120. * @param other the other vector
  121. * @return the sum
  122. */
  123. public Vec2i add(Vec2i other) {
  124. Vec2i result = new Vec2i();
  125. result.setX(x + other.getX());
  126. result.setY(y + other.getY());
  127. return result;
  128. }
  129. /**
  130. * Add the entries of another Vector to this vector.
  131. *
  132. * @param other the other vector
  133. */
  134. public void addAssign(Vec2i other) {
  135. x += other.getX();
  136. y += other.getY();
  137. }
  138. /**
  139. * Returns a new vector that is the subtraction of this vector and another Vector.
  140. *
  141. * @param other the other vector
  142. * @return the subtraction
  143. */
  144. public Vec2i subtract(Vec2i other) {
  145. Vec2i result = new Vec2i();
  146. result.setX(x - other.getX());
  147. result.setY(y - other.getY());
  148. return result;
  149. }
  150. /**
  151. * Subtract the entries of another Vector to this vector.
  152. *
  153. * @param other the other vector
  154. */
  155. public void subtractAssign(Vec2i other) {
  156. x -= other.getX();
  157. y -= other.getY();
  158. }
  159. /**
  160. * Returns a new vector which entries are multiplied times a scaled factor.
  161. * @param scaleFactor the scale factor
  162. * @return the multiplication from this vector with a scale
  163. */
  164. public Vec2i multiply(float scaleFactor) {
  165. Vec2i result = new Vec2i();
  166. result.setX((int) (x * scaleFactor));
  167. result.setY((int) (y * scaleFactor));
  168. return result;
  169. }
  170. /**
  171. * Multiply this vector entries time a scale factor.
  172. * @param scaleFactor the scale factor
  173. */
  174. public void multiplyAssign(float scaleFactor) {
  175. x *= scaleFactor;
  176. y *= scaleFactor;
  177. }
  178. /** Returns a new vector which entries are divided with a divide factor.
  179. * @param divideFactor the divide factor
  180. * @return the division from this vector and a divide factor
  181. */
  182. public Vec2i divide(float divideFactor) {
  183. return this.multiply(1.0f / divideFactor);
  184. }
  185. /**
  186. * Divide this vector entries with a divide factor.
  187. * @param divideFactor the divide factor
  188. */
  189. public void divideAssign(float divideFactor) {
  190. multiplyAssign(1.0f / divideFactor);
  191. }
  192. /**
  193. * Clamp the X Value two a upper or lower bound
  194. *
  195. * @param min lower bound
  196. * @param max upper bound
  197. */
  198. public void clampX(int min, int max) {
  199. x = Maths.clamp(x, min, max);
  200. }
  201. /**
  202. * Clamp the Y Value two a upper or lower bound
  203. *
  204. * @param min lower bound
  205. * @param max upper bound
  206. */
  207. public void clampY(int min, int max) {
  208. y = Maths.clamp(y, min, max);
  209. }
  210. @Override
  211. public String toString() {
  212. return "Vec2i(" + x + "," + y + ")";
  213. }
  214. }