2.6 KiB
2.6 KiB
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 |
获取矩形左边界 |
GetRight |
获取矩形右边界 |
GetTop |
获取矩形上边界 |
GetBottom |
获取矩形下边界 |
GetPosition |
获取矩形位置 |
GetSize |
获取矩形尺寸 |
GetCenter |
获取矩形中心点 |
Contains |
判断点是否在矩形内 |
Intersects |
判断是否与另一矩形相交 |
Intersect |
计算两矩形交集(静态方法) |
Union |
计算两矩形并集(静态方法) |
Set |
设置矩形所有属性 |
SetPosition |
设置矩形位置 |
使用示例
#include "XCEngine/Math/Rect.h"
#include "XCEngine/Math/Vector2.h"
#include <iostream>
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;
}