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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
1 // Written in the D programming language
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
2 // www.digitalmars.com/d/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
3
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
4 /*
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
5 * The contents of this file are subject to the Mozilla Public License Version
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
6 * 1.1 (the "License"); you may not use this file except in compliance with
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
7 * the License. You may obtain a copy of the License at
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
8 * http://www.mozilla.org/MPL/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
9 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
10 * Software distributed under the License is distributed on an "AS IS" basis,
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
12 * for the specific language governing rights and limitations under the
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
13 * License.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
14 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
15 * The Original Code is the Dynamin library.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
16 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
19 * the Initial Developer. All Rights Reserved.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
20 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
21 * Contributor(s):
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
22 * Jordan Miner <jminer7@gmail.com>
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
23 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
24 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
25
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
26 module dynamin.painting.coordinates;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
27
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
28 import dynamin.core.string;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
29 import dynamin.core.math;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
30
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
31 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
32 struct Point {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
35 public:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
36 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
37 static Point opCall() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
38 Point pt;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
39 return pt;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
40 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
41 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
42 static Point opCall(float x, float y) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
46 return pt;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
47 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
61 Point opNeg() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
65 return pt2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
66 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
67 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
68 Point opAdd(Point pt) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
72 return pt2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
73 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
74 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
75 Point opSub(Point pt) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
79 return pt2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
80 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
81 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
82 Rect opAdd(Size size) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
88 return rect;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
89 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
92 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
93 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
94
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
95 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
96 struct Size {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
99 public:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
100 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
101 static Size opCall() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
102 Size size;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
103 return size;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
104 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
105 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
106 static Size opCall(float width, float height) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
110 return size;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
111 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
125 Size opAdd(Size size) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
129 return size2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
130 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
131 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
132 Size opSub(Size size) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
136 return size2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
137 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
138 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
139 Size opAdd(BorderSize border) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
143 return size2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
144 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
145 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
146 Size opSub(BorderSize border) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
150 return size2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
151 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
154 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
155 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
156
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
157 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
158 struct Rect {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
161 public:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
162 static Rect opCall() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
163 Rect rect;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
164 return rect;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
165 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
166 static Rect opCall(float x, float y, float width, float height) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
172 return rect;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
173 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
203 Rect opAdd(Rect rect) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
209 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
210 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
211 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
212 Rect opSub(Rect rect) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
218 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
219 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
220 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
221 Rect opAdd(Point pt) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
227 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
228 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
229 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
230 Rect opSub(Point pt) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
236 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
237 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
238 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
239 Rect opAdd(Size size) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
245 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
246 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
247 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
248 Rect opSub(Size size) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
254 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
255 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
256 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
257 Rect opAdd(BorderSize border) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
263 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
264 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
265 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
266 Rect opSub(BorderSize border) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
272 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
273 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
274 bool contains(Point pt) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
275 return pt.x >= x && pt.y >= y && pt.x < right && pt.y < bottom;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
276 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
285 return rect2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
286 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
287 string toString() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
290 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
291 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
309 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
310
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
311 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
312 struct BorderSize {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
315 public:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
316 static BorderSize opCall() {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
317 BorderSize border;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
318 return border;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
326 return border;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
327 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
349 BorderSize opAdd(BorderSize border) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
355 return border2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
356 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
357 ///
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
358 BorderSize opSub(BorderSize border) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
364 return border2;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
365 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
369 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
370 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
371
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
381 assert(pt.x == 7);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
384 assert(rect == Rect(7, 9, 15, 13));
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
404 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
405