2026-03-20 02:35:15 +08:00
|
|
|
|
# Rect::Contains
|
|
|
|
|
|
|
|
|
|
|
|
判断点是否在矩形内部。
|
|
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
|
bool Contains(float px, float py) const;
|
|
|
|
|
|
bool Contains(const Vector2& point) const;
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
矩形使用左上位坐标系,内部定义为:x >= left && x < right && y >= top && y < bottom。即左边界和上边界在内部,右边界和下边界在外部。
|
|
|
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
- `px` - 点的 x 坐标
|
|
|
|
|
|
- `py` - 点的 y 坐标
|
|
|
|
|
|
- `point` - 要判断的点(Vector2 类型)
|
|
|
|
|
|
|
|
|
|
|
|
**返回:** 点在矩形内部返回 true,否则返回 false
|
|
|
|
|
|
|
|
|
|
|
|
**线程安全:** ✅
|
|
|
|
|
|
|
|
|
|
|
|
**复杂度:** O(1)
|
|
|
|
|
|
|
|
|
|
|
|
**示例:**
|
|
|
|
|
|
|
|
|
|
|
|
```cpp
|
2026-03-26 02:41:00 +08:00
|
|
|
|
#include "XCEngine/Core/Math/Rect.h"
|
2026-03-20 02:35:15 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace XCEngine::Math;
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
Rect rect(10.0f, 20.0f, 100.0f, 50.0f);
|
|
|
|
|
|
|
|
|
|
|
|
if (rect.Contains(50.0f, 30.0f)) {
|
|
|
|
|
|
std::cout << "Point (50, 30) is inside\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Vector2 point(150.0f, 100.0f);
|
|
|
|
|
|
if (!rect.Contains(point)) {
|
|
|
|
|
|
std::cout << "Point (150, 100) is outside\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
|
|
|
|
|
- [`Rect::Intersects`](intersects.md) - 判断是否相交
|
|
|
|
|
|
- [Rect 总览](rect.md)
|