]> git.localhorst.tv Git - sdl-test7.git/blob - src/geometry/AABB.h
imported current version
[sdl-test7.git] / src / geometry / AABB.h
1 /*
2  * AABB.h
3  *
4  *  Created on: Apr 10, 2012
5  *      Author: holy
6  */
7
8 #ifndef GEOMETRY_AABB_H_
9 #define GEOMETRY_AABB_H_
10
11 #include "Shape.h"
12
13 #include <ostream>
14 #include <SDL/SDL.h>
15
16
17 namespace geometry {
18
19 class AABB
20 : public Shape {
21
22         public:
23                 AABB(void) : leftTop(), rightBottom() { };
24                 explicit AABB(const Vector &size) : leftTop(), rightBottom(size) { };
25                 AABB(const Vector &position, const Vector &size)
26                 : leftTop(position), rightBottom(position + size) { };
27                 virtual ~AABB(void) { };
28
29         public:
30                 Scalar Left(void) const { return leftTop.X(); };
31                 Scalar Top(void) const { return leftTop.Y(); };
32                 Scalar Right(void) const { return rightBottom.X(); };
33                 Scalar Bottom(void) const { return rightBottom.Y(); };
34
35                 Scalar X(void) const { return Left(); };
36                 Scalar Y(void) const { return Top(); };
37                 Scalar Width(void) const { return Right() - Left(); };
38                 Scalar Height(void) const { return Bottom() - Top(); };
39
40         public:
41                 virtual bool Overlaps(const Shape &other, Vector &normal) const;
42                 virtual bool Overlaps(const AABB &other, Vector &normal) const;
43                 virtual bool Overlaps(const Circle &other, Vector &normal) const;
44                 virtual bool Overlaps(const FakeLens &, Vector &normal) const;
45
46                 bool VerticalOverlap(const AABB &other) const {
47                         if (Bottom() < other.Top()) return false;
48                         if (other.Bottom() < Top()) return false;
49                         return true;
50                 };
51                 bool HorizontalOverlap(const AABB &other) const {
52                         if (Right() < other.Left()) return false;
53                         if (other.Right() < Left()) return false;
54                         return true;
55                 };
56
57         public:
58                 void Move(const Vector &delta) {
59                         leftTop += delta;
60                         rightBottom += delta;
61                 };
62
63                 void SetBounds(const Vector &position, const Vector &size) {
64                         leftTop = position;
65                         rightBottom = position + size;
66                 };
67                 void SetPosition(const Vector &position) {
68                         rightBottom = position + (rightBottom - leftTop);
69                         leftTop = position;
70                 };
71                 void SetSize(const Vector &size) {
72                         rightBottom = leftTop + size;
73                 };
74
75                 virtual Vector Center(void) const {
76                         return leftTop + (rightBottom - leftTop) / 2;
77                 };
78
79                 virtual std::ostream &Write(std::ostream &out) const;
80
81         public:
82                 void WriteRect(SDL_Rect &rect) const {
83                         rect.x = X();
84                         rect.y = Y();
85                         rect.w = Width();
86                         rect.h = Height();
87                 };
88
89         private:
90                 Vector leftTop, rightBottom;
91
92 };
93
94 }
95
96 #endif /* GEOMETRY_AABB_H_ */