comparison doodle/tk/geometry.d @ 100:a274d16ab6ce

struct initialisers
author David Bryant <bagnose@gmail.com>
date Mon, 18 Oct 2010 18:10:02 +1030
parents 535bae7a7305
children bc5baa585b32
comparison
equal deleted inserted replaced
99:ad672ab258c5 100:a274d16ab6ce
22 // 22 //
23 // A location in 2D space 23 // A location in 2D space
24 // 24 //
25 25
26 struct Point { 26 struct Point {
27 static immutable Point DEFAULT = Point(0.0, 0.0);
28
29 this(in double x, in double y) { 27 this(in double x, in double y) {
30 _x = x; 28 _x = x;
31 _y = y; 29 _y = y;
32 } 30 }
33 31
49 47
50 double x() const { return _x; } 48 double x() const { return _x; }
51 double y() const { return _y; } 49 double y() const { return _y; }
52 50
53 private { 51 private {
54 double _x, _y; 52 double _x = 0.0, _y = 0.0;
55 } 53 }
56 } 54 }
57 55
58 Point minExtents(in Point a, in Point b) { 56 Point minExtents(in Point a, in Point b) {
59 return Point(min(a.x, b.x), min(a.y, b.y)); 57 return Point(min(a.x, b.x), min(a.y, b.y));
66 // 64 //
67 // The displacement between two locations in 2D space 65 // The displacement between two locations in 2D space
68 // 66 //
69 67
70 struct Vector { 68 struct Vector {
71 static Vector DEFAULT = Vector(0.0, 0.0);
72
73 this(in double x, in double y) { 69 this(in double x, in double y) {
74 _x = x; 70 _x = x;
75 _y = y; 71 _y = y;
76 } 72 }
77 73
107 103
108 double x() const { return _x; } 104 double x() const { return _x; }
109 double y() const { return _y; } 105 double y() const { return _y; }
110 106
111 private { 107 private {
112 double _x, _y; 108 double _x = 0.0, _y = 0.0;
113 } 109 }
114 } 110 }
115 111
116 /* 112 /*
117 Vector normal(in Vector v) { 113 Vector normal(in Vector v) {
133 // a point defining the bottom left corner 129 // a point defining the bottom left corner
134 // a vector defining the displacement to the upper right corner 130 // a vector defining the displacement to the upper right corner
135 // 131 //
136 132
137 struct Rectangle { 133 struct Rectangle {
138 static Rectangle DEFAULT;
139
140 static this() {
141 DEFAULT = Rectangle(Point.DEFAULT, Vector.DEFAULT);
142 }
143
144 /* 134 /*
145 static Rectangle from_arbitrary_corners(in Point corner1, in Point corner) { 135 static Rectangle from_arbitrary_corners(in Point corner1, in Point corner) {
146 } 136 }
147 */ 137 */
148 138
175 double area() const { return _size.x * _size.y; } 165 double area() const { return _size.x * _size.y; }
176 166
177 // Intersection 167 // Intersection
178 Rectangle opAnd(in Rectangle r) const { 168 Rectangle opAnd(in Rectangle r) const {
179 if (invalid() || r.invalid()) { 169 if (invalid() || r.invalid()) {
180 return DEFAULT; 170 return Rectangle();
181 } 171 }
182 else { 172 else {
183 Point max = minExtents(corner1(), r.corner1()); 173 Point max = minExtents(corner1(), r.corner1());
184 Point min = maxExtents(corner0(), r.corner0()); 174 Point min = maxExtents(corner0(), r.corner0());
185 175
186 if (max.x < min.x || max.y < min.y) { 176 if (max.x < min.x || max.y < min.y) {
187 return DEFAULT; 177 return Rectangle();
188 } 178 }
189 else { 179 else {
190 return Rectangle(min, max); 180 return Rectangle(min, max);
191 } 181 }
192 } 182 }