]> git.localhorst.tv Git - sdl-test7.git/blob - src/geometry/AABB.cpp
e40c69fc40daab3b0c5245606dd893eb4db98c9e
[sdl-test7.git] / src / geometry / AABB.cpp
1 /*
2  * AABB.cpp
3  *
4  *  Created on: Apr 20, 2012
5  *      Author: holy
6  */
7
8 #include "AABB.h"
9
10 #include "Circle.h"
11 #include "FakeLens.h"
12
13 namespace geometry {
14
15 bool AABB::Overlaps(const Shape &other, Vector &normal) const {
16         if (other.Overlaps(*this, normal)) {
17                 normal *= -1;
18                 return true;
19         } else {
20                 return false;
21         }
22 }
23
24 bool AABB::Overlaps(const AABB &other, Vector &normal) const {
25         if (Bottom() <= other.Top()) return false;
26         if (other.Bottom() <= Top()) return false;
27         if (Right() <= other.Left()) return false;
28         if (other.Right() <= Left()) return false;
29
30         if (HorizontalOverlap(other)) {
31                 if (Top() < other.Top()) {
32                         normal = Vector(0, -1);
33                 } else {
34                         normal = Vector(0, 1);
35                 }
36         } else {
37                 if (Left() < other.Left()) {
38                         normal = Vector(-1, 0);
39                 } else {
40                         normal = Vector(1, 0);
41                 }
42         }
43
44         return true;
45 }
46
47 bool AABB::Overlaps(const Circle &other, Vector &normal) const {
48         if (other.Overlaps(*this, normal)) {
49                 normal *= -1;
50                 return true;
51         } else {
52                 return false;
53         }
54 }
55
56 bool AABB::Overlaps(const FakeLens &other, Vector &normal) const {
57         if (other.Overlaps(*this, normal)) {
58                 normal *= -1;
59                 return true;
60         } else {
61                 return false;
62         }
63 }
64
65 std::ostream &AABB::Write(std::ostream &out) const {
66         return out << "AABB(" << Width() << 'x' << Height() << '+' << X() << '+' << Y() << ')';
67 }
68
69 } /* namespace geometry */