Vec2f.java 3.0 KB

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