Vector2Float.java 2.8 KB

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