vector2i.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*************************************************************************/
  2. /* vector2i.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VECTOR2I_H
  31. #define VECTOR2I_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/math_funcs.h"
  34. class String;
  35. struct Vector2;
  36. struct _NO_DISCARD_ Vector2i {
  37. enum Axis {
  38. AXIS_X,
  39. AXIS_Y,
  40. };
  41. union {
  42. struct {
  43. union {
  44. int32_t x;
  45. int32_t width;
  46. };
  47. union {
  48. int32_t y;
  49. int32_t height;
  50. };
  51. };
  52. int32_t coord[2] = { 0 };
  53. };
  54. _FORCE_INLINE_ int32_t &operator[](int p_idx) {
  55. DEV_ASSERT((unsigned int)p_idx < 2);
  56. return coord[p_idx];
  57. }
  58. _FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
  59. DEV_ASSERT((unsigned int)p_idx < 2);
  60. return coord[p_idx];
  61. }
  62. _FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
  63. return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y;
  64. }
  65. _FORCE_INLINE_ Vector2i::Axis max_axis_index() const {
  66. return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X;
  67. }
  68. Vector2i min(const Vector2i &p_vector2i) const {
  69. return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
  70. }
  71. Vector2i max(const Vector2i &p_vector2i) const {
  72. return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
  73. }
  74. Vector2i operator+(const Vector2i &p_v) const;
  75. void operator+=(const Vector2i &p_v);
  76. Vector2i operator-(const Vector2i &p_v) const;
  77. void operator-=(const Vector2i &p_v);
  78. Vector2i operator*(const Vector2i &p_v1) const;
  79. Vector2i operator*(const int32_t &rvalue) const;
  80. void operator*=(const int32_t &rvalue);
  81. Vector2i operator/(const Vector2i &p_v1) const;
  82. Vector2i operator/(const int32_t &rvalue) const;
  83. void operator/=(const int32_t &rvalue);
  84. Vector2i operator%(const Vector2i &p_v1) const;
  85. Vector2i operator%(const int32_t &rvalue) const;
  86. void operator%=(const int32_t &rvalue);
  87. Vector2i operator-() const;
  88. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  89. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  90. bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  91. bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  92. bool operator==(const Vector2i &p_vec2) const;
  93. bool operator!=(const Vector2i &p_vec2) const;
  94. int64_t length_squared() const;
  95. double length() const;
  96. real_t aspect() const { return width / (real_t)height; }
  97. Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
  98. Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
  99. Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
  100. operator String() const;
  101. operator Vector2() const;
  102. inline Vector2i() {}
  103. inline Vector2i(const int32_t p_x, const int32_t p_y) {
  104. x = p_x;
  105. y = p_y;
  106. }
  107. };
  108. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  109. _FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) {
  110. return p_vector * p_scalar;
  111. }
  112. _FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) {
  113. return p_vector * p_scalar;
  114. }
  115. _FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) {
  116. return p_vector * p_scalar;
  117. }
  118. _FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) {
  119. return p_vector * p_scalar;
  120. }
  121. typedef Vector2i Size2i;
  122. typedef Vector2i Point2i;
  123. #endif // VECTOR2I_H