-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.d
executable file
·302 lines (208 loc) · 5.17 KB
/
Vector.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
module Vector;
private import Log;
private import SofuResource;
private import Sofu.Sofu;
private import Misc;
const double M_PI = 3.14159265358979323846;
class Vector2d
{
public:
this()
{
}
this(double x, double y)
{
_x = x;
_y = y;
}
this(Sofu.List.List list)
{
_x = list.value(0).toDouble();
_y = list.value(1).toDouble();
}
this(Vector2d cpy)
{
_x = cpy._x;
_y = cpy._y;
}
double x()
{
return _x;
}
void x(double x)
{
_x = x;
}
double y()
{
return _y;
}
void y(double y)
{
_y = y;
}
Vector2d opAssign(Vector2d other) {
_x = other._x;
_y = other._y;
return this;
}
Vector2d opAdd(Vector2d other)
{
return new Vector2d(_x + other._x, _y + other._y);
}
Vector2d opMul(double scalar)
{
return new Vector2d(_x * scalar, _y * scalar);
}
Vector2d opDiv(double scalar)
{
return new Vector2d(_x / scalar, _y / scalar);
}
Vector2d opSub(Vector2d other)
{
return new Vector2d(_x - other._x, _y - other._y);
}
Vector2d opNeg()
{
return new Vector2d(-_x, -_y);
}
void opAddAssign(Vector2d other)
{
_x += other._x;
_y += other._y;
}
private:
double _x = 0.0;
double _y = 0.0;
}
const double D_TO_R = M_PI / 180.0;
const double R_TO_D = 180.0 / M_PI;
double degreesToRadians(double angle) {
return angle * D_TO_R;
}
double radiansToDegrees(double angle) {
return angle * R_TO_D;
}
class Rect
{
this(Vector2d v1, Vector2d v2)
{
_v1 = new Vector2d(v1);
_v2 = new Vector2d(v2);
}
this(double x1, double y1, double w, double h)
{
this(new Vector2d(x1, y1), new Vector2d(x1 + w, y1 + h));
}
this(Rect rect) {
_v1 = new Vector2d(rect._v1);
_v2 = new Vector2d(rect._v2);
}
Vector2d p1()
{
return _v1;
}
Vector2d p2()
{
return _v2;
}
double width() {
return _v2.x() - _v1.x();
}
double height() {
return _v2.y() - _v1.y();
}
/+ A vector pointing from the top left to the lower right corner +/
Vector2d diagonale()
{
return _v2 - _v1;
}
double top()
{
return _v1.y();
}
double bottom() {
return _v2.y();
}
double left() {
return _v1.x();
}
double right() {
return _v2.x();
}
Vector2d center() {
return _v1 + (diagonale() / 2);
}
Vector2d topleft() {
return new Vector2d(_v1);
}
Vector2d topright() {
return new Vector2d(_v2.x(), _v1.y());
}
Vector2d bottomleft() {
return new Vector2d(_v1.x(), _v2.y());
}
Vector2d bottomright() {
return new Vector2d(_v2);
}
double centerx() {
return center().x();
}
double centery() {
return center().y();
}
Rect move(Vector2d by) {
return new Rect(_v1 + by, _v2 + by);
}
Rect moveToTop(double top) {
Vector2d size = diagonale();
Vector2d nv1 = new Vector2d(_v1.x(), top);
Vector2d nv2 = nv1 + size;
Rect result = new Rect(nv1, nv2);
return result;
}
Rect moveToBottom(double bottom) {
Vector2d size = diagonale();
Vector2d nv2 = new Vector2d(_v2.x(), bottom);
Vector2d nv1 = nv2 - size;
Rect result = new Rect(nv1, nv2);
return result;
}
Rect moveToLeft(double left) {
Vector2d size = diagonale();
Vector2d nv1 = new Vector2d(left, _v1.y());
Vector2d nv2 = nv1 + size;
Rect result = new Rect(nv1, nv2);
return result;
}
Rect moveToRight(double right) {
Vector2d size = diagonale();
Vector2d nv2 = new Vector2d(right, _v2.y());
Vector2d nv1 = nv2 - size;
Rect result = new Rect(nv1, nv2);
return result;
}
bit containsPoint(Vector2d vec)
{
bit result = vec.x() > _v1.x() && vec.y() > _v1.y()
&& vec.x() < _v2.x() && vec.y() < _v2.y();
return result;
}
Rect overlaps(Rect rect) {
double top = max(top(), rect.top());
double left = max(left(), rect.left());
double bottom = min(bottom(), rect.bottom());
double right = min(right(), rect.right());
if(bottom > top && right > left) {
Rect result = new Rect(left, top, right-left, bottom-top);
return result;
}
return null;
}
invariant
{
assert(_v1.x() <= _v2.x() && _v1.y() <= _v2.y());
}
private:
Vector2d _v1, _v2;
}