0
|
1 module tk.geometry2;
|
|
2
|
1
|
3 private import std.stdio;
|
|
4 private import std.math;
|
|
5
|
0
|
6 struct Point2 {
|
1
|
7 this(in double x, in double y) {
|
|
8 _x = x;
|
|
9 _y = y;
|
|
10 }
|
|
11
|
|
12 Point2 opAdd(in Vector2 v) const {
|
|
13 return Point2(_x + v._x, _y + v._y);
|
|
14 }
|
|
15
|
|
16 Point2 opSub(in Vector2 v) const {
|
|
17 return Point2(_x - v._x, _y - v._y);
|
|
18 }
|
|
19
|
|
20 Vector2 opSub(in Point2 p) const {
|
|
21 return Vector2(_x - p._x, _y - p._y);
|
|
22 }
|
|
23
|
|
24 string toString() /* const */ {
|
|
25 return std.string.format("(%f, %f)", _x, _y);
|
|
26 }
|
|
27
|
|
28 double x() const { return _x; }
|
|
29 double y() const { return _y; }
|
|
30
|
|
31 private {
|
|
32 double _x, _y;
|
|
33 }
|
|
34 }
|
|
35
|
|
36 struct Vector2 {
|
|
37 this(in double x, in double y) {
|
0
|
38 _x = x;
|
|
39 _y = y;
|
|
40 }
|
|
41
|
1
|
42 Vector2 opAdd(in Vector2 v) const {
|
|
43 return Vector2(_x + v._x, _y + v._y);
|
|
44 }
|
|
45
|
|
46 Vector2 opSub(in Vector2 v) const {
|
|
47 return Vector2(_x - v._x, _y - v._y);
|
|
48 }
|
|
49
|
|
50 Vector2 opNeg() const {
|
|
51 return Vector2(-_x, -_y);
|
|
52 }
|
|
53
|
|
54 Vector2 opMul_r(double d) const {
|
|
55 return Vector2(d * _x, d * _y);
|
|
56 }
|
|
57
|
|
58 Vector2 opDiv(double d) const {
|
|
59 return Vector2(_x / d, _y / d);
|
|
60 }
|
|
61
|
|
62 double length() const {
|
|
63 return sqrt(_x * _x + _y * _y);
|
|
64 }
|
|
65
|
|
66 string toString() /* const */ {
|
|
67 return std.string.format("[%f, %f]", _x, _y);
|
|
68 }
|
|
69
|
|
70 double x() const { return _x; }
|
|
71 double y() const { return _y; }
|
|
72
|
0
|
73 private {
|
1
|
74 double _x, _y;
|
0
|
75 }
|
|
76 }
|
1
|
77
|
|
78 struct Rectangle2 {
|
|
79 this(in Point2 position, Vector2 size) {
|
|
80 this(position.x, position.y, size.x, size.y);
|
|
81 }
|
|
82
|
|
83 this(Point2 corner1, Point2 corner2) {
|
|
84 this(corner1.x, corner1.y, corner2.x - corner1.x, corner2.y - corner1.y);
|
|
85 }
|
|
86
|
|
87 // Intersection
|
|
88 Rectangle2 opAnd(in Rectangle2 r) const {
|
|
89 // FIXME
|
|
90 return Rectangle2(3.2, 5.3, 3.2, 2.3);
|
|
91 }
|
|
92
|
|
93 // Union
|
|
94 Rectangle2 opOr(in Rectangle2 r) const {
|
|
95 // FIXME
|
|
96 return Rectangle2(3.2, 5.3, 5.8, 2.1);
|
|
97 }
|
|
98
|
|
99 string toString() {
|
|
100 return std.string.format("{%s, %s}", mPosition, mSize);
|
|
101 }
|
|
102
|
|
103 private {
|
|
104 this(double x, double y, double w, double h) {
|
|
105 if (w < 0.0) { x += w; w = -w; }
|
|
106 if (h < 0.0) { y += h; h = -h; }
|
|
107 mPosition = Point2(x, y);
|
|
108 mSize = Vector2(w, h);
|
|
109 }
|
|
110
|
|
111 Point2 mPosition;
|
|
112 Vector2 mSize;
|
|
113 }
|
|
114 }
|