Vec2f.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package holeg.utility.math.vector;
  2. public class Vec2f {
  3. public float x;
  4. public float y;
  5. public Vec2f()
  6. {
  7. x = y = 0;
  8. }
  9. public Vec2f(float x, float y)
  10. {
  11. setX(x);
  12. setY(y);
  13. }
  14. public Vec2f(Vec2f other)
  15. {
  16. x = other.x;
  17. y = other.y;
  18. }
  19. public Vec2f(Vec2i other)
  20. {
  21. x = other.getX();
  22. y = other.getY();
  23. }
  24. public void setX(float x) {
  25. this.x = x;
  26. }
  27. public float getX() {
  28. return x;
  29. }
  30. public void setY(float y) {
  31. this.y = y;
  32. }
  33. public float getY() {
  34. return y;
  35. }
  36. public void set(float x, float y)
  37. {
  38. setX(x);
  39. setY(y);
  40. }
  41. public float dot(Vec2f other)
  42. {
  43. float result = 0.0f;
  44. result = x * other.getX() + y * other.getY();
  45. return result;
  46. }
  47. public static float dot(Vec2f p1, Vec2f p2)
  48. {
  49. return p1.dot(p2);
  50. }
  51. public float getLength()
  52. {
  53. return (float)Math.sqrt(x * x + y * y);
  54. }
  55. /**
  56. * Return the Distance to a other Position.
  57. * @param other the other Position.
  58. * @return distance to the Position.
  59. */
  60. public float getDistance(Vec2f other)
  61. {
  62. return (float) Math.sqrt(getSquaredDistance(other));
  63. }
  64. /**
  65. * Return the Distance squared to a other Position.
  66. * Faster then Distance because no Sqrt() needed.
  67. * @param other the other Position.
  68. * @return squared distance to the Position
  69. */
  70. public float getSquaredDistance(Vec2f other)
  71. {
  72. float xDistance = other.getX() - x;
  73. float yDistance = other.getY() - y;
  74. return xDistance * xDistance + yDistance * yDistance;
  75. }
  76. public Vec2f add(Vec2f other)
  77. {
  78. Vec2f result = new Vec2f();
  79. result.setX(x + other.getX());
  80. result.setY(y + other.getY());
  81. return result;
  82. }
  83. public void addAssign(Vec2f other) {
  84. x += other.getX();
  85. y += other.getY();
  86. }
  87. public Vec2f subtract(Vec2f other)
  88. {
  89. Vec2f result = new Vec2f();
  90. result.setX(x - other.getX());
  91. result.setY(y - other.getY());
  92. return result;
  93. }
  94. public void subtractAssign(Vec2f other) {
  95. x -= other.getX();
  96. y -= other.getY();
  97. }
  98. public Vec2f multiply(float scaleFactor)
  99. {
  100. Vec2f result = new Vec2f();
  101. result.setX(x * scaleFactor);
  102. result.setY(y * scaleFactor);
  103. return result;
  104. }
  105. public void multiplyAssign(float scaleFactor)
  106. {
  107. x *= scaleFactor;
  108. y *= scaleFactor;
  109. }
  110. public Vec2f divide(float divideFactor)
  111. {
  112. return this.multiply(1.0f/divideFactor);
  113. }
  114. public void divideAssign(float divideFactor)
  115. {
  116. multiplyAssign(1.0f/divideFactor);
  117. }
  118. public Vec2f normalize()
  119. {
  120. float len = getLength();
  121. if (len != 0.0f)
  122. {
  123. this.setX(x / len);
  124. this.setY(y / len);
  125. }
  126. else
  127. {
  128. this.setX(0.0f);
  129. this.setY(0.0f);
  130. }
  131. return this;
  132. }
  133. /**
  134. * Clamp the X Value two a upper or lower bound
  135. * @param min lower bound
  136. * @param max upper bound
  137. */
  138. public void clampX(float min, float max)
  139. {
  140. if(x < min) setX(min);
  141. else if(x > max) setX(max);
  142. }
  143. /**
  144. * Clamp the Y Value two a upper or lower bound
  145. * @param min lower bound
  146. * @param max upper bound
  147. */
  148. public void clampY(float min, float max)
  149. {
  150. if(y < min) setY(min);
  151. else if(y > max) setY(max);
  152. }
  153. public String toString()
  154. {
  155. return "X: " + x + " Y: " + y;
  156. }
  157. }