Vector2.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef VECTOR2_H
  2. #define VECTOR2_H
  3. #if defined(_WIN32)
  4. # ifdef _GD_CPP_CORE_API_IMPL
  5. # define GD_CPP_CORE_API __declspec(dllexport)
  6. # else
  7. # define GD_CPP_CORE_API __declspec(dllimport)
  8. # endif
  9. #else
  10. # define GD_CPP_CORE_API
  11. #endif
  12. #include <godot/godot_vector2.h>
  13. #include "Defs.hpp"
  14. namespace godot {
  15. class String;
  16. struct GD_CPP_CORE_API Vector2 {
  17. union {
  18. real_t x;
  19. real_t width;
  20. };
  21. union {
  22. real_t y;
  23. real_t height;
  24. };
  25. inline real_t& operator[](int p_idx) {
  26. return p_idx?y:x;
  27. }
  28. inline const real_t& operator[](int p_idx) const {
  29. return p_idx?y:x;
  30. }
  31. Vector2 operator+(const Vector2& p_v) const;
  32. void operator+=(const Vector2& p_v);
  33. Vector2 operator-(const Vector2& p_v) const;
  34. void operator-=(const Vector2& p_v);
  35. Vector2 operator*(const Vector2 &p_v1) const;
  36. Vector2 operator*(const real_t &rvalue) const;
  37. void operator*=(const real_t &rvalue);
  38. inline void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  39. Vector2 operator/(const Vector2 &p_v1) const;
  40. Vector2 operator/(const real_t &rvalue) const;
  41. void operator/=(const real_t &rvalue);
  42. Vector2 operator-() const;
  43. bool operator==(const Vector2& p_vec2) const;
  44. bool operator!=(const Vector2& p_vec2) const;
  45. inline bool operator<(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  46. inline bool operator<=(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<=p_vec2.y):(x<=p_vec2.x); }
  47. void normalize();
  48. Vector2 normalized() const;
  49. real_t length() const;
  50. real_t length_squared() const;
  51. real_t distance_to(const Vector2& p_vector2) const;
  52. real_t distance_squared_to(const Vector2& p_vector2) const;
  53. real_t angle_to(const Vector2& p_vector2) const;
  54. real_t angle_to_point(const Vector2& p_vector2) const;
  55. real_t dot(const Vector2& p_other) const;
  56. real_t cross(const Vector2& p_other) const;
  57. Vector2 cross(real_t p_other) const;
  58. Vector2 project(const Vector2& p_vec) const;
  59. Vector2 plane_project(real_t p_d, const Vector2& p_vec) const;
  60. Vector2 clamped(real_t p_len) const;
  61. static Vector2 linear_interpolate(const Vector2& p_a, const Vector2& p_b,real_t p_t);
  62. Vector2 linear_interpolate(const Vector2& p_b,real_t p_t) const;
  63. Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const;
  64. Vector2 slide(const Vector2& p_vec) const;
  65. Vector2 reflect(const Vector2& p_vec) const;
  66. real_t angle() const;
  67. void set_rotation(real_t p_radians);
  68. Vector2 abs() const;
  69. Vector2 rotated(real_t p_by) const;
  70. Vector2 tangent() const;
  71. Vector2 floor() const;
  72. Vector2 snapped(const Vector2& p_by) const;
  73. inline real_t aspect() const { return width/height; }
  74. operator String() const;
  75. inline Vector2(real_t p_x,real_t p_y) { x=p_x; y=p_y; }
  76. inline Vector2() { x=0; y=0; }
  77. };
  78. inline Vector2 operator*(real_t p_scalar, const Vector2& p_vec)
  79. {
  80. return p_vec*p_scalar;
  81. }
  82. }
  83. #endif // VECTOR2_H