Vec2f.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 Vec2f subtract(Vec2f other)
  75. {
  76. Vec2f result = new Vec2f();
  77. result.setX(x - other.getX());
  78. result.setY(y - other.getY());
  79. return result;
  80. }
  81. public Vec2f multiply(float scaleFactor)
  82. {
  83. Vec2f result = new Vec2f();
  84. result.setX(x * scaleFactor);
  85. result.setY(y * scaleFactor);
  86. return result;
  87. }
  88. public Vec2f divide(float divideFactor)
  89. {
  90. Vec2f result = new Vec2f();
  91. result.setX(x / divideFactor);
  92. result.setY(y / divideFactor);
  93. return result;
  94. }
  95. public Vec2f normalize()
  96. {
  97. float len = getLength();
  98. if (len != 0.0f)
  99. {
  100. this.setX(x / len);
  101. this.setY(y / len);
  102. }
  103. else
  104. {
  105. this.setX(0.0f);
  106. this.setY(0.0f);
  107. }
  108. return this;
  109. }
  110. /**
  111. * Clamp the X Value two a upper or lower bound
  112. * @param min lower bound
  113. * @param max upper bound
  114. */
  115. public void clampX(float min, float max)
  116. {
  117. if(x < min) setX(min);
  118. else if(x > max) setX(max);
  119. }
  120. /**
  121. * Clamp the Y Value two a upper or lower bound
  122. * @param min lower bound
  123. * @param max upper bound
  124. */
  125. public void clampY(float min, float max)
  126. {
  127. if(y < min) setY(min);
  128. else if(y > max) setY(max);
  129. }
  130. public String toString()
  131. {
  132. return "X: " + x + " Y: " + y;
  133. }
  134. }