]> Shamusworld >> Repos - architektonas/blob - src/rect.cpp
Preliminary support for Polylines.
[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
16 #include "rect.h"
17 #include <math.h>
18
19 Rect::Rect(): l(0), r(0), t(0), b(0)
20 {
21 }
22
23 Rect::Rect(double tt, double ll, double bb, double rr):
24         l(ll), r(rr), t(tt), b(bb)
25 {
26         Normalize();
27 }
28
29 Rect::Rect(Point tl, Point br): l(tl.x), r(br.x), t(tl.y), b(br.y)
30 {
31         Normalize();
32 }
33
34 Rect::Rect(Point p): l(p.x), r(p.x), t(p.y), b(p.y)
35 {
36 }
37
38 Rect & Rect::operator*=(double scale)
39 {
40         l *= scale;
41         r *= scale;
42         t *= scale;
43         b *= scale;
44
45         return *this;
46 }
47
48 Rect & Rect::operator|=(Rect r2)
49 {
50 //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);
51         if (r2.l < l)
52                 l = r2.l;
53
54         if (r2.r > r)
55                 r = r2.r;
56
57         if (r2.t > t)
58                 t = r2.t;
59
60         if (r2.b < b)
61                 b = r2.b;
62
63         return *this;
64 }
65
66 Rect & Rect::operator+=(Point p)
67 {
68         if (p.x < l)
69                 l = p.x;
70
71         if (p.x > r)
72                 r = p.x;
73
74         if (p.y < b)
75                 b = p.y;
76
77         if (p.y > t)
78                 t = p.y;
79
80         return *this;
81 }
82
83 //
84 // We use this to give a rect an array-like access, which allows access of the
85 // rect in TLBR order (from 0 to 3).  Also, values greater than 3 are treated
86 // as mod 4.
87 //
88 double & Rect::operator[](int idx)
89 {
90         idx = idx % 4;
91
92         switch (idx)
93         {
94         case 0:
95                 return t;
96         case 1:
97                 return l;
98         case 2:
99                 return b;
100         }
101
102         return r;
103 }
104
105 void Rect::Normalize(void)
106 {
107         if (l > r)
108         {
109                 double x = l;
110                 l = r;
111                 r = x;
112         }
113
114         if (b > t)
115         {
116                 double x = b;
117                 b = t;
118                 t = x;
119         }
120 }
121
122 void Rect::Translate(Point p)
123 {
124         l += p.x;
125         r += p.x;
126         t += p.y;
127         b += p.y;
128 }
129
130 void Rect::Expand(double amt)
131 {
132         l -= amt;
133         r += amt;
134         t += amt;
135         b -= amt;
136 }
137
138 double Rect::Width(void)
139 {
140         return fabs(r - l);
141 }
142
143 double Rect::Height(void)
144 {
145         return fabs(t - b);
146 }
147
148 bool Rect::Contains(Point p)
149 {
150         return ((p.x >= l) && (p.x <= r) && (p.y >= b) && (p.y <= t) ? true : false);
151 }
152
153 bool Rect::Contains(Rect rect)
154 {
155         return ((rect.l >= l) && (rect.r <= r) && (rect.b >= b) && (rect.t <= t) ? true : false);
156 }
157
158 Point Rect::TopLeft(void)
159 {
160         return Point(l, t);
161 }
162
163 Point Rect::TopRight(void)
164 {
165         return Point(r, t);
166 }
167
168 Point Rect::BottomLeft(void)
169 {
170         return Point(l, b);
171 }
172
173 Point Rect::BottomRight(void)
174 {
175         return Point(r, b);
176 }