Vector2dFloat.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package classes;
  2. public class Vector2dFloat {
  3. private float x;
  4. private float y;
  5. public Vector2dFloat()
  6. {
  7. x = y = 0;
  8. }
  9. public Vector2dFloat(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(Vector2dFloat 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(Vector2dFloat 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(Vector2dFloat other)
  57. {
  58. float xDistance = other.getX() - x;
  59. float yDistance = other.getY() - y;
  60. return xDistance * xDistance + yDistance * yDistance;
  61. }
  62. public Vector2dFloat add(Vector2dFloat other)
  63. {
  64. Vector2dFloat result = new Vector2dFloat();
  65. result.setX(x + other.getX());
  66. result.setY(y + other.getY());
  67. return result;
  68. }
  69. public Vector2dFloat subtract(Vector2dFloat other)
  70. {
  71. Vector2dFloat result = new Vector2dFloat();
  72. result.setX(x - other.getX());
  73. result.setY(y - other.getY());
  74. return result;
  75. }
  76. public Vector2dFloat multiply(float scaleFactor)
  77. {
  78. Vector2dFloat result = new Vector2dFloat();
  79. result.setX(x * scaleFactor);
  80. result.setY(y * scaleFactor);
  81. return result;
  82. }
  83. public Vector2dFloat normalize()
  84. {
  85. float len = getLength();
  86. if (len != 0.0f)
  87. {
  88. this.setX(x / len);
  89. this.setY(y / len);
  90. }
  91. else
  92. {
  93. this.setX(0.0f);
  94. this.setY(0.0f);
  95. }
  96. return this;
  97. }
  98. /**
  99. * Clamp the X Value two a upper or lower bound
  100. * @param min lower bound
  101. * @param max upper bound
  102. */
  103. public void clampX(float min, float max)
  104. {
  105. if(x < min) setX(min);
  106. else if(x > max) setX(max);
  107. }
  108. /**
  109. * Clamp the Y Value two a upper or lower bound
  110. * @param min lower bound
  111. * @param max upper bound
  112. */
  113. public void clampY(float min, float max)
  114. {
  115. if(y < min) setY(min);
  116. else if(y > max) setY(max);
  117. }
  118. public String toString()
  119. {
  120. return "X: " + x + " Y: " + y;
  121. }
  122. }