]> Shamusworld >> Repos - architektonas/blob - src/rect.cpp
693cf31582e34b3533bb1df7bbd73a9a60b71fa6
[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
19 Rect::Rect(): l(0), r(0), t(0), b(0)
20 {
21 }
22
23
24 Rect::Rect(double ll, double rr, double tt, double bb):
25         l(ll), r(rr), t(tt), b(bb)
26 {
27         Normalize();
28 }
29
30
31 Rect::Rect(Point tl, Point br): l(tl.x), r(br.x), t(tl.y), b(br.y)
32 {
33         Normalize();
34 }
35
36
37 Rect & Rect::operator*=(double scale)
38 {
39         l *= scale;
40         r *= scale;
41         t *= scale;
42         b *= scale;
43         return *this;
44 }
45
46
47 Rect & Rect::operator|=(Rect r2)
48 {
49 //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);
50         if (r2.l < l)
51                 l = r2.l;
52
53         if (r2.r > r)
54                 r = r2.r;
55
56         if (r2.t > t)
57                 t = r2.t;
58
59         if (r2.b < b)
60                 b = r2.b;
61
62         return *this;
63 }
64
65
66 void Rect::Normalize(void)
67 {
68         if (l > r)
69         {
70                 double x = l;
71                 l = r;
72                 r = x;
73         }
74
75         if (b > t)
76         {
77                 double x = b;
78                 b = t;
79                 t = x;
80         }
81 }
82
83
84 void Rect::Translate(Point p)
85 {
86         l += p.x;
87         r += p.x;
88         t += p.y;
89         b += p.y;
90 }
91
92
93 void Rect::Expand(double amt)
94 {
95         l -= amt;
96         r += amt;
97         t += amt;
98         b -= amt;
99 }
100
101
102 double Rect::Width(void)
103 {
104         return r - l;
105 }
106
107
108 double Rect::Height(void)
109 {
110         return t - b;
111 }
112
113
114 bool Rect::Contains(Point p)
115 {
116         return ((p.x >= l) && (p.x <= r) && (p.y >= b) && (p.y <= t) ? true : false);
117 }
118
119
120 Point Rect::TopLeft(void)
121 {
122         return Point(l, t);
123 }
124
125
126 Point Rect::BottomRight(void)
127 {
128         return Point(r, b);
129 }
130