class_rect2.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Rect2.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Rect2:
  6. Rect2
  7. =====
  8. 2D axis-aligned bounding box using floating point coordinates.
  9. Description
  10. -----------
  11. ``Rect2`` consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.
  12. It uses floating-point coordinates. If you need integer coordinates, use :ref:`Rect2i<class_Rect2i>` instead.
  13. The 3D counterpart to ``Rect2`` is :ref:`AABB<class_AABB>`.
  14. Tutorials
  15. ---------
  16. - :doc:`Math documentation index <../tutorials/math/index>`
  17. - :doc:`Vector math <../tutorials/math/vector_math>`
  18. - :doc:`Advanced vector math <../tutorials/math/vectors_advanced>`
  19. Properties
  20. ----------
  21. +-------------------------------+------------------------------------------------+-------------------+
  22. | :ref:`Vector2<class_Vector2>` | :ref:`end<class_Rect2_property_end>` | ``Vector2(0, 0)`` |
  23. +-------------------------------+------------------------------------------------+-------------------+
  24. | :ref:`Vector2<class_Vector2>` | :ref:`position<class_Rect2_property_position>` | ``Vector2(0, 0)`` |
  25. +-------------------------------+------------------------------------------------+-------------------+
  26. | :ref:`Vector2<class_Vector2>` | :ref:`size<class_Rect2_property_size>` | ``Vector2(0, 0)`` |
  27. +-------------------------------+------------------------------------------------+-------------------+
  28. Constructors
  29. ------------
  30. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** **)** |
  32. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`Rect2<class_Rect2>` from **)** |
  34. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`Rect2i<class_Rect2i>` from **)** |
  36. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`Vector2<class_Vector2>` position, :ref:`Vector2<class_Vector2>` size **)** |
  38. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`float<class_float>` x, :ref:`float<class_float>` y, :ref:`float<class_float>` width, :ref:`float<class_float>` height **)** |
  40. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. Methods
  42. -------
  43. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Rect2<class_Rect2>` | :ref:`abs<class_Rect2_method_abs>` **(** **)** |const| |
  45. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`bool<class_bool>` | :ref:`encloses<class_Rect2_method_encloses>` **(** :ref:`Rect2<class_Rect2>` b **)** |const| |
  47. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`Rect2<class_Rect2>` | :ref:`expand<class_Rect2_method_expand>` **(** :ref:`Vector2<class_Vector2>` to **)** |const| |
  49. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`float<class_float>` | :ref:`get_area<class_Rect2_method_get_area>` **(** **)** |const| |
  51. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Vector2<class_Vector2>` | :ref:`get_center<class_Rect2_method_get_center>` **(** **)** |const| |
  53. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`Rect2<class_Rect2>` | :ref:`grow<class_Rect2_method_grow>` **(** :ref:`float<class_float>` amount **)** |const| |
  55. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`Rect2<class_Rect2>` | :ref:`grow_individual<class_Rect2_method_grow_individual>` **(** :ref:`float<class_float>` left, :ref:`float<class_float>` top, :ref:`float<class_float>` right, :ref:`float<class_float>` bottom **)** |const| |
  57. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`Rect2<class_Rect2>` | :ref:`grow_side<class_Rect2_method_grow_side>` **(** :ref:`int<class_int>` side, :ref:`float<class_float>` amount **)** |const| |
  59. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`bool<class_bool>` | :ref:`has_no_area<class_Rect2_method_has_no_area>` **(** **)** |const| |
  61. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`bool<class_bool>` | :ref:`has_point<class_Rect2_method_has_point>` **(** :ref:`Vector2<class_Vector2>` point **)** |const| |
  63. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Rect2<class_Rect2>` | :ref:`intersection<class_Rect2_method_intersection>` **(** :ref:`Rect2<class_Rect2>` b **)** |const| |
  65. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`bool<class_bool>` | :ref:`intersects<class_Rect2_method_intersects>` **(** :ref:`Rect2<class_Rect2>` b, :ref:`bool<class_bool>` include_borders=false **)** |const| |
  67. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`bool<class_bool>` | :ref:`is_equal_approx<class_Rect2_method_is_equal_approx>` **(** :ref:`Rect2<class_Rect2>` rect **)** |const| |
  69. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`Rect2<class_Rect2>` | :ref:`merge<class_Rect2_method_merge>` **(** :ref:`Rect2<class_Rect2>` b **)** |const| |
  71. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. Operators
  73. ---------
  74. +---------------------------+-----------------------------------------------------------------------------------------------------------+
  75. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Rect2_operator_neq_bool>` **(** **)** |
  76. +---------------------------+-----------------------------------------------------------------------------------------------------------+
  77. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Rect2_operator_neq_bool>` **(** :ref:`Rect2<class_Rect2>` right **)** |
  78. +---------------------------+-----------------------------------------------------------------------------------------------------------+
  79. | :ref:`Rect2<class_Rect2>` | :ref:`operator *<class_Rect2_operator_mul_Rect2>` **(** :ref:`Transform2D<class_Transform2D>` right **)** |
  80. +---------------------------+-----------------------------------------------------------------------------------------------------------+
  81. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Rect2_operator_eq_bool>` **(** **)** |
  82. +---------------------------+-----------------------------------------------------------------------------------------------------------+
  83. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Rect2_operator_eq_bool>` **(** :ref:`Rect2<class_Rect2>` right **)** |
  84. +---------------------------+-----------------------------------------------------------------------------------------------------------+
  85. Property Descriptions
  86. ---------------------
  87. .. _class_Rect2_property_end:
  88. - :ref:`Vector2<class_Vector2>` **end**
  89. +-----------+-------------------+
  90. | *Default* | ``Vector2(0, 0)`` |
  91. +-----------+-------------------+
  92. Ending corner. This is calculated as ``position + size``. Setting this value will change the size.
  93. ----
  94. .. _class_Rect2_property_position:
  95. - :ref:`Vector2<class_Vector2>` **position**
  96. +-----------+-------------------+
  97. | *Default* | ``Vector2(0, 0)`` |
  98. +-----------+-------------------+
  99. Beginning corner. Typically has values lower than :ref:`end<class_Rect2_property_end>`.
  100. ----
  101. .. _class_Rect2_property_size:
  102. - :ref:`Vector2<class_Vector2>` **size**
  103. +-----------+-------------------+
  104. | *Default* | ``Vector2(0, 0)`` |
  105. +-----------+-------------------+
  106. Size from :ref:`position<class_Rect2_property_position>` to :ref:`end<class_Rect2_property_end>`. Typically, all components are positive.
  107. If the size is negative, you can use :ref:`abs<class_Rect2_method_abs>` to fix it.
  108. Constructor Descriptions
  109. ------------------------
  110. .. _class_Rect2_constructor_Rect2:
  111. - :ref:`Rect2<class_Rect2>` **Rect2** **(** **)**
  112. Constructs a default-initialized ``Rect2`` with default (zero) values of :ref:`position<class_Rect2_property_position>` and :ref:`size<class_Rect2_property_size>`.
  113. ----
  114. - :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`Rect2<class_Rect2>` from **)**
  115. Constructs a ``Rect2`` as a copy of the given ``Rect2``.
  116. ----
  117. - :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`Rect2i<class_Rect2i>` from **)**
  118. Constructs a ``Rect2`` from a :ref:`Rect2i<class_Rect2i>`.
  119. ----
  120. - :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`Vector2<class_Vector2>` position, :ref:`Vector2<class_Vector2>` size **)**
  121. Constructs a ``Rect2`` by position and size.
  122. ----
  123. - :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`float<class_float>` x, :ref:`float<class_float>` y, :ref:`float<class_float>` width, :ref:`float<class_float>` height **)**
  124. Constructs a ``Rect2`` by x, y, width, and height.
  125. Method Descriptions
  126. -------------------
  127. .. _class_Rect2_method_abs:
  128. - :ref:`Rect2<class_Rect2>` **abs** **(** **)** |const|
  129. Returns a ``Rect2`` with equivalent position and area, modified so that the top-left corner is the origin and ``width`` and ``height`` are positive.
  130. ----
  131. .. _class_Rect2_method_encloses:
  132. - :ref:`bool<class_bool>` **encloses** **(** :ref:`Rect2<class_Rect2>` b **)** |const|
  133. Returns ``true`` if this ``Rect2`` completely encloses another one.
  134. ----
  135. .. _class_Rect2_method_expand:
  136. - :ref:`Rect2<class_Rect2>` **expand** **(** :ref:`Vector2<class_Vector2>` to **)** |const|
  137. Returns a copy of this ``Rect2`` expanded to include a given point.
  138. **Example:**
  139. .. tabs::
  140. .. code-tab:: gdscript
  141. # position (-3, 2), size (1, 1)
  142. var rect = Rect2(Vector2(-3, 2), Vector2(1, 1))
  143. # position (-3, -1), size (3, 4), so we fit both rect and Vector2(0, -1)
  144. var rect2 = rect.expand(Vector2(0, -1))
  145. .. code-tab:: csharp
  146. # position (-3, 2), size (1, 1)
  147. var rect = new Rect2(new Vector2(-3, 2), new Vector2(1, 1));
  148. # position (-3, -1), size (3, 4), so we fit both rect and Vector2(0, -1)
  149. var rect2 = rect.Expand(new Vector2(0, -1));
  150. ----
  151. .. _class_Rect2_method_get_area:
  152. - :ref:`float<class_float>` **get_area** **(** **)** |const|
  153. Returns the area of the ``Rect2``.
  154. ----
  155. .. _class_Rect2_method_get_center:
  156. - :ref:`Vector2<class_Vector2>` **get_center** **(** **)** |const|
  157. Returns the center of the ``Rect2``, which is equal to :ref:`position<class_Rect2_property_position>` + (:ref:`size<class_Rect2_property_size>` / 2).
  158. ----
  159. .. _class_Rect2_method_grow:
  160. - :ref:`Rect2<class_Rect2>` **grow** **(** :ref:`float<class_float>` amount **)** |const|
  161. Returns a copy of the ``Rect2`` grown by the specified ``amount`` on all sides.
  162. ----
  163. .. _class_Rect2_method_grow_individual:
  164. - :ref:`Rect2<class_Rect2>` **grow_individual** **(** :ref:`float<class_float>` left, :ref:`float<class_float>` top, :ref:`float<class_float>` right, :ref:`float<class_float>` bottom **)** |const|
  165. Returns a copy of the ``Rect2`` grown by the specified amount on each side individually.
  166. ----
  167. .. _class_Rect2_method_grow_side:
  168. - :ref:`Rect2<class_Rect2>` **grow_side** **(** :ref:`int<class_int>` side, :ref:`float<class_float>` amount **)** |const|
  169. Returns a copy of the ``Rect2`` grown by the specified ``amount`` on the specified :ref:`Side<enum_@GlobalScope_Side>`.
  170. ----
  171. .. _class_Rect2_method_has_no_area:
  172. - :ref:`bool<class_bool>` **has_no_area** **(** **)** |const|
  173. Returns ``true`` if the ``Rect2`` is flat or empty.
  174. ----
  175. .. _class_Rect2_method_has_point:
  176. - :ref:`bool<class_bool>` **has_point** **(** :ref:`Vector2<class_Vector2>` point **)** |const|
  177. Returns ``true`` if the ``Rect2`` contains a point. By convention, the right and bottom edges of the ``Rect2`` are considered exclusive, so points on these edges are **not** included.
  178. **Note:** This method is not reliable for ``Rect2`` with a *negative size*. Use :ref:`abs<class_Rect2_method_abs>` to get a positive sized equivalent rectangle to check for contained points.
  179. ----
  180. .. _class_Rect2_method_intersection:
  181. - :ref:`Rect2<class_Rect2>` **intersection** **(** :ref:`Rect2<class_Rect2>` b **)** |const|
  182. Returns the intersection of this ``Rect2`` and ``b``.
  183. If the rectangles do not intersect, an empty ``Rect2`` is returned.
  184. ----
  185. .. _class_Rect2_method_intersects:
  186. - :ref:`bool<class_bool>` **intersects** **(** :ref:`Rect2<class_Rect2>` b, :ref:`bool<class_bool>` include_borders=false **)** |const|
  187. Returns ``true`` if the ``Rect2`` overlaps with ``b`` (i.e. they have at least one point in common).
  188. If ``include_borders`` is ``true``, they will also be considered overlapping if their borders touch, even without intersection.
  189. ----
  190. .. _class_Rect2_method_is_equal_approx:
  191. - :ref:`bool<class_bool>` **is_equal_approx** **(** :ref:`Rect2<class_Rect2>` rect **)** |const|
  192. Returns ``true`` if this ``Rect2`` and ``rect`` are approximately equal, by calling ``is_equal_approx`` on each component.
  193. ----
  194. .. _class_Rect2_method_merge:
  195. - :ref:`Rect2<class_Rect2>` **merge** **(** :ref:`Rect2<class_Rect2>` b **)** |const|
  196. Returns a larger ``Rect2`` that contains this ``Rect2`` and ``b``.
  197. Operator Descriptions
  198. ---------------------
  199. .. _class_Rect2_operator_neq_bool:
  200. - :ref:`bool<class_bool>` **operator !=** **(** **)**
  201. ----
  202. - :ref:`bool<class_bool>` **operator !=** **(** :ref:`Rect2<class_Rect2>` right **)**
  203. Returns ``true`` if the rectangles are not equal.
  204. **Note:** Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Rect2_method_is_equal_approx>` instead, which is more reliable.
  205. ----
  206. .. _class_Rect2_operator_mul_Rect2:
  207. - :ref:`Rect2<class_Rect2>` **operator *** **(** :ref:`Transform2D<class_Transform2D>` right **)**
  208. Inversely transforms (multiplies) the ``Rect2`` by the given :ref:`Transform2D<class_Transform2D>` transformation matrix.
  209. ----
  210. .. _class_Rect2_operator_eq_bool:
  211. - :ref:`bool<class_bool>` **operator ==** **(** **)**
  212. ----
  213. - :ref:`bool<class_bool>` **operator ==** **(** :ref:`Rect2<class_Rect2>` right **)**
  214. Returns ``true`` if the rectangles are exactly equal.
  215. **Note:** Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Rect2_method_is_equal_approx>` instead, which is more reliable.
  216. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  217. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  218. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  219. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  220. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  221. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`