Mercurial > projects > dynamin
annotate dynamin/painting/coordinates.d @ 103:73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 22:06:02 -0500 |
parents | 1690ebff00a0 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
101
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
101
diff
changeset
|
5 * This Source Code Form is subject to the terms of the Mozilla Public |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
101
diff
changeset
|
6 * License, v. 2.0. If a copy of the MPL was not distributed with this |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
101
diff
changeset
|
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.painting.coordinates; | |
12 | |
13 import dynamin.core.string; | |
14 import dynamin.core.math; | |
15 | |
16 /// | |
17 struct Point { | |
18 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
|
19 float[2] _values = [0, 0]; |
0 | 20 public: |
21 /// | |
22 static Point opCall() { | |
23 Point pt; | |
24 return pt; | |
25 } | |
26 /// | |
27 static Point opCall(float x, float y) { | |
28 Point pt; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
29 pt.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
30 pt.y = y; |
0 | 31 return pt; |
32 } | |
33 /// | |
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 x() { return _values[0]; } |
0 | 35 /// |
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
|
36 void x(float f) { _values[0] = f; } |
0 | 37 /// |
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
|
38 float y() { return _values[1]; } |
0 | 39 /// |
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
|
40 void y(float f) { _values[1] = f; } |
0 | 41 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
42 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
|
43 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
44 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
|
45 /// |
0 | 46 Point opNeg() { |
47 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
48 pt2.x = -x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
49 pt2.y = -y; |
0 | 50 return pt2; |
51 } | |
52 /// | |
53 Point opAdd(Point pt) { | |
54 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
55 pt2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
56 pt2.y = y + pt.y; |
0 | 57 return pt2; |
58 } | |
59 /// | |
60 Point opSub(Point pt) { | |
61 Point pt2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
62 pt2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
63 pt2.y = y - pt.y; |
0 | 64 return pt2; |
65 } | |
66 /// | |
67 Rect opAdd(Size size) { | |
68 Rect rect; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
69 rect.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
70 rect.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
71 rect.width = size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
72 rect.height = size.height; |
0 | 73 return rect; |
74 } | |
75 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
76 return format("Point [x={}, y={}]", x, y); |
0 | 77 } |
78 } | |
79 | |
80 /// | |
81 struct Size { | |
82 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
|
83 float[2] _values = [0, 0]; |
0 | 84 public: |
85 /// | |
86 static Size opCall() { | |
87 Size size; | |
88 return size; | |
89 } | |
90 /// | |
91 static Size opCall(float width, float height) { | |
92 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
|
93 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
|
94 size.height = height; |
0 | 95 return size; |
96 } | |
97 /// | |
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 width() { return _values[0]; } |
0 | 99 /// |
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
|
100 void width(float f) { _values[0] = f; } |
0 | 101 /// |
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
|
102 float height() { return _values[1]; } |
0 | 103 /// |
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
|
104 void height(float f) { _values[1] = f; } |
0 | 105 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
106 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
|
107 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
108 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
|
109 /// |
0 | 110 Size opAdd(Size size) { |
111 Size size2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
112 size2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
113 size2.height = height + size.height; |
0 | 114 return size2; |
115 } | |
116 /// | |
117 Size opSub(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 opAdd(BorderSize border) { | |
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 + border.left + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
127 size2.height = height + border.top + border.bottom; |
0 | 128 return size2; |
129 } | |
130 /// | |
131 Size opSub(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 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
138 return format("Size [width={}, height={}]", width, height); |
0 | 139 } |
140 } | |
141 | |
142 /// | |
143 struct Rect { | |
144 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
|
145 float[4] _values = [0, 0, 0, 0]; |
0 | 146 public: |
147 static Rect opCall() { | |
148 Rect rect; | |
149 return rect; | |
150 } | |
151 static Rect opCall(float x, float y, float width, float height) { | |
152 Rect rect; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
153 rect.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
154 rect.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
155 rect.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
156 rect.height = height; |
0 | 157 return rect; |
158 } | |
159 /// | |
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 x() { return _values[0]; } |
0 | 161 /// |
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
|
162 void x(float f) { _values[0] = f; } |
0 | 163 /// |
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
|
164 float y() { return _values[1]; } |
0 | 165 /// |
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
|
166 void y(float f) { _values[1] = f; } |
0 | 167 /// |
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
|
168 float width() { return _values[2]; } |
0 | 169 /// |
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
|
170 void width(float f) { _values[2] = f; } |
0 | 171 /// |
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
|
172 float height() { return _values[3]; } |
0 | 173 /// |
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
|
174 void height(float f) { _values[3] = f; } |
0 | 175 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
176 float right() { return x + width; } |
0 | 177 /// |
98
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
178 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
|
179 /// |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
180 float bottom() { return y + height; } |
0 | 181 /// |
98
e7e36dca4768
Add setters for Rect.right and Rect.bottom.
Jordan Miner <jminer7@gmail.com>
parents:
97
diff
changeset
|
182 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
|
183 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
184 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
|
185 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
186 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
|
187 /// |
0 | 188 Rect opAdd(Rect rect) { |
189 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
190 rect2.x = x + rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
191 rect2.y = y + rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
192 rect2.width = width + rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
193 rect2.height = height + rect.height; |
0 | 194 return rect2; |
195 } | |
196 /// | |
197 Rect opSub(Rect rect) { | |
198 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
199 rect2.x = x - rect.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
200 rect2.y = y - rect.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
201 rect2.width = width - rect.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
202 rect2.height = height - rect.height; |
0 | 203 return rect2; |
204 } | |
205 /// | |
206 Rect opAdd(Point pt) { | |
207 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
208 rect2.x = x + pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
209 rect2.y = y + pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
210 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
211 rect2.height = height; |
0 | 212 return rect2; |
213 } | |
214 /// | |
215 Rect opSub(Point pt) { | |
216 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
217 rect2.x = x - pt.x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
218 rect2.y = y - pt.y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
219 rect2.width = width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
220 rect2.height = height; |
0 | 221 return rect2; |
222 } | |
223 /// | |
224 Rect opAdd(Size size) { | |
225 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
226 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
227 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
228 rect2.width = width + size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
229 rect2.height = height + size.height; |
0 | 230 return rect2; |
231 } | |
232 /// | |
233 Rect opSub(Size size) { | |
234 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
235 rect2.x = x; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
236 rect2.y = y; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
237 rect2.width = width - size.width; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
238 rect2.height = height - size.height; |
0 | 239 return rect2; |
240 } | |
241 /// | |
242 Rect opAdd(BorderSize border) { | |
243 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
244 rect2.x = x - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
245 rect2.y = y - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
246 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
|
247 rect2.height = height + border.top + border.bottom; |
0 | 248 return rect2; |
249 } | |
250 /// | |
251 Rect opSub(BorderSize border) { | |
252 Rect rect2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
253 rect2.x = x + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
254 rect2.y = y + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
255 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
|
256 rect2.height = height - border.top - border.bottom; |
0 | 257 return rect2; |
258 } | |
259 bool contains(Point pt) { | |
260 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom; | |
261 } | |
262 Rect getUnion(Rect rect) { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
263 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
|
264 auto y2 = min(y, rect.y); |
0 | 265 Rect rect2; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
266 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
|
267 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
|
268 rect2.x = x2; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
269 rect2.y = y2; |
0 | 270 return rect2; |
271 } | |
272 string toString() { | |
273 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
|
274 x, y, width, height); |
0 | 275 } |
276 } | |
277 unittest { | |
99
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
278 assert(Rect(2, 3, 5, 9).right == 7); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
279 assert(Rect(2, 3, 5, 9).bottom == 12); |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
280 // same |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
281 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
|
282 // second inside first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
283 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
|
284 // second outside first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
285 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
|
286 // second up left of first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
287 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
|
288 // second down right of first |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
289 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
|
290 // not overlapping |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
291 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
|
292 // negative |
8daa63a28707
Add more tests for Rect.getUnion().
Jordan Miner <jminer7@gmail.com>
parents:
98
diff
changeset
|
293 assert(Rect(-4, -5, 2, 3).getUnion(Rect(-10, -2, 1, 5)) == Rect(-10, -5, 8, 8)); |
0 | 294 } |
295 | |
296 /// | |
297 struct BorderSize { | |
298 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
|
299 float[4] _values = [0, 0, 0, 0]; |
0 | 300 public: |
301 static BorderSize opCall() { | |
302 BorderSize border; | |
303 return border; | |
304 } | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
305 static BorderSize opCall(float left, float top, float right, float bottom) { |
0 | 306 BorderSize border; |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
307 border.left = left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
308 border.top = top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
309 border.right = right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
310 border.bottom = bottom; |
0 | 311 return border; |
312 } | |
313 /// | |
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 left() { return _values[0]; } |
0 | 315 /// |
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
|
316 void left(float f) { _values[0] = f; } |
0 | 317 /// |
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
|
318 float top() { return _values[1]; } |
0 | 319 /// |
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
|
320 void top(float f) { _values[1] = f; } |
0 | 321 /// |
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
|
322 float right() { return _values[2]; } |
0 | 323 /// |
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
|
324 void right(float f) { _values[2] = f; } |
0 | 325 /// |
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
|
326 float bottom() { return _values[3]; } |
0 | 327 /// |
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
|
328 void bottom(float f) { _values[3] = f; } |
0 | 329 /// |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
330 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
|
331 /// |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
332 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
|
333 /// |
0 | 334 BorderSize opAdd(BorderSize border) { |
335 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
336 border2.left = left + border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
337 border2.right = right + border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
338 border2.top = top + border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
339 border2.bottom = bottom + border.bottom; |
0 | 340 return border2; |
341 } | |
342 /// | |
343 BorderSize opSub(BorderSize border) { | |
344 BorderSize border2; | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
345 border2.left = left - border.left; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
346 border2.right = right - border.right; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
347 border2.top = top - border.top; |
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
348 border2.bottom = bottom - border.bottom; |
0 | 349 return border2; |
350 } | |
351 string toString() { | |
97
dccadd297348
Use methods/properties instead of the instance variables directly.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
352 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
|
353 left, top, right, bottom); |
0 | 354 } |
355 } | |
356 | |
357 unittest { | |
101
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
358 Point pt; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
359 Size sz; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
360 Rect rect; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
361 BorderSize border; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
362 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
363 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
|
364 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
365 pt = Point(7, 9); |
0 | 366 assert(pt.x == 7); |
367 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
|
368 rect = pt + Size(15, 13); |
0 | 369 assert(rect == Rect(7, 9, 15, 13)); |
370 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
|
371 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
372 pt = Point(1, 2); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
373 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
|
374 pt[0] = 13; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
375 assert(pt[0] == 13); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
376 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
377 sz = Size(20, 30); |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
378 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
|
379 sz[1] = 35; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
380 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
|
381 |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
382 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
|
383 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
|
384 rect[2] = 5; |
1690ebff00a0
Add opIndex and opIndexAssign and tests to Point, Size, etc.
Jordan Miner <jminer7@gmail.com>
parents:
100
diff
changeset
|
385 assert(rect[2] == 5); |
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 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
|
388 assert(border[0] == 5 && border[1] == 6 && border[2] == 7 && border[3] == 8); |
0 | 389 } |
390 |