Mercurial > projects > dynamin
annotate dynamin/painting/coordinates.d @ 98:e7e36dca4768
Add setters for Rect.right and Rect.bottom.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 14:27:21 -0500 |
parents | dccadd297348 |
children | 8daa63a28707 |
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: | |
34 float _x = 0, _y = 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 /// | |
49 float x() { return _x; } | |
50 /// | |
51 void x(float f) { _x = f; } | |
52 /// | |
53 float y() { return _y; } | |
54 /// | |
55 void y(float f) { _y = f; } | |
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: | |
94 float _width = 0, _height = 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; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
104 size._width = width; // TODO: underscores for CTFE--remove with D2 |
0 | 105 size._height = height; |
106 return size; | |
107 } | |
108 /// | |
109 float width() { return _width; } | |
110 /// | |
111 void width(float f) { _width = f; } | |
112 /// | |
113 float height() { return _height; } | |
114 /// | |
115 void height(float f) { _height = f; } | |
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: | |
152 float _x = 0, _y = 0, _width = 0, _height = 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 /// | |
167 float x() { return _x; } | |
168 /// | |
169 void x(float f) { _x = f; } | |
170 /// | |
171 float y() { return _y; } | |
172 /// | |
173 void y(float f) { _y = f; } | |
174 /// | |
175 float width() { return _width; } | |
176 /// | |
177 void width(float f) { _width = f; } | |
178 /// | |
179 float height() { return _height; } | |
180 /// | |
181 void height(float f) { _height = f; } | |
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 { | |
281 assert(Rect(20, 2, 70, 5).getUnion(Rect(22, 3, 70, 4)) == Rect(20, 2, 72, 5)); | |
282 } | |
283 | |
284 /// | |
285 struct BorderSize { | |
286 private: | |
287 float _left = 0, _top = 0, _right = 0, _bottom = 0; | |
288 public: | |
289 static BorderSize opCall() { | |
290 BorderSize border; | |
291 return border; | |
292 } | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
293 static BorderSize opCall(float left, float top, float right, float bottom) { |
0 | 294 BorderSize border; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
295 border.left = left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
296 border.top = top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
297 border.right = right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
298 border.bottom = bottom; |
0 | 299 return border; |
300 } | |
301 /// | |
302 float left() { return _left; } | |
303 /// | |
304 void left(float f) { _left = f; } | |
305 /// | |
306 float top() { return _top; } | |
307 /// | |
308 void top(float f) { _top = f; } | |
309 /// | |
310 float right() { return _right; } | |
311 /// | |
312 void right(float f) { _right = f; } | |
313 /// | |
314 float bottom() { return _bottom; } | |
315 /// | |
316 void bottom(float f) { _bottom = f; } | |
317 /// | |
318 BorderSize opAdd(BorderSize border) { | |
319 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
320 border2.left = left + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
321 border2.right = right + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
322 border2.top = top + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
323 border2.bottom = bottom + border.bottom; |
0 | 324 return border2; |
325 } | |
326 /// | |
327 BorderSize opSub(BorderSize border) { | |
328 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
329 border2.left = left - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
330 border2.right = right - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
331 border2.top = top - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
332 border2.bottom = bottom - border.bottom; |
0 | 333 return border2; |
334 } | |
335 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
336 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
|
337 left, top, right, bottom); |
0 | 338 } |
339 } | |
340 | |
341 unittest { | |
342 Point pt = Point(7, 9); | |
343 assert(pt.x == 7); | |
344 assert(pt.y == 9); | |
345 Rect rect = pt + Size(15, 13); | |
346 assert(rect == Rect(7, 9, 15, 13)); | |
347 assert(Size(15, 10) + BorderSize(3, 5, 1, 7) == Size(19, 22)); | |
348 } | |
349 |