Mercurial > projects > dynamin
comparison 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 |
comparison
equal
deleted
inserted
replaced
96:301e077da540 | 97:dccadd297348 |
---|---|
39 return pt; | 39 return pt; |
40 } | 40 } |
41 /// | 41 /// |
42 static Point opCall(float x, float y) { | 42 static Point opCall(float x, float y) { |
43 Point pt; | 43 Point pt; |
44 pt._x = x; | 44 pt.x = x; |
45 pt._y = y; | 45 pt.y = y; |
46 return pt; | 46 return pt; |
47 } | 47 } |
48 /// | 48 /// |
49 float x() { return _x; } | 49 float x() { return _x; } |
50 /// | 50 /// |
54 /// | 54 /// |
55 void y(float f) { _y = f; } | 55 void y(float f) { _y = f; } |
56 /// | 56 /// |
57 Point opNeg() { | 57 Point opNeg() { |
58 Point pt2; | 58 Point pt2; |
59 pt2._x = -_x; | 59 pt2.x = -x; |
60 pt2._y = -_y; | 60 pt2.y = -y; |
61 return pt2; | 61 return pt2; |
62 } | 62 } |
63 /// | 63 /// |
64 Point opAdd(Point pt) { | 64 Point opAdd(Point pt) { |
65 Point pt2; | 65 Point pt2; |
66 pt2._x = _x + pt._x; | 66 pt2.x = x + pt.x; |
67 pt2._y = _y + pt._y; | 67 pt2.y = y + pt.y; |
68 return pt2; | 68 return pt2; |
69 } | 69 } |
70 /// | 70 /// |
71 Point opSub(Point pt) { | 71 Point opSub(Point pt) { |
72 Point pt2; | 72 Point pt2; |
73 pt2._x = _x - pt._x; | 73 pt2.x = x - pt.x; |
74 pt2._y = _y - pt._y; | 74 pt2.y = y - pt.y; |
75 return pt2; | 75 return pt2; |
76 } | 76 } |
77 /// | 77 /// |
78 Rect opAdd(Size size) { | 78 Rect opAdd(Size size) { |
79 Rect rect; | 79 Rect rect; |
80 rect._x = _x; | 80 rect.x = x; |
81 rect._y = _y; | 81 rect.y = y; |
82 rect._width = size._width; | 82 rect.width = size.width; |
83 rect._height = size._height; | 83 rect.height = size.height; |
84 return rect; | 84 return rect; |
85 } | 85 } |
86 string toString() { | 86 string toString() { |
87 return format("Point [x={}, y={}]", _x, _y); | 87 return format("Point [x={}, y={}]", x, y); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 /// | 91 /// |
92 struct Size { | 92 struct Size { |
99 return size; | 99 return size; |
100 } | 100 } |
101 /// | 101 /// |
102 static Size opCall(float width, float height) { | 102 static Size opCall(float width, float height) { |
103 Size size; | 103 Size size; |
104 size._width = width; | 104 size._width = width; // TODO: underscores for CTFE--remove with D2 |
105 size._height = height; | 105 size._height = height; |
106 return size; | 106 return size; |
107 } | 107 } |
108 /// | 108 /// |
109 float width() { return _width; } | 109 float width() { return _width; } |
114 /// | 114 /// |
115 void height(float f) { _height = f; } | 115 void height(float f) { _height = f; } |
116 /// | 116 /// |
117 Size opAdd(Size size) { | 117 Size opAdd(Size size) { |
118 Size size2; | 118 Size size2; |
119 size2._width = _width + size._width; | 119 size2.width = width + size.width; |
120 size2._height = _height + size._height; | 120 size2.height = height + size.height; |
121 return size2; | 121 return size2; |
122 } | 122 } |
123 /// | 123 /// |
124 Size opSub(Size size) { | 124 Size opSub(Size size) { |
125 Size size2; | 125 Size size2; |
126 size2._width = _width - size._width; | 126 size2.width = width - size.width; |
127 size2._height = _height - size._height; | 127 size2.height = height - size.height; |
128 return size2; | 128 return size2; |
129 } | 129 } |
130 /// | 130 /// |
131 Size opAdd(BorderSize border) { | 131 Size opAdd(BorderSize border) { |
132 Size size2; | 132 Size size2; |
133 size2._width = _width + border._left + border._right; | 133 size2.width = width + border.left + border.right; |
134 size2._height = _height + border._top + border._bottom; | 134 size2.height = height + border.top + border.bottom; |
135 return size2; | 135 return size2; |
136 } | 136 } |
137 /// | 137 /// |
138 Size opSub(BorderSize border) { | 138 Size opSub(BorderSize border) { |
139 Size size2; | 139 Size size2; |
140 size2._width = _width - border._left - border._right; | 140 size2.width = width - border.left - border.right; |
141 size2._height = _height - border._top - border._bottom; | 141 size2.height = height - border.top - border.bottom; |
142 return size2; | 142 return size2; |
143 } | 143 } |
144 string toString() { | 144 string toString() { |
145 return format("Size [width={}, height={}]", _width, _height); | 145 return format("Size [width={}, height={}]", width, height); |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 /// | 149 /// |
150 struct Rect { | 150 struct Rect { |
155 Rect rect; | 155 Rect rect; |
156 return rect; | 156 return rect; |
157 } | 157 } |
158 static Rect opCall(float x, float y, float width, float height) { | 158 static Rect opCall(float x, float y, float width, float height) { |
159 Rect rect; | 159 Rect rect; |
160 rect._x = x; | 160 rect.x = x; |
161 rect._y = y; | 161 rect.y = y; |
162 rect._width = width; | 162 rect.width = width; |
163 rect._height = height; | 163 rect.height = height; |
164 return rect; | 164 return rect; |
165 } | 165 } |
166 /// | 166 /// |
167 float x() { return _x; } | 167 float x() { return _x; } |
168 /// | 168 /// |
178 /// | 178 /// |
179 float height() { return _height; } | 179 float height() { return _height; } |
180 /// | 180 /// |
181 void height(float f) { _height = f; } | 181 void height(float f) { _height = f; } |
182 /// | 182 /// |
183 float right() { return _x+_width; } | 183 float right() { return x + width; } |
184 /// | 184 /// |
185 float bottom() { return _y+_height; } | 185 float bottom() { return y + height; } |
186 /// | 186 /// |
187 Rect opAdd(Rect rect) { | 187 Rect opAdd(Rect rect) { |
188 Rect rect2; | 188 Rect rect2; |
189 rect2._x = _x + rect._x; | 189 rect2.x = x + rect.x; |
190 rect2._y = _y + rect._y; | 190 rect2.y = y + rect.y; |
191 rect2._width = _width + rect._width; | 191 rect2.width = width + rect.width; |
192 rect2._height = _height + rect._height; | 192 rect2.height = height + rect.height; |
193 return rect2; | 193 return rect2; |
194 } | 194 } |
195 /// | 195 /// |
196 Rect opSub(Rect rect) { | 196 Rect opSub(Rect rect) { |
197 Rect rect2; | 197 Rect rect2; |
198 rect2._x = _x - rect._x; | 198 rect2.x = x - rect.x; |
199 rect2._y = _y - rect._y; | 199 rect2.y = y - rect.y; |
200 rect2._width = _width - rect._width; | 200 rect2.width = width - rect.width; |
201 rect2._height = _height - rect._height; | 201 rect2.height = height - rect.height; |
202 return rect2; | 202 return rect2; |
203 } | 203 } |
204 /// | 204 /// |
205 Rect opAdd(Point pt) { | 205 Rect opAdd(Point pt) { |
206 Rect rect2; | 206 Rect rect2; |
207 rect2._x = _x + pt._x; | 207 rect2.x = x + pt.x; |
208 rect2._y = _y + pt._y; | 208 rect2.y = y + pt.y; |
209 rect2._width = _width; | 209 rect2.width = width; |
210 rect2._height = _height; | 210 rect2.height = height; |
211 return rect2; | 211 return rect2; |
212 } | 212 } |
213 /// | 213 /// |
214 Rect opSub(Point pt) { | 214 Rect opSub(Point pt) { |
215 Rect rect2; | 215 Rect rect2; |
216 rect2._x = _x - pt._x; | 216 rect2.x = x - pt.x; |
217 rect2._y = _y - pt._y; | 217 rect2.y = y - pt.y; |
218 rect2._width = _width; | 218 rect2.width = width; |
219 rect2._height = _height; | 219 rect2.height = height; |
220 return rect2; | 220 return rect2; |
221 } | 221 } |
222 /// | 222 /// |
223 Rect opAdd(Size size) { | 223 Rect opAdd(Size size) { |
224 Rect rect2; | 224 Rect rect2; |
225 rect2._x = _x; | 225 rect2.x = x; |
226 rect2._y = _y; | 226 rect2.y = y; |
227 rect2._width = _width + size._width; | 227 rect2.width = width + size.width; |
228 rect2._height = _height + size._height; | 228 rect2.height = height + size.height; |
229 return rect2; | 229 return rect2; |
230 } | 230 } |
231 /// | 231 /// |
232 Rect opSub(Size size) { | 232 Rect opSub(Size size) { |
233 Rect rect2; | 233 Rect rect2; |
234 rect2._x = _x; | 234 rect2.x = x; |
235 rect2._y = _y; | 235 rect2.y = y; |
236 rect2._width = _width - size._width; | 236 rect2.width = width - size.width; |
237 rect2._height = _height - size._height; | 237 rect2.height = height - size.height; |
238 return rect2; | 238 return rect2; |
239 } | 239 } |
240 /// | 240 /// |
241 Rect opAdd(BorderSize border) { | 241 Rect opAdd(BorderSize border) { |
242 Rect rect2; | 242 Rect rect2; |
243 rect2._x = _x - border._left; | 243 rect2.x = x - border.left; |
244 rect2._y = _y - border._top; | 244 rect2.y = y - border.top; |
245 rect2._width = _width + border._left + border._right; | 245 rect2.width = width + border.left + border.right; |
246 rect2._height = _height + border._top + border._bottom; | 246 rect2.height = height + border.top + border.bottom; |
247 return rect2; | 247 return rect2; |
248 } | 248 } |
249 /// | 249 /// |
250 Rect opSub(BorderSize border) { | 250 Rect opSub(BorderSize border) { |
251 Rect rect2; | 251 Rect rect2; |
252 rect2._x = _x + border._left; | 252 rect2.x = x + border.left; |
253 rect2._y = _y + border._top; | 253 rect2.y = y + border.top; |
254 rect2._width = _width - border._left - border._right; | 254 rect2.width = width - border.left - border.right; |
255 rect2._height = _height - border._top - border._bottom; | 255 rect2.height = height - border.top - border.bottom; |
256 return rect2; | 256 return rect2; |
257 } | 257 } |
258 bool contains(Point pt) { | 258 bool contains(Point pt) { |
259 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom; | 259 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom; |
260 } | 260 } |
261 Rect getUnion(Rect rect) { | 261 Rect getUnion(Rect rect) { |
262 auto x2 = min(_x, rect._x); | 262 auto x2 = min(x, rect.x); |
263 auto y2 = min(_y, rect._y); | 263 auto y2 = min(y, rect.y); |
264 Rect rect2; | 264 Rect rect2; |
265 rect2._width = max(_x+_width, rect._x+rect._width)-x2; | 265 rect2.width = max(x + width, rect.x + rect.width ) - x2; |
266 rect2._height = max(_y+_height, rect._y+rect._height)-y2; | 266 rect2.height = max(y + height, rect.y + rect.height) - y2; |
267 rect2._x = x2; | 267 rect2.x = x2; |
268 rect2._y = y2; | 268 rect2.y = y2; |
269 return rect2; | 269 return rect2; |
270 } | 270 } |
271 string toString() { | 271 string toString() { |
272 return format("Rect [x={}, y={}, width={}, height={}]", | 272 return format("Rect [x={}, y={}, width={}, height={}]", |
273 _x, _y, _width, _height); | 273 x, y, width, height); |
274 } | 274 } |
275 } | 275 } |
276 unittest { | 276 unittest { |
277 assert(Rect(20, 2, 70, 5).getUnion(Rect(22, 3, 70, 4)) == Rect(20, 2, 72, 5)); | 277 assert(Rect(20, 2, 70, 5).getUnion(Rect(22, 3, 70, 4)) == Rect(20, 2, 72, 5)); |
278 } | 278 } |
284 public: | 284 public: |
285 static BorderSize opCall() { | 285 static BorderSize opCall() { |
286 BorderSize border; | 286 BorderSize border; |
287 return border; | 287 return border; |
288 } | 288 } |
289 static BorderSize opCall(float _left, float _top, float _right, float _bottom) { | 289 static BorderSize opCall(float left, float top, float right, float bottom) { |
290 BorderSize border; | 290 BorderSize border; |
291 border._left = _left; | 291 border.left = left; |
292 border._top = _top; | 292 border.top = top; |
293 border._right = _right; | 293 border.right = right; |
294 border._bottom = _bottom; | 294 border.bottom = bottom; |
295 return border; | 295 return border; |
296 } | 296 } |
297 /// | 297 /// |
298 float left() { return _left; } | 298 float left() { return _left; } |
299 /// | 299 /// |
311 /// | 311 /// |
312 void bottom(float f) { _bottom = f; } | 312 void bottom(float f) { _bottom = f; } |
313 /// | 313 /// |
314 BorderSize opAdd(BorderSize border) { | 314 BorderSize opAdd(BorderSize border) { |
315 BorderSize border2; | 315 BorderSize border2; |
316 border2._left = _left + border._left; | 316 border2.left = left + border.left; |
317 border2._right = _right + border._right; | 317 border2.right = right + border.right; |
318 border2._top = _top + border._top; | 318 border2.top = top + border.top; |
319 border2._bottom = _bottom + border._bottom; | 319 border2.bottom = bottom + border.bottom; |
320 return border2; | 320 return border2; |
321 } | 321 } |
322 /// | 322 /// |
323 BorderSize opSub(BorderSize border) { | 323 BorderSize opSub(BorderSize border) { |
324 BorderSize border2; | 324 BorderSize border2; |
325 border2._left = _left - border._left; | 325 border2.left = left - border.left; |
326 border2._right = _right - border._right; | 326 border2.right = right - border.right; |
327 border2._top = _top - border._top; | 327 border2.top = top - border.top; |
328 border2._bottom = _bottom - border._bottom; | 328 border2.bottom = bottom - border.bottom; |
329 return border2; | 329 return border2; |
330 } | 330 } |
331 string toString() { | 331 string toString() { |
332 return format("BorderSize [_left={}, _top={}, _right={}, _bottom={}]", | 332 return format("BorderSize [left={}, top={}, right={}, bottom={}]", |
333 _left, _top, _right, _bottom); | 333 left, top, right, bottom); |
334 } | 334 } |
335 } | 335 } |
336 | 336 |
337 unittest { | 337 unittest { |
338 Point pt = Point(7, 9); | 338 Point pt = Point(7, 9); |