2.6 KiB
2.6 KiB
Rect / RectInt / Viewport
2D 矩形和视口结构体。
头文件
#include <XCEngine/Math/Rect.h>
命名空间
XCEngine::Math
Rect - 浮点矩形
struct Rect {
float x = 0.0f; // 左边界
float y = 0.0f; // 上边界
float width = 0.0f;
float height = 0.0f;
};
构造
Rect(float x, float y, float w, float h)
边界访问
| 方法 | 返回值 | 描述 |
|---|---|---|
GetLeft() |
float |
x |
GetRight() |
float |
x + width |
GetTop() |
float |
y |
GetBottom() |
float |
y + height |
GetPosition() |
Vector2 |
(x, y) |
GetSize() |
Vector2 |
(width, height) |
GetCenter() |
Vector2 |
中心点 |
检测方法
| 方法 | 返回值 | 描述 |
|---|---|---|
Contains(px, py) |
bool |
点是否在矩形内 |
Contains(point) |
bool |
Vector2 点检测 |
Intersects(other) |
bool |
与另一矩形相交 |
静态方法
| 方法 | 返回值 | 描述 |
|---|---|---|
Intersect(a, b) |
Rect |
两矩形交集 |
Union(a, b) |
Rect |
两矩形并集 |
设置方法
| 方法 | 返回值 | 描述 |
|---|---|---|
Set(x, y, w, h) |
void |
设置所有值 |
SetPosition(x, y) |
void |
设置位置 |
SetPosition(Vector2) |
void |
设置位置 |
RectInt - 整数矩形
struct RectInt {
int32_t x = 0;
int32_t y = 0;
int32_t width = 0;
int32_t height = 0;
};
与 Rect 类似,但使用 int32_t 类型。
边界访问
GetLeft/Right/Top/Bottom/GetPosition/GetSize/GetCenter - 返回 int32_t 或 Vector2。
检测
| 方法 | 返回值 | 描述 |
|---|---|---|
Contains(px, py) |
bool |
整数点检测 |
Intersects(other) |
bool |
相交检测 |
Viewport - 视口
用于渲染视口和屏幕映射。
struct Viewport {
float x = 0.0f;
float y = 0.0f;
float width = 0.0f;
float height = 0.0f;
float minDepth = 0.0f;
float maxDepth = 1.0f;
};
构造
Viewport(float x, float y, float w, float h)Viewport(float x, float y, float w, float h, float minD, float maxD)
方法
| 方法 | 返回值 | 描述 |
|---|---|---|
GetAspectRatio() |
float |
宽高比 (width/height) |
GetRect() |
Rect |
转换为 Rect |
使用示例
// Rect
Rect screenRect(0.0f, 0.0f, 1920.0f, 1080.0f);
if (screenRect.Contains(mouseX, mouseY)) { ... }
// 矩形相交
Rect overlap = Rect::Intersect(rectA, rectB);
// Viewport
Viewport viewport(0, 0, 1920, 1080);
float aspect = viewport.GetAspectRatio(); // 16:9 = 1.78