]> Shamusworld >> Repos - architektonas/blob - src/rect.cpp
54f44ddf73abcb404c95d1b6d397674a90a16b8c
[architektonas] / src / rect.cpp
1 //
2 // rect.cpp: Rectangle object implementation
3 //
4 // Part of the Architektonas Project
5 // (C) 2016 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  11/10/2016  Created this file
13 //
14
15 #include "rect.h"
16 //#include <stdio.h>
17
18 Rect::Rect(): l(0), r(0), t(0), b(0)
19 {
20 }
21
22 Rect::Rect(double ll, double rr, double tt, double bb):
23         l(ll), r(rr), t(tt), b(bb)
24 {
25         Normalize();
26 }
27
28 Rect::Rect(Point tl, Point br): l(tl.x), r(br.x), t(tl.y), b(br.y)
29 {
30         Normalize();
31 }
32
33 Rect & Rect::operator*=(double scale)
34 {
35         l *= scale;
36         r *= scale;
37         t *= scale;
38         b *= scale;
39
40         return *this;
41 }
42
43 Rect & Rect::operator|=(Rect r2)
44 {
45 //printf("operatore|=\nthis = (%lf, %lf, %lf, %lf), r = (%lf, %lf, %lf, %lf)\n", l, t, r, b, r2.l, r2.t, r2.r, r2.b);
46         if (r2.l < l)
47                 l = r2.l;
48
49         if (r2.r > r)
50                 r = r2.r;
51
52         if (r2.t > t)
53                 t = r2.t;
54
55         if (r2.b < b)
56                 b = r2.b;
57
58         return *this;
59 }
60
61 void Rect::Normalize(void)
62 {
63         if (l > r)
64         {
65                 double x = l;
66                 l = r;
67                 r = x;
68         }
69
70         if (b > t)
71         {
72                 double x = b;
73                 b = t;
74                 t = x;
75         }
76 }
77
78 void Rect::Translate(Point p)
79 {
80         l += p.x;
81         r += p.x;
82         t += p.y;
83         b += p.y;
84 }
85
86 void Rect::Expand(double amt)
87 {
88         l -= amt;
89         r += amt;
90         t += amt;
91         b -= amt;
92 }
93
94 double Rect::Width(void)
95 {
96         return r - l;
97 }
98
99 double Rect::Height(void)
100 {
101         return t - b;
102 }
103
104 bool Rect::Contains(Point p)
105 {
106         return ((p.x >= l) && (p.x <= r) && (p.y >= b) && (p.y <= t) ? true : false);
107 }
108
109 Point Rect::TopLeft(void)
110 {
111         return Point(l, t);
112 }
113
114 Point Rect::TopRight(void)
115 {
116         return Point(r, t);
117 }
118
119 Point Rect::BottomLeft(void)
120 {
121         return Point(l, b);
122 }
123
124 Point Rect::BottomRight(void)
125 {
126         return Point(r, b);
127 }