vector2i.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**************************************************************************/
  2. /* vector2i.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. static const int AXIS_COUNT = 2;
  38. enum Axis {
  39. AXIS_X,
  40. AXIS_Y,
  41. };
  42. union {
  43. struct {
  44. union {
  45. int32_t x;
  46. int32_t width;
  47. };
  48. union {
  49. int32_t y;
  50. int32_t height;
  51. };
  52. };
  53. int32_t coord[2] = { 0 };
  54. };
  55. _FORCE_INLINE_ int32_t &operator[](int p_idx) {
  56. DEV_ASSERT((unsigned int)p_idx < 2);
  57. return coord[p_idx];
  58. }
  59. _FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
  60. DEV_ASSERT((unsigned int)p_idx < 2);
  61. return coord[p_idx];
  62. }
  63. _FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
  64. return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y;
  65. }
  66. _FORCE_INLINE_ Vector2i::Axis max_axis_index() const {
  67. return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X;
  68. }
  69. Vector2i min(const Vector2i &p_vector2i) const {
  70. return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
  71. }
  72. Vector2i max(const Vector2i &p_vector2i) const {
  73. return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
  74. }
  75. Vector2i operator+(const Vector2i &p_v) const;
  76. void operator+=(const Vector2i &p_v);
  77. Vector2i operator-(const Vector2i &p_v) const;
  78. void operator-=(const Vector2i &p_v);
  79. Vector2i operator*(const Vector2i &p_v1) const;
  80. Vector2i operator*(const int32_t &rvalue) const;
  81. void operator*=(const int32_t &rvalue);
  82. Vector2i operator/(const Vector2i &p_v1) const;
  83. Vector2i operator/(const int32_t &rvalue) const;
  84. void operator/=(const int32_t &rvalue);
  85. Vector2i operator%(const Vector2i &p_v1) const;
  86. Vector2i operator%(const int32_t &rvalue) const;
  87. void operator%=(const int32_t &rvalue);
  88. Vector2i operator-() const;
  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 { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  93. bool operator==(const Vector2i &p_vec2) const;
  94. bool operator!=(const Vector2i &p_vec2) const;
  95. int64_t length_squared() const;
  96. double length() const;
  97. real_t aspect() const { return width / (real_t)height; }
  98. Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
  99. Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
  100. Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
  101. Vector2i snapped(const Vector2i &p_step) const;
  102. operator String() const;
  103. operator Vector2() const;
  104. inline Vector2i() {}
  105. inline Vector2i(const int32_t p_x, const int32_t p_y) {
  106. x = p_x;
  107. y = p_y;
  108. }
  109. };
  110. // Multiplication operators required to workaround issues with LLVM using implicit conversion.
  111. _FORCE_INLINE_ Vector2i operator*(const int32_t p_scalar, const Vector2i &p_vector) {
  112. return p_vector * p_scalar;
  113. }
  114. _FORCE_INLINE_ Vector2i operator*(const int64_t p_scalar, const Vector2i &p_vector) {
  115. return p_vector * p_scalar;
  116. }
  117. _FORCE_INLINE_ Vector2i operator*(const float p_scalar, const Vector2i &p_vector) {
  118. return p_vector * p_scalar;
  119. }
  120. _FORCE_INLINE_ Vector2i operator*(const double p_scalar, const Vector2i &p_vector) {
  121. return p_vector * p_scalar;
  122. }
  123. typedef Vector2i Size2i;
  124. typedef Vector2i Point2i;
  125. #endif // VECTOR2I_H