-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArea.cpp
More file actions
72 lines (63 loc) · 1.5 KB
/
Area.cpp
File metadata and controls
72 lines (63 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Area.h"
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <algorithm>
// 获取给定矩形的右下角坐标
static inline sf::Vector2f __getLowerRight(sf::FloatRect rect)
{
return {
rect.left + rect.width,
rect.top + rect.height
};
}
// 根据给定的左上角和右下角计算矩形的尺寸
static inline sf::Vector2f __getSize(sf::Vector2f upperLeft, sf::Vector2f lowerRight)
{
return {
lowerRight.x - upperLeft.x,
lowerRight.y - upperLeft.y
};
}
Area::Area(sf::Vector2f pos, sf::Vector2f size)
{
this->x1 = pos.x;
this->y1 = pos.y;
this->x2 = pos.x + size.x - 1;
this->y2 = pos.y + size.y - 1;
}
Area::Area(float x, float y, float w, float h)
{
*this = Area(
{ x, y },
{ w, h }
);
}
sf::Vector2f Area::getSize()
{
return __getSize({ x1, y1 }, { x2, y2 });
}
const sf::Vector2f Area::getAbsPos(sf::Vector2f pos)
{
return { this->x1 + pos.x, this->y1 + pos.y };
}
const sf::Vector2f Area::getRelPos(sf::Vector2f pos)
{
return { pos.x - this->x1, pos.y - this->y1 };
}
const sf::FloatRect Area::getOverlap(sf::FloatRect rect)
{
float right = __getLowerRight(rect).x;
float bottom = __getLowerRight(rect).y;
sf::Vector2f upperLeft(
std::max(this->x1, rect.left),
std::max(this->y1, rect.top)
);
sf::Vector2f size = __getSize(
upperLeft,
{
std::min(this->x1, right),
std::min(this->y1, bottom)
}
);
return sf::FloatRect(upperLeft, size);
}