Mercurial > projects > dynamin
annotate dynamin/painting/coordinates.d @ 100:4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 15:17:24 -0500 |
parents | 8daa63a28707 |
children | 1690ebff00a0 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.painting.coordinates; | |
27 | |
28 import dynamin.core.string; | |
29 import dynamin.core.math; | |
30 | |
31 /// | |
32 struct Point { | |
33 private: | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
34 float[2] _values = [0, 0]; |
0 | 35 public: |
36 /// | |
37 static Point opCall() { | |
38 Point pt; | |
39 return pt; | |
40 } | |
41 /// | |
42 static Point opCall(float x, float y) { | |
43 Point pt; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
44 pt.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
45 pt.y = y; |
0 | 46 return pt; |
47 } | |
48 /// | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
49 float x() { return _values[0]; } |
0 | 50 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
51 void x(float f) { _values[0] = f; } |
0 | 52 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
53 float y() { return _values[1]; } |
0 | 54 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
55 void y(float f) { _values[1] = f; } |
0 | 56 /// |
57 Point opNeg() { | |
58 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
59 pt2.x = -x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
60 pt2.y = -y; |
0 | 61 return pt2; |
62 } | |
63 /// | |
64 Point opAdd(Point pt) { | |
65 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
66 pt2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
67 pt2.y = y + pt.y; |
0 | 68 return pt2; |
69 } | |
70 /// | |
71 Point opSub(Point pt) { | |
72 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
73 pt2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
74 pt2.y = y - pt.y; |
0 | 75 return pt2; |
76 } | |
77 /// | |
78 Rect opAdd(Size size) { | |
79 Rect rect; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
80 rect.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
81 rect.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
82 rect.width = size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
83 rect.height = size.height; |
0 | 84 return rect; |
85 } | |
86 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
87 return format("Point [x={}, y={}]", x, y); |
0 | 88 } |
89 } | |
90 | |
91 /// | |
92 struct Size { | |
93 private: | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
94 float[2] _values = [0, 0]; |
0 | 95 public: |
96 /// | |
97 static Size opCall() { | |
98 Size size; | |
99 return size; | |
100 } | |
101 /// | |
102 static Size opCall(float width, float height) { | |
103 Size size; | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
104 size.width = width; |
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
105 size.height = height; |
0 | 106 return size; |
107 } | |
108 /// | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
109 float width() { return _values[0]; } |
0 | 110 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
111 void width(float f) { _values[0] = f; } |
0 | 112 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
113 float height() { return _values[1]; } |
0 | 114 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
115 void height(float f) { _values[1] = f; } |
0 | 116 /// |
117 Size opAdd(Size size) { | |
118 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
119 size2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
120 size2.height = height + size.height; |
0 | 121 return size2; |
122 } | |
123 /// | |
124 Size opSub(Size size) { | |
125 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
126 size2.width = width - size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
127 size2.height = height - size.height; |
0 | 128 return size2; |
129 } | |
130 /// | |
131 Size opAdd(BorderSize border) { | |
132 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
133 size2.width = width + border.left + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
134 size2.height = height + border.top + border.bottom; |
0 | 135 return size2; |
136 } | |
137 /// | |
138 Size opSub(BorderSize border) { | |
139 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
140 size2.width = width - border.left - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
141 size2.height = height - border.top - border.bottom; |
0 | 142 return size2; |
143 } | |
144 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
145 return format("Size [width={}, height={}]", width, height); |
0 | 146 } |
147 } | |
148 | |
149 /// | |
150 struct Rect { | |
151 private: | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
152 float[4] _values = [0, 0, 0, 0]; |
0 | 153 public: |
154 static Rect opCall() { | |
155 Rect rect; | |
156 return rect; | |
157 } | |
158 static Rect opCall(float x, float y, float width, float height) { | |
159 Rect rect; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
160 rect.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
161 rect.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
162 rect.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
163 rect.height = height; |
0 | 164 return rect; |
165 } | |
166 /// | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
167 float x() { return _values[0]; } |
0 | 168 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
169 void x(float f) { _values[0] = f; } |
0 | 170 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
171 float y() { return _values[1]; } |
0 | 172 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
173 void y(float f) { _values[1] = f; } |
0 | 174 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
175 float width() { return _values[2]; } |
0 | 176 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
177 void width(float f) { _values[2] = f; } |
0 | 178 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
179 float height() { return _values[3]; } |
0 | 180 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
181 void height(float f) { _values[3] = f; } |
0 | 182 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
183 float right() { return x + width; } |
0 | 184 /// |
98
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
185 void right(float f) { width = f - x; } |
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
186 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
187 float bottom() { return y + height; } |
0 | 188 /// |
98
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
189 void bottom(float f) { height = f - y; } |
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
190 /// |
0 | 191 Rect opAdd(Rect rect) { |
192 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
193 rect2.x = x + rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
194 rect2.y = y + rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
195 rect2.width = width + rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
196 rect2.height = height + rect.height; |
0 | 197 return rect2; |
198 } | |
199 /// | |
200 Rect opSub(Rect rect) { | |
201 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
202 rect2.x = x - rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
203 rect2.y = y - rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
204 rect2.width = width - rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
205 rect2.height = height - rect.height; |
0 | 206 return rect2; |
207 } | |
208 /// | |
209 Rect opAdd(Point pt) { | |
210 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
211 rect2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
212 rect2.y = y + pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
213 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
214 rect2.height = height; |
0 | 215 return rect2; |
216 } | |
217 /// | |
218 Rect opSub(Point pt) { | |
219 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
220 rect2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
221 rect2.y = y - pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
222 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
223 rect2.height = height; |
0 | 224 return rect2; |
225 } | |
226 /// | |
227 Rect opAdd(Size size) { | |
228 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
229 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
230 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
231 rect2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
232 rect2.height = height + size.height; |
0 | 233 return rect2; |
234 } | |
235 /// | |
236 Rect opSub(Size size) { | |
237 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
238 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
239 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
240 rect2.width = width - size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
241 rect2.height = height - size.height; |
0 | 242 return rect2; |
243 } | |
244 /// | |
245 Rect opAdd(BorderSize border) { | |
246 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
247 rect2.x = x - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
248 rect2.y = y - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
249 rect2.width = width + border.left + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
250 rect2.height = height + border.top + border.bottom; |
0 | 251 return rect2; |
252 } | |
253 /// | |
254 Rect opSub(BorderSize border) { | |
255 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
256 rect2.x = x + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
257 rect2.y = y + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
258 rect2.width = width - border.left - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
259 rect2.height = height - border.top - border.bottom; |
0 | 260 return rect2; |
261 } | |
262 bool contains(Point pt) { | |
263 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom; | |
264 } | |
265 Rect getUnion(Rect rect) { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
266 auto x2 = min(x, rect.x); |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
267 auto y2 = min(y, rect.y); |
0 | 268 Rect rect2; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
269 rect2.width = max(x + width, rect.x + rect.width ) - x2; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
270 rect2.height = max(y + height, rect.y + rect.height) - y2; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
271 rect2.x = x2; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
272 rect2.y = y2; |
0 | 273 return rect2; |
274 } | |
275 string toString() { | |
276 return format("Rect [x={}, y={}, width={}, height={}]", | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
277 x, y, width, height); |
0 | 278 } |
279 } | |
280 unittest { | |
99
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
281 assert(Rect(2, 3, 5, 9).right == 7); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
282 assert(Rect(2, 3, 5, 9).bottom == 12); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
283 // same |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
284 assert(Rect(1, 2, 3, 3).getUnion(Rect(1, 2, 3, 3)) == Rect(1, 2, 3, 3)); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
285 // second inside first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
286 assert(Rect(1, 2, 5, 7.5).getUnion(Rect(3, 4, 1.5, 2)) == Rect(1, 2, 5, 7.5)); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
287 // second outside first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
288 assert(Rect(1, 2, 5, 7.5).getUnion(Rect(0.5, 0.7, 10, 11)) == Rect(0.5, 0.7, 10, 11)); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
289 // second up left of first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
290 assert(Rect(2, 3, 6, 8).getUnion(Rect(1, 2, 5, 6)) == Rect(1, 2, 7, 9)); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
291 // second down right of first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
292 assert(Rect(2, 3, 6, 8).getUnion(Rect(5, 4, 7, 9)) == Rect(2, 3, 10, 10)); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
293 // not overlapping |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
294 assert(Rect(1, 2, 1, 3).getUnion(Rect(20, 30, 5, 5)) == Rect(1, 2, 24, 33)); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
295 // negative |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
296 assert(Rect(-4, -5, 2, 3).getUnion(Rect(-10, -2, 1, 5)) == Rect(-10, -5, 8, 8)); |
0 | 297 } |
298 | |
299 /// | |
300 struct BorderSize { | |
301 private: | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
302 float[4] _values = [0, 0, 0, 0]; |
0 | 303 public: |
304 static BorderSize opCall() { | |
305 BorderSize border; | |
306 return border; | |
307 } | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
308 static BorderSize opCall(float left, float top, float right, float bottom) { |
0 | 309 BorderSize border; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
310 border.left = left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
311 border.top = top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
312 border.right = right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
313 border.bottom = bottom; |
0 | 314 return border; |
315 } | |
316 /// | |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
317 float left() { return _values[0]; } |
0 | 318 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
319 void left(float f) { _values[0] = f; } |
0 | 320 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
321 float top() { return _values[1]; } |
0 | 322 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
323 void top(float f) { _values[1] = f; } |
0 | 324 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
325 float right() { return _values[2]; } |
0 | 326 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
327 void right(float f) { _values[2] = f; } |
0 | 328 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
329 float bottom() { return _values[3]; } |
0 | 330 /// |
100
4f2d709760eb
Change from individual fields to a static array for x, y, width, height, etc.
Jordan Miner <jminer7@gmail.com>
parents:
99
diff
changeset
|
331 void bottom(float f) { _values[3] = f; } |
0 | 332 /// |
333 BorderSize opAdd(BorderSize border) { | |
334 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
335 border2.left = left + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
336 border2.right = right + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
337 border2.top = top + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
338 border2.bottom = bottom + border.bottom; |
0 | 339 return border2; |
340 } | |
341 /// | |
342 BorderSize opSub(BorderSize border) { | |
343 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
344 border2.left = left - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
345 border2.right = right - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
346 border2.top = top - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
347 border2.bottom = bottom - border.bottom; |
0 | 348 return border2; |
349 } | |
350 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
351 return format("BorderSize [left={}, top={}, right={}, bottom={}]", |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
352 left, top, right, bottom); |
0 | 353 } |
354 } | |
355 | |
356 unittest { | |
357 Point pt = Point(7, 9); | |
358 assert(pt.x == 7); | |
359 assert(pt.y == 9); | |
360 Rect rect = pt + Size(15, 13); | |
361 assert(rect == Rect(7, 9, 15, 13)); | |
362 assert(Size(15, 10) + BorderSize(3, 5, 1, 7) == Size(19, 22)); | |
363 } | |
364 |