/
Rectangle.zn
160 lines (128 loc) · 4.85 KB
/
Rectangle.zn
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
//! zinc
library Rectangle {
public struct Rectangle {
real cx, cy, tx, ty, lx, ly, rx, ry, bx, by;
real tlx, tly, trx, try, blx, bly, brx, bry;
real radians;
real width, height, diagonal;
static method new(real cx, real cy, real width, real height, real radians) -> thistype {
return thistype.allocate().setData(cx, cy, width, height, radians);
}
method setData(real cx, real cy, real width, real height, real radians) -> thistype {
this.cx = cx;
this.cy = cy;
this.width = width;
this.height = height;
return setRadians(radians);
}
method setCenter(real cx, real cy, real radians) -> thistype {
this.cx = cx;
this.cy = cy;
return setRadians(radians);
}
method setSize(real width, real height, real radians) -> thistype {
this.width = width;
this.height = height;
return setRadians(radians);
}
method setRadians(real radians) -> thistype {
real wa = (bj_PI - Atan(height / width) * 2) * .5;
real nr, d;
real hw = width * .5;
real hh = height * .5;
real hp = bj_PI * .5;
diagonal = SquareRoot(width * width + height * height);
d = diagonal * .5;
this.radians = radians - 2 * bj_PI * R2I(radians / (bj_PI * 2));
tx = cx + hh * Cos(this.radians);
ty = cy + hh * Sin(this.radians);
nr = radians + hp;
lx = cx + hw * Cos(nr);
ly = cy + hw * Sin(nr);
nr = radians - hp;
rx = cx + hw * Cos(nr);
ry = cy + hw * Sin(nr);
nr = radians - wa;
trx = cx + d * Cos(nr);
try = cy + d * Sin(nr);
nr = radians + wa;
tlx = cx + d * Cos(nr);
tly = cy + d * Sin(nr);
radians = radians + bj_PI;
bx = cx + hh * Cos(radians);
by = cy + hh * Sin(radians);
nr = radians - wa;
blx = cx + d * Cos(nr);
bly = cy + d * Sin(nr);
nr = radians + wa;
brx = cx + d * Cos(nr);
bry = cy + d * Sin(nr);
return this;
}
method translateOffset(real dx, real dy) -> thistype {
cx += dx;
cy += dy;
tx += dx;
ty += dy;
lx += dx;
ly += dy;
rx += dx;
ry += dy;
bx += dx;
by += dy;
tlx += dx;
tly += dy;
trx += dx;
try += dy;
blx += dx;
bly += dy;
brx += dx;
bry += dy;
return this;
}
method translatePolar(real distance, real radians) -> thistype {
real x = cx + distance * Cos(radians);
real y = cy + distance * Sin(radians);
return translateOffset(x - cx, y - cy);
}
method rotate(real ox, real oy, real radians) -> thistype {
real cos = Cos(radians);
real sin = Sin(radians);
real xn, yn;
cx -= ox;
cy -= oy;
xn = cx * cos - cy * sin;
yn = cx * sin + cy * cos;
cx = xn + rx;
cy = yn + ry;
return setRadians(this.radians + radians);
}
method distanceXY(real x, real y) -> real {
real a = 0 - radians;
real cos = Cos(a);
real sin = Sin(a);
real xn, yn, dx , dy, d;
real hh = height * .5;
real hw = width * .5;
real xmin = cx - hh;
real xmax = cx + hh;
real ymin = cy - hw;
real ymax = cy + hw;
x = x - cx;
y = y - cy;
xn = x * cos - y * sin;
yn = x * sin + y * cos;
x = xn + cx;
y = yn + cy;
dx = RMaxBJ(0, RMaxBJ(xmin - x, x - xmax));
dy = RMaxBJ(0, RMaxBJ(ymin - y, y - ymax));
d = dx * dx + dy * dy;
if (d > 0) {
return 0 - d;
}
d = RMinBJ(x - xmin, RMinBJ(xmax - x, RMinBJ(y - ymin, ymax - y)));
return d * d;
}
}
}
//! endzinc