comparison dynamin/painting/coordinates.d @ 0:aa4efef0f0b1

Initial commit of code.
author Jordan Miner <jminer7@gmail.com>
date Mon, 15 Jun 2009 22:10:48 -0500
parents
children dccadd297348
comparison
equal deleted inserted replaced
-1:000000000000 0:aa4efef0f0b1
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;
44 pt._x = x;
45 pt._y = y;
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;
59 pt2._x = -_x;
60 pt2._y = -_y;
61 return pt2;
62 }
63 ///
64 Point opAdd(Point pt) {
65 Point pt2;
66 pt2._x = _x + pt._x;
67 pt2._y = _y + pt._y;
68 return pt2;
69 }
70 ///
71 Point opSub(Point pt) {
72 Point pt2;
73 pt2._x = _x - pt._x;
74 pt2._y = _y - pt._y;
75 return pt2;
76 }
77 ///
78 Rect opAdd(Size size) {
79 Rect rect;
80 rect._x = _x;
81 rect._y = _y;
82 rect._width = size._width;
83 rect._height = size._height;
84 return rect;
85 }
86 string toString() {
87 return format("Point [x={}, y={}]", _x, _y);
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;
104 size._width = width;
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;
119 size2._width = _width + size._width;
120 size2._height = _height + size._height;
121 return size2;
122 }
123 ///
124 Size opSub(Size size) {
125 Size size2;
126 size2._width = _width - size._width;
127 size2._height = _height - size._height;
128 return size2;
129 }
130 ///
131 Size opAdd(BorderSize border) {
132 Size size2;
133 size2._width = _width + border._left + border._right;
134 size2._height = _height + border._top + border._bottom;
135 return size2;
136 }
137 ///
138 Size opSub(BorderSize border) {
139 Size size2;
140 size2._width = _width - border._left - border._right;
141 size2._height = _height - border._top - border._bottom;
142 return size2;
143 }
144 string toString() {
145 return format("Size [width={}, height={}]", _width, _height);
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;
160 rect._x = x;
161 rect._y = y;
162 rect._width = width;
163 rect._height = height;
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 ///
183 float right() { return _x+_width; }
184 ///
185 float bottom() { return _y+_height; }
186 ///
187 Rect opAdd(Rect rect) {
188 Rect rect2;
189 rect2._x = _x + rect._x;
190 rect2._y = _y + rect._y;
191 rect2._width = _width + rect._width;
192 rect2._height = _height + rect._height;
193 return rect2;
194 }
195 ///
196 Rect opSub(Rect rect) {
197 Rect rect2;
198 rect2._x = _x - rect._x;
199 rect2._y = _y - rect._y;
200 rect2._width = _width - rect._width;
201 rect2._height = _height - rect._height;
202 return rect2;
203 }
204 ///
205 Rect opAdd(Point pt) {
206 Rect rect2;
207 rect2._x = _x + pt._x;
208 rect2._y = _y + pt._y;
209 rect2._width = _width;
210 rect2._height = _height;
211 return rect2;
212 }
213 ///
214 Rect opSub(Point pt) {
215 Rect rect2;
216 rect2._x = _x - pt._x;
217 rect2._y = _y - pt._y;
218 rect2._width = _width;
219 rect2._height = _height;
220 return rect2;
221 }
222 ///
223 Rect opAdd(Size size) {
224 Rect rect2;
225 rect2._x = _x;
226 rect2._y = _y;
227 rect2._width = _width + size._width;
228 rect2._height = _height + size._height;
229 return rect2;
230 }
231 ///
232 Rect opSub(Size size) {
233 Rect rect2;
234 rect2._x = _x;
235 rect2._y = _y;
236 rect2._width = _width - size._width;
237 rect2._height = _height - size._height;
238 return rect2;
239 }
240 ///
241 Rect opAdd(BorderSize border) {
242 Rect rect2;
243 rect2._x = _x - border._left;
244 rect2._y = _y - border._top;
245 rect2._width = _width + border._left + border._right;
246 rect2._height = _height + border._top + border._bottom;
247 return rect2;
248 }
249 ///
250 Rect opSub(BorderSize border) {
251 Rect rect2;
252 rect2._x = _x + border._left;
253 rect2._y = _y + border._top;
254 rect2._width = _width - border._left - border._right;
255 rect2._height = _height - border._top - border._bottom;
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) {
262 auto x2 = min(_x, rect._x);
263 auto y2 = min(_y, rect._y);
264 Rect rect2;
265 rect2._width = max(_x+_width, rect._x+rect._width)-x2;
266 rect2._height = max(_y+_height, rect._y+rect._height)-y2;
267 rect2._x = x2;
268 rect2._y = y2;
269 return rect2;
270 }
271 string toString() {
272 return format("Rect [x={}, y={}, width={}, height={}]",
273 _x, _y, _width, _height);
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 }
289 static BorderSize opCall(float _left, float _top, float _right, float _bottom) {
290 BorderSize border;
291 border._left = _left;
292 border._top = _top;
293 border._right = _right;
294 border._bottom = _bottom;
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;
316 border2._left = _left + border._left;
317 border2._right = _right + border._right;
318 border2._top = _top + border._top;
319 border2._bottom = _bottom + border._bottom;
320 return border2;
321 }
322 ///
323 BorderSize opSub(BorderSize border) {
324 BorderSize border2;
325 border2._left = _left - border._left;
326 border2._right = _right - border._right;
327 border2._top = _top - border._top;
328 border2._bottom = _bottom - border._bottom;
329 return border2;
330 }
331 string toString() {
332 return format("BorderSize [_left={}, _top={}, _right={}, _bottom={}]",
333 _left, _top, _right, _bottom);
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