Mercurial > projects > dynamin
annotate dynamin/painting/coordinates.d @ 97:dccadd297348
Use methods/properties instead of the instance variables directly.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 14:25:29 -0500 |
parents | aa4efef0f0b1 |
children | e7e36dca4768 |
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 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
185 float bottom() { return y + height; } |
0 | 186 /// |
187 Rect opAdd(Rect rect) { | |
188 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
189 rect2.x = x + rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
190 rect2.y = y + rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
191 rect2.width = width + rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
192 rect2.height = height + rect.height; |
0 | 193 return rect2; |
194 } | |
195 /// | |
196 Rect opSub(Rect rect) { | |
197 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
198 rect2.x = x - rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
199 rect2.y = y - rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
200 rect2.width = width - rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
201 rect2.height = height - rect.height; |
0 | 202 return rect2; |
203 } | |
204 /// | |
205 Rect opAdd(Point pt) { | |
206 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
207 rect2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
208 rect2.y = y + pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
209 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
210 rect2.height = height; |
0 | 211 return rect2; |
212 } | |
213 /// | |
214 Rect opSub(Point pt) { | |
215 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
216 rect2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
217 rect2.y = y - pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
218 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
219 rect2.height = height; |
0 | 220 return rect2; |
221 } | |
222 /// | |
223 Rect opAdd(Size size) { | |
224 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
225 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
226 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
227 rect2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
228 rect2.height = height + size.height; |
0 | 229 return rect2; |
230 } | |
231 /// | |
232 Rect opSub(Size size) { | |
233 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
234 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
235 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
236 rect2.width = width - size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
237 rect2.height = height - size.height; |
0 | 238 return rect2; |
239 } | |
240 /// | |
241 Rect opAdd(BorderSize border) { | |
242 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
243 rect2.x = x - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
244 rect2.y = y - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
245 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
|
246 rect2.height = height + border.top + border.bottom; |
0 | 247 return rect2; |
248 } | |
249 /// | |
250 Rect opSub(BorderSize border) { | |
251 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
252 rect2.x = x + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
253 rect2.y = y + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
254 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
|
255 rect2.height = height - border.top - border.bottom; |
0 | 256 return rect2; |
257 } | |
258 bool contains(Point pt) { | |
259 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom; | |
260 } | |
261 Rect getUnion(Rect rect) { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
262 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
|
263 auto y2 = min(y, rect.y); |
0 | 264 Rect rect2; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
265 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
|
266 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
|
267 rect2.x = x2; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
268 rect2.y = y2; |
0 | 269 return rect2; |
270 } | |
271 string toString() { | |
272 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
|
273 x, y, width, height); |
0 | 274 } |
275 } | |
276 unittest { | |
277 assert(Rect(20, 2, 70, 5).getUnion(Rect(22, 3, 70, 4)) == Rect(20, 2, 72, 5)); | |
278 } | |
279 | |
280 /// | |
281 struct BorderSize { | |
282 private: | |
283 float _left = 0, _top = 0, _right = 0, _bottom = 0; | |
284 public: | |
285 static BorderSize opCall() { | |
286 BorderSize border; | |
287 return border; | |
288 } | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
289 static BorderSize opCall(float left, float top, float right, float bottom) { |
0 | 290 BorderSize border; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
291 border.left = left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
292 border.top = top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
293 border.right = right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
294 border.bottom = bottom; |
0 | 295 return border; |
296 } | |
297 /// | |
298 float left() { return _left; } | |
299 /// | |
300 void left(float f) { _left = f; } | |
301 /// | |
302 float top() { return _top; } | |
303 /// | |
304 void top(float f) { _top = f; } | |
305 /// | |
306 float right() { return _right; } | |
307 /// | |
308 void right(float f) { _right = f; } | |
309 /// | |
310 float bottom() { return _bottom; } | |
311 /// | |
312 void bottom(float f) { _bottom = f; } | |
313 /// | |
314 BorderSize opAdd(BorderSize border) { | |
315 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
316 border2.left = left + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
317 border2.right = right + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
318 border2.top = top + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
319 border2.bottom = bottom + border.bottom; |
0 | 320 return border2; |
321 } | |
322 /// | |
323 BorderSize opSub(BorderSize border) { | |
324 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
325 border2.left = left - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
326 border2.right = right - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
327 border2.top = top - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
328 border2.bottom = bottom - border.bottom; |
0 | 329 return border2; |
330 } | |
331 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
332 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
|
333 left, top, right, bottom); |
0 | 334 } |
335 } | |
336 | |
337 unittest { | |
338 Point pt = Point(7, 9); | |
339 assert(pt.x == 7); | |
340 assert(pt.y == 9); | |
341 Rect rect = pt + Size(15, 13); | |
342 assert(rect == Rect(7, 9, 15, 13)); | |
343 assert(Size(15, 10) + BorderSize(3, 5, 1, 7) == Size(19, 22)); | |
344 } | |
345 |