class_rect2.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.1/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.1/doc/classes/Rect2.xml.
  6. .. _class_Rect2:
  7. Rect2
  8. =====
  9. A 2D axis-aligned bounding box using floating-point coordinates.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. **Rect2** consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.
  14. It uses floating-point coordinates. If you need integer coordinates, use :ref:`Rect2i<class_Rect2i>` instead.
  15. The 3D counterpart to **Rect2** is :ref:`AABB<class_AABB>`.
  16. Negative values for :ref:`size<class_Rect2_property_size>` are not supported and will not work for most methods. Use :ref:`abs<class_Rect2_method_abs>` to get a Rect2 with a positive size.
  17. .. rst-class:: classref-introduction-group
  18. Tutorials
  19. ---------
  20. - :doc:`Math documentation index <../tutorials/math/index>`
  21. - :doc:`Vector math <../tutorials/math/vector_math>`
  22. - :doc:`Advanced vector math <../tutorials/math/vectors_advanced>`
  23. .. rst-class:: classref-reftable-group
  24. Properties
  25. ----------
  26. .. table::
  27. :widths: auto
  28. +-------------------------------+------------------------------------------------+-------------------+
  29. | :ref:`Vector2<class_Vector2>` | :ref:`end<class_Rect2_property_end>` | ``Vector2(0, 0)`` |
  30. +-------------------------------+------------------------------------------------+-------------------+
  31. | :ref:`Vector2<class_Vector2>` | :ref:`position<class_Rect2_property_position>` | ``Vector2(0, 0)`` |
  32. +-------------------------------+------------------------------------------------+-------------------+
  33. | :ref:`Vector2<class_Vector2>` | :ref:`size<class_Rect2_property_size>` | ``Vector2(0, 0)`` |
  34. +-------------------------------+------------------------------------------------+-------------------+
  35. .. rst-class:: classref-reftable-group
  36. Constructors
  37. ------------
  38. .. table::
  39. :widths: auto
  40. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** **)** |
  42. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`Rect2<class_Rect2>` from **)** |
  44. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`Rect2i<class_Rect2i>` from **)** |
  46. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`Rect2<class_Rect2>` | :ref:`Rect2<class_Rect2_constructor_Rect2>` **(** :ref:`Vector2<class_Vector2>` position, :ref:`Vector2<class_Vector2>` size **)** |
  48. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :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 **)** |
  50. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. .. rst-class:: classref-reftable-group
  52. Methods
  53. -------
  54. .. table::
  55. :widths: auto
  56. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Rect2<class_Rect2>` | :ref:`abs<class_Rect2_method_abs>` **(** **)** |const| |
  58. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`bool<class_bool>` | :ref:`encloses<class_Rect2_method_encloses>` **(** :ref:`Rect2<class_Rect2>` b **)** |const| |
  60. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Rect2<class_Rect2>` | :ref:`expand<class_Rect2_method_expand>` **(** :ref:`Vector2<class_Vector2>` to **)** |const| |
  62. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`float<class_float>` | :ref:`get_area<class_Rect2_method_get_area>` **(** **)** |const| |
  64. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`Vector2<class_Vector2>` | :ref:`get_center<class_Rect2_method_get_center>` **(** **)** |const| |
  66. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`Rect2<class_Rect2>` | :ref:`grow<class_Rect2_method_grow>` **(** :ref:`float<class_float>` amount **)** |const| |
  68. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :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| |
  70. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Rect2<class_Rect2>` | :ref:`grow_side<class_Rect2_method_grow_side>` **(** :ref:`int<class_int>` side, :ref:`float<class_float>` amount **)** |const| |
  72. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`bool<class_bool>` | :ref:`has_area<class_Rect2_method_has_area>` **(** **)** |const| |
  74. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`bool<class_bool>` | :ref:`has_point<class_Rect2_method_has_point>` **(** :ref:`Vector2<class_Vector2>` point **)** |const| |
  76. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`Rect2<class_Rect2>` | :ref:`intersection<class_Rect2_method_intersection>` **(** :ref:`Rect2<class_Rect2>` b **)** |const| |
  78. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`bool<class_bool>` | :ref:`intersects<class_Rect2_method_intersects>` **(** :ref:`Rect2<class_Rect2>` b, :ref:`bool<class_bool>` include_borders=false **)** |const| |
  80. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`bool<class_bool>` | :ref:`is_equal_approx<class_Rect2_method_is_equal_approx>` **(** :ref:`Rect2<class_Rect2>` rect **)** |const| |
  82. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`bool<class_bool>` | :ref:`is_finite<class_Rect2_method_is_finite>` **(** **)** |const| |
  84. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`Rect2<class_Rect2>` | :ref:`merge<class_Rect2_method_merge>` **(** :ref:`Rect2<class_Rect2>` b **)** |const| |
  86. +-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. .. rst-class:: classref-reftable-group
  88. Operators
  89. ---------
  90. .. table::
  91. :widths: auto
  92. +---------------------------+-----------------------------------------------------------------------------------------------------------------+
  93. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Rect2_operator_neq_Rect2>` **(** :ref:`Rect2<class_Rect2>` right **)** |
  94. +---------------------------+-----------------------------------------------------------------------------------------------------------------+
  95. | :ref:`Rect2<class_Rect2>` | :ref:`operator *<class_Rect2_operator_mul_Transform2D>` **(** :ref:`Transform2D<class_Transform2D>` right **)** |
  96. +---------------------------+-----------------------------------------------------------------------------------------------------------------+
  97. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Rect2_operator_eq_Rect2>` **(** :ref:`Rect2<class_Rect2>` right **)** |
  98. +---------------------------+-----------------------------------------------------------------------------------------------------------------+
  99. .. rst-class:: classref-section-separator
  100. ----
  101. .. rst-class:: classref-descriptions-group
  102. Property Descriptions
  103. ---------------------
  104. .. _class_Rect2_property_end:
  105. .. rst-class:: classref-property
  106. :ref:`Vector2<class_Vector2>` **end** = ``Vector2(0, 0)``
  107. Ending corner. This is calculated as ``position + size``. Setting this value will change the size.
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_Rect2_property_position:
  111. .. rst-class:: classref-property
  112. :ref:`Vector2<class_Vector2>` **position** = ``Vector2(0, 0)``
  113. Beginning corner. Typically has values lower than :ref:`end<class_Rect2_property_end>`.
  114. .. rst-class:: classref-item-separator
  115. ----
  116. .. _class_Rect2_property_size:
  117. .. rst-class:: classref-property
  118. :ref:`Vector2<class_Vector2>` **size** = ``Vector2(0, 0)``
  119. Size from :ref:`position<class_Rect2_property_position>` to :ref:`end<class_Rect2_property_end>`. Typically, all components are positive.
  120. If the size is negative, you can use :ref:`abs<class_Rect2_method_abs>` to fix it.
  121. .. rst-class:: classref-section-separator
  122. ----
  123. .. rst-class:: classref-descriptions-group
  124. Constructor Descriptions
  125. ------------------------
  126. .. _class_Rect2_constructor_Rect2:
  127. .. rst-class:: classref-constructor
  128. :ref:`Rect2<class_Rect2>` **Rect2** **(** **)**
  129. Constructs a default-initialized **Rect2** with default (zero) values of :ref:`position<class_Rect2_property_position>` and :ref:`size<class_Rect2_property_size>`.
  130. .. rst-class:: classref-item-separator
  131. ----
  132. .. rst-class:: classref-constructor
  133. :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`Rect2<class_Rect2>` from **)**
  134. Constructs a **Rect2** as a copy of the given **Rect2**.
  135. .. rst-class:: classref-item-separator
  136. ----
  137. .. rst-class:: classref-constructor
  138. :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`Rect2i<class_Rect2i>` from **)**
  139. Constructs a **Rect2** from a :ref:`Rect2i<class_Rect2i>`.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. rst-class:: classref-constructor
  143. :ref:`Rect2<class_Rect2>` **Rect2** **(** :ref:`Vector2<class_Vector2>` position, :ref:`Vector2<class_Vector2>` size **)**
  144. Constructs a **Rect2** by position and size.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. rst-class:: classref-constructor
  148. :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 **)**
  149. Constructs a **Rect2** by x, y, width, and height.
  150. .. rst-class:: classref-section-separator
  151. ----
  152. .. rst-class:: classref-descriptions-group
  153. Method Descriptions
  154. -------------------
  155. .. _class_Rect2_method_abs:
  156. .. rst-class:: classref-method
  157. :ref:`Rect2<class_Rect2>` **abs** **(** **)** |const|
  158. Returns a **Rect2** with equivalent position and area, modified so that the top-left corner is the origin and ``width`` and ``height`` are positive.
  159. .. rst-class:: classref-item-separator
  160. ----
  161. .. _class_Rect2_method_encloses:
  162. .. rst-class:: classref-method
  163. :ref:`bool<class_bool>` **encloses** **(** :ref:`Rect2<class_Rect2>` b **)** |const|
  164. Returns ``true`` if this **Rect2** completely encloses another one.
  165. .. rst-class:: classref-item-separator
  166. ----
  167. .. _class_Rect2_method_expand:
  168. .. rst-class:: classref-method
  169. :ref:`Rect2<class_Rect2>` **expand** **(** :ref:`Vector2<class_Vector2>` to **)** |const|
  170. Returns a copy of this **Rect2** expanded to include a given point.
  171. \ **Example:**\
  172. .. tabs::
  173. .. code-tab:: gdscript
  174. # position (-3, 2), size (1, 1)
  175. var rect = Rect2(Vector2(-3, 2), Vector2(1, 1))
  176. # position (-3, -1), size (3, 4), so we fit both rect and Vector2(0, -1)
  177. var rect2 = rect.expand(Vector2(0, -1))
  178. .. code-tab:: csharp
  179. // position (-3, 2), size (1, 1)
  180. var rect = new Rect2(new Vector2(-3, 2), new Vector2(1, 1));
  181. // position (-3, -1), size (3, 4), so we fit both rect and Vector2(0, -1)
  182. var rect2 = rect.Expand(new Vector2(0, -1));
  183. .. rst-class:: classref-item-separator
  184. ----
  185. .. _class_Rect2_method_get_area:
  186. .. rst-class:: classref-method
  187. :ref:`float<class_float>` **get_area** **(** **)** |const|
  188. Returns the area of the **Rect2**. See also :ref:`has_area<class_Rect2_method_has_area>`.
  189. .. rst-class:: classref-item-separator
  190. ----
  191. .. _class_Rect2_method_get_center:
  192. .. rst-class:: classref-method
  193. :ref:`Vector2<class_Vector2>` **get_center** **(** **)** |const|
  194. Returns the center of the **Rect2**, which is equal to :ref:`position<class_Rect2_property_position>` + (:ref:`size<class_Rect2_property_size>` / 2).
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _class_Rect2_method_grow:
  198. .. rst-class:: classref-method
  199. :ref:`Rect2<class_Rect2>` **grow** **(** :ref:`float<class_float>` amount **)** |const|
  200. Returns a copy of the **Rect2** grown by the specified ``amount`` on all sides.
  201. .. rst-class:: classref-item-separator
  202. ----
  203. .. _class_Rect2_method_grow_individual:
  204. .. rst-class:: classref-method
  205. :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|
  206. Returns a copy of the **Rect2** grown by the specified amount on each side individually.
  207. .. rst-class:: classref-item-separator
  208. ----
  209. .. _class_Rect2_method_grow_side:
  210. .. rst-class:: classref-method
  211. :ref:`Rect2<class_Rect2>` **grow_side** **(** :ref:`int<class_int>` side, :ref:`float<class_float>` amount **)** |const|
  212. Returns a copy of the **Rect2** grown by the specified ``amount`` on the specified :ref:`Side<enum_@GlobalScope_Side>`.
  213. .. rst-class:: classref-item-separator
  214. ----
  215. .. _class_Rect2_method_has_area:
  216. .. rst-class:: classref-method
  217. :ref:`bool<class_bool>` **has_area** **(** **)** |const|
  218. Returns ``true`` if the **Rect2** has area, and ``false`` if the **Rect2** is linear, empty, or has a negative :ref:`size<class_Rect2_property_size>`. See also :ref:`get_area<class_Rect2_method_get_area>`.
  219. .. rst-class:: classref-item-separator
  220. ----
  221. .. _class_Rect2_method_has_point:
  222. .. rst-class:: classref-method
  223. :ref:`bool<class_bool>` **has_point** **(** :ref:`Vector2<class_Vector2>` point **)** |const|
  224. 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.
  225. \ **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.
  226. .. rst-class:: classref-item-separator
  227. ----
  228. .. _class_Rect2_method_intersection:
  229. .. rst-class:: classref-method
  230. :ref:`Rect2<class_Rect2>` **intersection** **(** :ref:`Rect2<class_Rect2>` b **)** |const|
  231. Returns the intersection of this **Rect2** and ``b``.
  232. If the rectangles do not intersect, an empty **Rect2** is returned.
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_Rect2_method_intersects:
  236. .. rst-class:: classref-method
  237. :ref:`bool<class_bool>` **intersects** **(** :ref:`Rect2<class_Rect2>` b, :ref:`bool<class_bool>` include_borders=false **)** |const|
  238. Returns ``true`` if the **Rect2** overlaps with ``b`` (i.e. they have at least one point in common).
  239. If ``include_borders`` is ``true``, they will also be considered overlapping if their borders touch, even without intersection.
  240. .. rst-class:: classref-item-separator
  241. ----
  242. .. _class_Rect2_method_is_equal_approx:
  243. .. rst-class:: classref-method
  244. :ref:`bool<class_bool>` **is_equal_approx** **(** :ref:`Rect2<class_Rect2>` rect **)** |const|
  245. Returns ``true`` if this **Rect2** and ``rect`` are approximately equal, by calling ``is_equal_approx`` on each component.
  246. .. rst-class:: classref-item-separator
  247. ----
  248. .. _class_Rect2_method_is_finite:
  249. .. rst-class:: classref-method
  250. :ref:`bool<class_bool>` **is_finite** **(** **)** |const|
  251. Returns ``true`` if this **Rect2** is finite, by calling :ref:`@GlobalScope.is_finite<class_@GlobalScope_method_is_finite>` on each component.
  252. .. rst-class:: classref-item-separator
  253. ----
  254. .. _class_Rect2_method_merge:
  255. .. rst-class:: classref-method
  256. :ref:`Rect2<class_Rect2>` **merge** **(** :ref:`Rect2<class_Rect2>` b **)** |const|
  257. Returns a larger **Rect2** that contains this **Rect2** and ``b``.
  258. .. rst-class:: classref-section-separator
  259. ----
  260. .. rst-class:: classref-descriptions-group
  261. Operator Descriptions
  262. ---------------------
  263. .. _class_Rect2_operator_neq_Rect2:
  264. .. rst-class:: classref-operator
  265. :ref:`bool<class_bool>` **operator !=** **(** :ref:`Rect2<class_Rect2>` right **)**
  266. Returns ``true`` if the rectangles are not equal.
  267. \ **Note:** Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Rect2_method_is_equal_approx>` instead, which is more reliable.
  268. .. rst-class:: classref-item-separator
  269. ----
  270. .. _class_Rect2_operator_mul_Transform2D:
  271. .. rst-class:: classref-operator
  272. :ref:`Rect2<class_Rect2>` **operator *** **(** :ref:`Transform2D<class_Transform2D>` right **)**
  273. Inversely transforms (multiplies) the **Rect2** by the given :ref:`Transform2D<class_Transform2D>` transformation matrix.
  274. .. rst-class:: classref-item-separator
  275. ----
  276. .. _class_Rect2_operator_eq_Rect2:
  277. .. rst-class:: classref-operator
  278. :ref:`bool<class_bool>` **operator ==** **(** :ref:`Rect2<class_Rect2>` right **)**
  279. Returns ``true`` if the rectangles are exactly equal.
  280. \ **Note:** Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Rect2_method_is_equal_approx>` instead, which is more reliable.
  281. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  282. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  283. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  284. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  285. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  286. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  287. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`