Vector2.js 1005 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class Vector2
  2. {
  3. static ZERO()
  4. {
  5. return new Vector2(0, 0);
  6. }
  7. static ONE()
  8. {
  9. return new Vector2(1, 1);
  10. }
  11. static RIGHT()
  12. {
  13. return new Vector2(1, 0);
  14. }
  15. static LEFT()
  16. {
  17. return new Vector2(-1, 0);
  18. }
  19. static UP()
  20. {
  21. return new Vector2(0, -1);
  22. }
  23. static DOWN()
  24. {
  25. return new Vector2(0, 1);
  26. }
  27. constructor(x, y)
  28. {
  29. this.x = x;
  30. this.y = y;
  31. }
  32. // Methods
  33. abs()
  34. {
  35. return new Vector2(abs(this.x), abs(this.y));
  36. }
  37. angle()
  38. {
  39. return atan2(this.y, this.x);
  40. }
  41. lengthSquared()
  42. {
  43. return this.x * this.x + this.y * this.y;
  44. }
  45. length()
  46. {
  47. return sqrt(this.lengthSquared());
  48. }
  49. normalized()
  50. {
  51. let len = this.length();
  52. return new Vector2(this.x / len, this.y / len);
  53. }
  54. distanceSquaredTo(v)
  55. {
  56. return new Vector2(v.x - this.x, v.y - this.y).length();
  57. }
  58. }