-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAABB.h
89 lines (74 loc) · 1.52 KB
/
AABB.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include "stdafx.h"
class Object;
class AABB
{
public:
float minX;
float minY;
float maxX;
float maxY;
float surfaceArea;
Object *object;
AABB() {
minX = 0;
minY = 0;
maxX = 0;
maxY = 0;
surfaceArea = 0;
}
AABB(float minx, float miny, float maxx, float maxy) {
minX = minx;
minY = miny;
maxX = maxx;
maxY = maxy;
surfaceArea = CalculateSurfaceArea();
}
AABB(float minx, float miny, float maxx, float maxy, float srfcarea) {
minX = minx;
minY = miny;
maxX = maxx;
maxY = maxy;
surfaceArea = srfcarea;
}
bool overlaps(const AABB& other) const
{
return maxX > other.minX &&
minX < other.maxX &&
maxY > other.minY &&
minY < other.maxY;
}
bool containsAABB(const AABB& other) const
{
return maxX >= other.maxX &&
minX <= other.maxX &&
maxY >= other.minY &&
minY <= other.maxY;
}
bool containsPoint(const sf::Vector2f point) const
{
return maxX >= point.x &&
minX <= point.x &&
maxY >= point.y &&
minY <= point.y;
}
AABB merge(const AABB& other) const
{
return AABB(
std::min(minX, other.minX), std::min(minY, other.minY),
std::max(maxX, other.maxX), std::max(maxY, other.maxY)
);
}
AABB intersection(const AABB& other) const
{
return AABB(
std::max(minX, other.minX), std::max(minY, other.minY),
std::min(maxX, other.maxX), std::min(maxY, other.maxY)
);
}
float CalculateSurfaceArea() {
return (getWidth()*getHeight());
}
float getWidth() const { return maxX - minX; }
float getHeight() const { return maxY - minY; }
};