Mercurial > projects > dynamin
annotate dynamin/painting/coordinates.d @ 101:1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 20:54:39 -0500 |
parents | 4f2d709760eb |
children | 73060bc3f004 |
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. | |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
18 * Portions created by the Initial Developer are Copyright (C) 2006-2012 |
0 | 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 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
57 float opIndex(int index) { return _values[index]; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
58 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
59 float opIndexAssign(float f, int index) { return _values[index] = f; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
60 /// |
0 | 61 Point opNeg() { |
62 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
63 pt2.x = -x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
64 pt2.y = -y; |
0 | 65 return pt2; |
66 } | |
67 /// | |
68 Point opAdd(Point pt) { | |
69 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
70 pt2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
71 pt2.y = y + pt.y; |
0 | 72 return pt2; |
73 } | |
74 /// | |
75 Point opSub(Point pt) { | |
76 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
77 pt2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
78 pt2.y = y - pt.y; |
0 | 79 return pt2; |
80 } | |
81 /// | |
82 Rect opAdd(Size size) { | |
83 Rect rect; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
84 rect.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
85 rect.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
86 rect.width = size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
87 rect.height = size.height; |
0 | 88 return rect; |
89 } | |
90 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
91 return format("Point [x={}, y={}]", x, y); |
0 | 92 } |
93 } | |
94 | |
95 /// | |
96 struct Size { | |
97 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
|
98 float[2] _values = [0, 0]; |
0 | 99 public: |
100 /// | |
101 static Size opCall() { | |
102 Size size; | |
103 return size; | |
104 } | |
105 /// | |
106 static Size opCall(float width, float height) { | |
107 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
|
108 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
|
109 size.height = height; |
0 | 110 return size; |
111 } | |
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 width() { return _values[0]; } |
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 width(float f) { _values[0] = f; } |
0 | 116 /// |
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
|
117 float height() { return _values[1]; } |
0 | 118 /// |
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
|
119 void height(float f) { _values[1] = f; } |
0 | 120 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
121 float opIndex(int index) { return _values[index]; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
122 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
123 float opIndexAssign(float f, int index) { return _values[index] = f; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
124 /// |
0 | 125 Size opAdd(Size size) { |
126 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
127 size2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
128 size2.height = height + size.height; |
0 | 129 return size2; |
130 } | |
131 /// | |
132 Size opSub(Size size) { | |
133 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
134 size2.width = width - size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
135 size2.height = height - size.height; |
0 | 136 return size2; |
137 } | |
138 /// | |
139 Size opAdd(BorderSize border) { | |
140 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
141 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
|
142 size2.height = height + border.top + border.bottom; |
0 | 143 return size2; |
144 } | |
145 /// | |
146 Size opSub(BorderSize border) { | |
147 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
148 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
|
149 size2.height = height - border.top - border.bottom; |
0 | 150 return size2; |
151 } | |
152 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
153 return format("Size [width={}, height={}]", width, height); |
0 | 154 } |
155 } | |
156 | |
157 /// | |
158 struct Rect { | |
159 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
|
160 float[4] _values = [0, 0, 0, 0]; |
0 | 161 public: |
162 static Rect opCall() { | |
163 Rect rect; | |
164 return rect; | |
165 } | |
166 static Rect opCall(float x, float y, float width, float height) { | |
167 Rect rect; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
168 rect.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
169 rect.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
170 rect.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
171 rect.height = height; |
0 | 172 return rect; |
173 } | |
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 x() { return _values[0]; } |
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 x(float f) { _values[0] = 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 y() { return _values[1]; } |
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 y(float f) { _values[1] = f; } |
0 | 182 /// |
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
|
183 float width() { return _values[2]; } |
0 | 184 /// |
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
|
185 void width(float f) { _values[2] = f; } |
0 | 186 /// |
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
|
187 float height() { return _values[3]; } |
0 | 188 /// |
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
|
189 void height(float f) { _values[3] = f; } |
0 | 190 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
191 float right() { return x + width; } |
0 | 192 /// |
98
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
193 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
|
194 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
195 float bottom() { return y + height; } |
0 | 196 /// |
98
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
197 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
|
198 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
199 float opIndex(int index) { return _values[index]; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
200 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
201 float opIndexAssign(float f, int index) { return _values[index] = f; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
202 /// |
0 | 203 Rect opAdd(Rect rect) { |
204 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
205 rect2.x = x + rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
206 rect2.y = y + rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
207 rect2.width = width + rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
208 rect2.height = height + rect.height; |
0 | 209 return rect2; |
210 } | |
211 /// | |
212 Rect opSub(Rect rect) { | |
213 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
214 rect2.x = x - rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
215 rect2.y = y - rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
216 rect2.width = width - rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
217 rect2.height = height - rect.height; |
0 | 218 return rect2; |
219 } | |
220 /// | |
221 Rect opAdd(Point pt) { | |
222 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
223 rect2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
224 rect2.y = y + pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
225 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
226 rect2.height = height; |
0 | 227 return rect2; |
228 } | |
229 /// | |
230 Rect opSub(Point pt) { | |
231 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
232 rect2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
233 rect2.y = y - pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
234 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
235 rect2.height = height; |
0 | 236 return rect2; |
237 } | |
238 /// | |
239 Rect opAdd(Size size) { | |
240 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
241 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
242 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
243 rect2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
244 rect2.height = height + size.height; |
0 | 245 return rect2; |
246 } | |
247 /// | |
248 Rect opSub(Size size) { | |
249 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
250 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
251 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
252 rect2.width = width - size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
253 rect2.height = height - size.height; |
0 | 254 return rect2; |
255 } | |
256 /// | |
257 Rect opAdd(BorderSize border) { | |
258 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
259 rect2.x = x - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
260 rect2.y = y - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
261 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
|
262 rect2.height = height + border.top + border.bottom; |
0 | 263 return rect2; |
264 } | |
265 /// | |
266 Rect opSub(BorderSize border) { | |
267 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
268 rect2.x = x + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
269 rect2.y = y + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
270 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
|
271 rect2.height = height - border.top - border.bottom; |
0 | 272 return rect2; |
273 } | |
274 bool contains(Point pt) { | |
275 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom; | |
276 } | |
277 Rect getUnion(Rect rect) { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
278 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
|
279 auto y2 = min(y, rect.y); |
0 | 280 Rect rect2; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
281 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
|
282 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
|
283 rect2.x = x2; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
284 rect2.y = y2; |
0 | 285 return rect2; |
286 } | |
287 string toString() { | |
288 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
|
289 x, y, width, height); |
0 | 290 } |
291 } | |
292 unittest { | |
99
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
293 assert(Rect(2, 3, 5, 9).right == 7); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
294 assert(Rect(2, 3, 5, 9).bottom == 12); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
295 // same |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
296 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
|
297 // second inside first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
298 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
|
299 // second outside first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
300 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
|
301 // second up left of first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
302 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
|
303 // second down right of first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
304 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
|
305 // not overlapping |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
306 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
|
307 // negative |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
308 assert(Rect(-4, -5, 2, 3).getUnion(Rect(-10, -2, 1, 5)) == Rect(-10, -5, 8, 8)); |
0 | 309 } |
310 | |
311 /// | |
312 struct BorderSize { | |
313 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
|
314 float[4] _values = [0, 0, 0, 0]; |
0 | 315 public: |
316 static BorderSize opCall() { | |
317 BorderSize border; | |
318 return border; | |
319 } | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
320 static BorderSize opCall(float left, float top, float right, float bottom) { |
0 | 321 BorderSize border; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
322 border.left = left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
323 border.top = top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
324 border.right = right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
325 border.bottom = bottom; |
0 | 326 return border; |
327 } | |
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 left() { return _values[0]; } |
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 left(float f) { _values[0] = f; } |
0 | 332 /// |
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
|
333 float top() { return _values[1]; } |
0 | 334 /// |
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
|
335 void top(float f) { _values[1] = f; } |
0 | 336 /// |
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
|
337 float right() { return _values[2]; } |
0 | 338 /// |
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
|
339 void right(float f) { _values[2] = f; } |
0 | 340 /// |
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
|
341 float bottom() { return _values[3]; } |
0 | 342 /// |
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
|
343 void bottom(float f) { _values[3] = f; } |
0 | 344 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
345 float opIndex(int index) { return _values[index]; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
346 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
347 float opIndexAssign(float f, int index) { return _values[index] = f; } |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
348 /// |
0 | 349 BorderSize opAdd(BorderSize border) { |
350 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
351 border2.left = left + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
352 border2.right = right + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
353 border2.top = top + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
354 border2.bottom = bottom + border.bottom; |
0 | 355 return border2; |
356 } | |
357 /// | |
358 BorderSize opSub(BorderSize border) { | |
359 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
360 border2.left = left - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
361 border2.right = right - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
362 border2.top = top - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
363 border2.bottom = bottom - border.bottom; |
0 | 364 return border2; |
365 } | |
366 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
367 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
|
368 left, top, right, bottom); |
0 | 369 } |
370 } | |
371 | |
372 unittest { | |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
373 Point pt; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
374 Size sz; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
375 Rect rect; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
376 BorderSize border; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
377 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
378 assert(pt.x == 0 && pt.y == 0); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
379 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
380 pt = Point(7, 9); |
0 | 381 assert(pt.x == 7); |
382 assert(pt.y == 9); | |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
383 rect = pt + Size(15, 13); |
0 | 384 assert(rect == Rect(7, 9, 15, 13)); |
385 assert(Size(15, 10) + BorderSize(3, 5, 1, 7) == Size(19, 22)); | |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
386 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
387 pt = Point(1, 2); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
388 assert(pt[0] == 1 && pt[1] == 2); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
389 pt[0] = 13; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
390 assert(pt[0] == 13); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
391 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
392 sz = Size(20, 30); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
393 assert(sz[0] == 20 && sz[1] == 30); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
394 sz[1] = 35; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
395 assert(sz[0] == 20 && sz[1] == 35); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
396 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
397 rect = Rect(1, 2, 3, 4); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
398 assert(rect[0] == 1 && rect[1] == 2 && rect[2] == 3 && rect[3] == 4); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
399 rect[2] = 5; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
400 assert(rect[2] == 5); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
401 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
402 border = BorderSize(5, 6, 7, 8); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
403 assert(border[0] == 5 && border[1] == 6 && border[2] == 7 && border[3] == 8); |
0 | 404 } |
405 |