:github_url: hide .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Rect2i.xml. .. _class_Rect2i: Rect2i ====== A 2D axis-aligned bounding box using integer coordinates. .. rst-class:: classref-introduction-group Description ----------- The **Rect2i** built-in :ref:`Variant` type represents an axis-aligned rectangle in a 2D space, using integer coordinates. It is defined by its :ref:`position` and :ref:`size`, which are :ref:`Vector2i`. Because it does not rotate, it is frequently used for fast overlap tests (see :ref:`intersects()`). For floating-point coordinates, see :ref:`Rect2`. \ **Note:** Negative values for :ref:`size` are not supported. With negative size, most **Rect2i** methods do not work correctly. Use :ref:`abs()` to get an equivalent **Rect2i** with a non-negative size. \ **Note:** In a boolean context, a **Rect2i** evaluates to ``false`` if both :ref:`position` and :ref:`size` are zero (equal to :ref:`Vector2i.ZERO`). Otherwise, it always evaluates to ``true``. .. note:: There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information. .. rst-class:: classref-introduction-group Tutorials --------- - :doc:`Math documentation index <../tutorials/math/index>` - :doc:`Vector math <../tutorials/math/vector_math>` .. rst-class:: classref-reftable-group Properties ---------- .. table:: :widths: auto +---------------------------------+-------------------------------------------------+--------------------+ | :ref:`Vector2i` | :ref:`end` | ``Vector2i(0, 0)`` | +---------------------------------+-------------------------------------------------+--------------------+ | :ref:`Vector2i` | :ref:`position` | ``Vector2i(0, 0)`` | +---------------------------------+-------------------------------------------------+--------------------+ | :ref:`Vector2i` | :ref:`size` | ``Vector2i(0, 0)`` | +---------------------------------+-------------------------------------------------+--------------------+ .. rst-class:: classref-reftable-group Constructors ------------ .. table:: :widths: auto +-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`Rect2i`\ (\ ) | +-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`Rect2i`\ (\ from\: :ref:`Rect2i`\ ) | +-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`Rect2i`\ (\ from\: :ref:`Rect2`\ ) | +-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`Rect2i`\ (\ position\: :ref:`Vector2i`, size\: :ref:`Vector2i`\ ) | +-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`Rect2i`\ (\ x\: :ref:`int`, y\: :ref:`int`, width\: :ref:`int`, height\: :ref:`int`\ ) | +-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group Methods ------- .. table:: :widths: auto +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`abs`\ (\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`encloses`\ (\ b\: :ref:`Rect2i`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`expand`\ (\ to\: :ref:`Vector2i`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_area`\ (\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Vector2i` | :ref:`get_center`\ (\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`grow`\ (\ amount\: :ref:`int`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`grow_individual`\ (\ left\: :ref:`int`, top\: :ref:`int`, right\: :ref:`int`, bottom\: :ref:`int`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`grow_side`\ (\ side\: :ref:`int`, amount\: :ref:`int`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_area`\ (\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_point`\ (\ point\: :ref:`Vector2i`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`intersection`\ (\ b\: :ref:`Rect2i`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`intersects`\ (\ b\: :ref:`Rect2i`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Rect2i` | :ref:`merge`\ (\ b\: :ref:`Rect2i`\ ) |const| | +---------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-reftable-group Operators --------- .. table:: :widths: auto +-------------------------+-------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator !=`\ (\ right\: :ref:`Rect2i`\ ) | +-------------------------+-------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`operator ==`\ (\ right\: :ref:`Rect2i`\ ) | +-------------------------+-------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Property Descriptions --------------------- .. _class_Rect2i_property_end: .. rst-class:: classref-property :ref:`Vector2i` **end** = ``Vector2i(0, 0)`` :ref:`🔗` The ending point. This is usually the bottom-right corner of the rectangle, and is equivalent to ``position + size``. Setting this point affects the :ref:`size`. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_property_position: .. rst-class:: classref-property :ref:`Vector2i` **position** = ``Vector2i(0, 0)`` :ref:`🔗` The origin point. This is usually the top-left corner of the rectangle. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_property_size: .. rst-class:: classref-property :ref:`Vector2i` **size** = ``Vector2i(0, 0)`` :ref:`🔗` The rectangle's width and height, starting from :ref:`position`. Setting this value also affects the :ref:`end` point. \ **Note:** It's recommended setting the width and height to non-negative values, as most methods in Godot assume that the :ref:`position` is the top-left corner, and the :ref:`end` is the bottom-right corner. To get an equivalent rectangle with non-negative size, use :ref:`abs()`. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Constructor Descriptions ------------------------ .. _class_Rect2i_constructor_Rect2i: .. rst-class:: classref-constructor :ref:`Rect2i` **Rect2i**\ (\ ) :ref:`🔗` Constructs a **Rect2i** with its :ref:`position` and :ref:`size` set to :ref:`Vector2i.ZERO`. .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Rect2i` **Rect2i**\ (\ from\: :ref:`Rect2i`\ ) Constructs a **Rect2i** as a copy of the given **Rect2i**. .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Rect2i` **Rect2i**\ (\ from\: :ref:`Rect2`\ ) Constructs a **Rect2i** from a :ref:`Rect2`. The floating-point coordinates are truncated. .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Rect2i` **Rect2i**\ (\ position\: :ref:`Vector2i`, size\: :ref:`Vector2i`\ ) Constructs a **Rect2i** by ``position`` and ``size``. .. rst-class:: classref-item-separator ---- .. rst-class:: classref-constructor :ref:`Rect2i` **Rect2i**\ (\ x\: :ref:`int`, y\: :ref:`int`, width\: :ref:`int`, height\: :ref:`int`\ ) Constructs a **Rect2i** by setting its :ref:`position` to (``x``, ``y``), and its :ref:`size` to (``width``, ``height``). .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Method Descriptions ------------------- .. _class_Rect2i_method_abs: .. rst-class:: classref-method :ref:`Rect2i` **abs**\ (\ ) |const| :ref:`🔗` Returns a **Rect2i** equivalent to this rectangle, with its width and height modified to be non-negative values, and with its :ref:`position` being the top-left corner of the rectangle. .. tabs:: .. code-tab:: gdscript var rect = Rect2i(25, 25, -100, -50) var absolute = rect.abs() # absolute is Rect2i(-75, -25, 100, 50) .. code-tab:: csharp var rect = new Rect2I(25, 25, -100, -50); var absolute = rect.Abs(); // absolute is Rect2I(-75, -25, 100, 50) \ **Note:** It's recommended to use this method when :ref:`size` is negative, as most other methods in Godot assume that the :ref:`position` is the top-left corner, and the :ref:`end` is the bottom-right corner. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_encloses: .. rst-class:: classref-method :ref:`bool` **encloses**\ (\ b\: :ref:`Rect2i`\ ) |const| :ref:`🔗` Returns ``true`` if this **Rect2i** completely encloses another one. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_expand: .. rst-class:: classref-method :ref:`Rect2i` **expand**\ (\ to\: :ref:`Vector2i`\ ) |const| :ref:`🔗` Returns a copy of this rectangle expanded to align the edges with the given ``to`` point, if necessary. .. tabs:: .. code-tab:: gdscript var rect = Rect2i(0, 0, 5, 2) rect = rect.expand(Vector2i(10, 0)) # rect is Rect2i(0, 0, 10, 2) rect = rect.expand(Vector2i(-5, 5)) # rect is Rect2i(-5, 0, 15, 5) .. code-tab:: csharp var rect = new Rect2I(0, 0, 5, 2); rect = rect.Expand(new Vector2I(10, 0)); // rect is Rect2I(0, 0, 10, 2) rect = rect.Expand(new Vector2I(-5, 5)); // rect is Rect2I(-5, 0, 15, 5) .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_get_area: .. rst-class:: classref-method :ref:`int` **get_area**\ (\ ) |const| :ref:`🔗` Returns the rectangle's area. This is equivalent to ``size.x * size.y``. See also :ref:`has_area()`. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_get_center: .. rst-class:: classref-method :ref:`Vector2i` **get_center**\ (\ ) |const| :ref:`🔗` Returns the center point of the rectangle. This is the same as ``position + (size / 2)``. \ **Note:** If the :ref:`size` is odd, the result will be rounded towards :ref:`position`. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_grow: .. rst-class:: classref-method :ref:`Rect2i` **grow**\ (\ amount\: :ref:`int`\ ) |const| :ref:`🔗` Returns a copy of this rectangle extended on all sides by the given ``amount``. A negative ``amount`` shrinks the rectangle instead. See also :ref:`grow_individual()` and :ref:`grow_side()`. .. tabs:: .. code-tab:: gdscript var a = Rect2i(4, 4, 8, 8).grow(4) # a is Rect2i(0, 0, 16, 16) var b = Rect2i(0, 0, 8, 4).grow(2) # b is Rect2i(-2, -2, 12, 8) .. code-tab:: csharp var a = new Rect2I(4, 4, 8, 8).Grow(4); // a is Rect2I(0, 0, 16, 16) var b = new Rect2I(0, 0, 8, 4).Grow(2); // b is Rect2I(-2, -2, 12, 8) .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_grow_individual: .. rst-class:: classref-method :ref:`Rect2i` **grow_individual**\ (\ left\: :ref:`int`, top\: :ref:`int`, right\: :ref:`int`, bottom\: :ref:`int`\ ) |const| :ref:`🔗` Returns a copy of this rectangle with its ``left``, ``top``, ``right``, and ``bottom`` sides extended by the given amounts. Negative values shrink the sides, instead. See also :ref:`grow()` and :ref:`grow_side()`. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_grow_side: .. rst-class:: classref-method :ref:`Rect2i` **grow_side**\ (\ side\: :ref:`int`, amount\: :ref:`int`\ ) |const| :ref:`🔗` Returns a copy of this rectangle with its ``side`` extended by the given ``amount`` (see :ref:`Side` constants). A negative ``amount`` shrinks the rectangle, instead. See also :ref:`grow()` and :ref:`grow_individual()`. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_has_area: .. rst-class:: classref-method :ref:`bool` **has_area**\ (\ ) |const| :ref:`🔗` Returns ``true`` if this rectangle has positive width and height. See also :ref:`get_area()`. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_has_point: .. rst-class:: classref-method :ref:`bool` **has_point**\ (\ point\: :ref:`Vector2i`\ ) |const| :ref:`🔗` Returns ``true`` if the rectangle contains the given ``point``. By convention, points on the right and bottom edges are **not** included. \ **Note:** This method is not reliable for **Rect2i** with a *negative* :ref:`size`. Use :ref:`abs()` first to get a valid rectangle. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_intersection: .. rst-class:: classref-method :ref:`Rect2i` **intersection**\ (\ b\: :ref:`Rect2i`\ ) |const| :ref:`🔗` Returns the intersection between this rectangle and ``b``. If the rectangles do not intersect, returns an empty **Rect2i**. .. tabs:: .. code-tab:: gdscript var a = Rect2i(0, 0, 5, 10) var b = Rect2i(2, 0, 8, 4) var c = a.intersection(b) # c is Rect2i(2, 0, 3, 4) .. code-tab:: csharp var a = new Rect2I(0, 0, 5, 10); var b = new Rect2I(2, 0, 8, 4); var c = rect1.Intersection(rect2); // c is Rect2I(2, 0, 3, 4) \ **Note:** If you only need to know whether two rectangles are overlapping, use :ref:`intersects()`, instead. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_intersects: .. rst-class:: classref-method :ref:`bool` **intersects**\ (\ b\: :ref:`Rect2i`\ ) |const| :ref:`🔗` Returns ``true`` if this rectangle overlaps with the ``b`` rectangle. The edges of both rectangles are excluded. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_method_merge: .. rst-class:: classref-method :ref:`Rect2i` **merge**\ (\ b\: :ref:`Rect2i`\ ) |const| :ref:`🔗` Returns a **Rect2i** that encloses both this rectangle and ``b`` around the edges. See also :ref:`encloses()`. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Operator Descriptions --------------------- .. _class_Rect2i_operator_neq_Rect2i: .. rst-class:: classref-operator :ref:`bool` **operator !=**\ (\ right\: :ref:`Rect2i`\ ) :ref:`🔗` Returns ``true`` if the :ref:`position` or :ref:`size` of both rectangles are not equal. .. rst-class:: classref-item-separator ---- .. _class_Rect2i_operator_eq_Rect2i: .. rst-class:: classref-operator :ref:`bool` **operator ==**\ (\ right\: :ref:`Rect2i`\ ) :ref:`🔗` Returns ``true`` if both :ref:`position` and :ref:`size` of the rectangles are equal, respectively. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)` .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)` .. |void| replace:: :abbr:`void (No return value.)`