Vec2f.java 5.6 KB

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