# Rect **命名空间**: `XCEngine::Math` **类型**: `struct` **头文件**: `XCEngine/Math/Rect.h` **描述**: 二维矩形,用于表示平面上的矩形区域 ## 概述 Rect 结构体表示一个二维矩形,由位置 `(x, y)` 和尺寸 `(width, height)` 组成。矩形使用左上位坐标系,x 向右增加,y 向下增加。 ## 结构体成员 | 成员 | 类型 | 描述 | 默认值 | |------|------|------|--------| | `x` | `float` | 矩形左上角 X 坐标 | `0.0f` | | `y` | `float` | 矩形左上角 Y 坐标 | `0.0f` | | `width` | `float` | 矩形宽度 | `0.0f` | | `height` | `float` | 矩形高度 | `0.0f` | ## 公共方法 | 方法 | 描述 | |------|------| | [`GetLeft`](getleft.md) | 获取矩形左边界 | | [`GetRight`](getright.md) | 获取矩形右边界 | | [`GetTop`](gettop.md) | 获取矩形上边界 | | [`GetBottom`](getbottom.md) | 获取矩形下边界 | | [`GetPosition`](getposition.md) | 获取矩形位置 | | [`GetSize`](getsize.md) | 获取矩形尺寸 | | [`GetCenter`](getcenter.md) | 获取矩形中心点 | | [`Contains`](contains.md) | 判断点是否在矩形内 | | [`Intersects`](intersects.md) | 判断是否与另一矩形相交 | | [`Intersect`](intersect.md) | 计算两矩形交集(静态方法) | | [`Union`](union.md) | 计算两矩形并集(静态方法) | | [`Set`](set.md) | 设置矩形所有属性 | | [`SetPosition`](setposition.md) | 设置矩形位置 | ## 使用示例 ```cpp #include "XCEngine/Math/Rect.h" #include "XCEngine/Math/Vector2.h" #include using namespace XCEngine::Math; int main() { Rect rect(10.0f, 20.0f, 100.0f, 50.0f); std::cout << "Position: (" << rect.x << ", " << rect.y << ")\n"; std::cout << "Size: " << rect.width << " x " << rect.height << "\n"; std::cout << "Left: " << rect.GetLeft() << ", Right: " << rect.GetRight() << "\n"; std::cout << "Top: " << rect.GetTop() << ", Bottom: " << rect.GetBottom() << "\n"; std::cout << "Center: (" << rect.GetCenter().x << ", " << rect.GetCenter().y << ")\n"; Vector2 point(50.0f, 30.0f); if (rect.Contains(point)) { std::cout << "Point is inside rect\n"; } Rect other(60.0f, 40.0f, 100.0f, 50.0f); if (rect.Intersects(other)) { std::cout << "Rects intersect\n"; } Rect intersection = Rect::Intersect(rect, other); std::cout << "Intersection: " << intersection.width << " x " << intersection.height << "\n"; return 0; } ``` ## 相关文档 - [`RectInt`](rectint.md) - 整数矩形 - [`Viewport`](viewport.md) - 视口 - [`Vector2`](../vector2/vector2.md) - 二维向量