-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.pas
283 lines (238 loc) · 7.2 KB
/
Vector.pas
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
unit Vector;
{$MODE Delphi}
//
// Vector calculations unit, (c) Roelof Oomen, 2007
//
// Coordinate system
//
// Cartesian
// z|
// | / y
// | /
// __________|/_________ x
// /|
// / |
// / |
//
//
// Spherical coordinates are defined as (r, psi, theta) with psi being
// the azimuth angle (0-360 degrees, in the x-y plane, counterclockwise)
// and theta the polar/zenith angle (0-180 degrees, angle with z-axis).
//
// Cylindrical coordinates are defined as (r, psi, h)
// where r is the radius (distance to the z-axis), h is the
// height, i.e. the Z-coordinate, and psi the angle with the x-axis in
// the x-y plane, counterclockwise.
//
// WARNING: The definitions of the spherical coordinates differ from many texts,
// (common: r, theta, phi).
//
interface
type
// Cartesian
TVectorC = record
private
function getR: double; //inline;
public
x,
y,
z : double;
property r : double read getR; // Length
procedure makeUnit; //inline;
function isUnit : boolean; //inline;
procedure InitC(x, y, z : Double);
procedure Null; //inline;
function isNull : boolean; //inline;
// Operator overloadings - Delphi 2005 and later
class operator Negative(a: TVectorC) : TVectorC; //inline; // Negate
class operator Add(a: TVectorC; b: TVectorC): TVectorC; //inline;
class operator Subtract(a: TVectorC; b: TVectorC) : TVectorC; //inline;
class operator Multiply(a: Double; b: TVectorC) : TVectorC; //inline; // Multiply a vector by a scalar
class operator Multiply(a: TVectorC; b: Double) : TVectorC; //inline; // Multiply a vector by a scalar
class operator Multiply(a: TVectorC; b: TVectorC) : Double; //inline; // Dot product!
class operator Divide(a: TVectorC; b: Double) : TVectorC; //inline;
class operator Equal(a: TVectorC; b: TVectorC) : Boolean; //inline;
end;
// Spherical
TVectorS = record
theta,
psi,
r : double;
procedure Null; //inline;
function isNull : boolean; //inline;
class operator Equal(a: TVectorS; b: TVectorS) : Boolean; //inline;
class operator Multiply(a: TVectorS; b: TVectorS) : Double; //inline; // Dot product!
class operator Implicit(a : TVectorS) : TVectorC; //inline;
class operator Explicit(a : TVectorS) : TVectorC; //inline;
// Forward declaration of TVectorS is not possible, so these two casts are placed here
class operator Implicit(a : TVectorC) : TVectorS; //inline;
class operator Explicit(a : TVectorC) : TVectorS; //inline;
end;
// Cylindrical
{ TVectorCyl = record
r,
psi,
h : double;
class operator Implicit(a : TVectorCyl) : TVectorS; //inline;
class operator Explicit(a : TVectorCyl) : TVectorS; //inline;
end; }
implementation
uses
math, sysutils, LCLIntf, LCLType;
{ *** TVector *************************************************************** }
class operator TVectorC.Add(a, b: TVectorC): TVectorC;
begin
result.x:=a.x+b.x;
result.y:=a.y+b.y;
result.z:=a.z+b.z;
end;
class operator TVectorC.Subtract(a, b: TVectorC): TVectorC;
begin
result.x:=a.x-b.x;
result.y:=a.y-b.y;
result.z:=a.z-b.z;
end;
class operator TVectorC.Divide(a: TVectorC; b: Double): TVectorC;
begin
result.x:=a.x/b;
result.y:=a.y/b;
result.z:=a.z/b;
end;
class operator TVectorC.Equal(a, b: TVectorC): Boolean;
begin
result:=(a.x=b.x) and (a.y=b.y) and (a.z=b.z);
end;
function TVectorC.getR: double;
begin
result:=sqrt(sqr(x)+sqr(y)+sqr(z));
end;
procedure TVectorC.InitC(x, y, z: Double);
begin
self.x:=x;
self.y:=y;
self.z:=z;
end;
class operator TVectorC.Multiply(a, b: TVectorC): Double;
begin
result:=a.x*b.x+a.y*b.y+a.z*b.z;
end;
class operator TVectorC.Multiply(a: Double; b: TVectorC): TVectorC;
begin
result.x:=a*b.x;
result.y:=a*b.y;
result.z:=a*b.z;
end;
class operator TVectorC.Multiply(a: TVectorC; b: Double): TVectorC;
begin
result.x:=b*a.x;
result.y:=b*a.y;
result.z:=b*a.z;
end;
class operator TVectorC.Negative(a: TVectorC): TVectorC;
begin
result.x:=-a.x;
result.y:=-a.y;
result.z:=-a.z;
end;
function TVectorC.isNull: boolean;
begin
if ( (x=0) and (y=0) and (z=0) ) then
result:=true
else
result:=false;
end;
function TVectorC.isUnit: boolean;
begin
// Compensate for very minor differences (rounding errors etc)
result:=( RoundTo(r,-12)=1 );
end;
procedure TVectorC.makeUnit;
var
r_i:Double;
begin
r_i:=r;
x:=x/r_i;
y:=y/r_i;
z:=z/r_i;
end;
procedure TVectorC.null;
begin
x:=0;
y:=0;
z:=0;
end;
{ TVectorS }
function TVectorS.isNull: boolean;
begin
if (theta=0) and (psi=0) and (r=0) then
result:=true
else
result:=false;
end;
class operator TVectorS.Multiply(a, b: TVectorS): Double;
var
ax, ay, az, bx, by, bz : double;
begin
//result:=cos(a.psi-b.psi)*sin(a.theta)*sin(b.theta)+cos(a.theta)*cos(b.theta);
ax:=a.r*sin(a.theta)*cos(a.psi);
ay:=a.r*sin(a.theta)*sin(a.psi);
az:=a.r*cos(a.theta);
bx:=b.r*sin(b.theta)*cos(b.psi);
by:=b.r*sin(b.theta)*sin(b.psi);
bz:=b.r*cos(b.theta);
result:=ax*bx+ay*by+az*bz;
end;
procedure TVectorS.Null;
begin
theta:=0;
psi:=0;
r:=0;
end;
class operator TVectorS.Equal(a, b: TVectorS): Boolean;
begin
//inherited;
result:=(a.theta=b.theta) and (a.psi=b.psi) and (a.r=b.r);
end;
class operator TVectorS.Explicit(a: TVectorS): TVectorC;
begin
result.x:=a.r*sin(a.theta)*cos(a.psi);
result.y:=a.r*sin(a.theta)*sin(a.psi);
result.z:=a.r*cos(a.theta);
end;
class operator TVectorS.Implicit(a: TVectorS): TVectorC;
begin
result.x:=a.r*sin(a.theta)*cos(a.psi);
result.y:=a.r*sin(a.theta)*sin(a.psi);
result.z:=a.r*cos(a.theta);
end;
class operator TVectorS.Implicit(a: TVectorC): TVectorS;
begin
result.r:=a.r;
result.theta := arccos(a.z/result.r); // Use result.r instead of a.r, because
// a.r is calculated for each call.
// ArcTan2(Y,X) calculates ArcTan(Y/X), and returns an angle in the correct quadrant.
result.psi := arctan2(a.y,a.x);
end;
class operator TVectorS.Explicit(a: TVectorC): TVectorS;
begin
result.r:=sqrt(sqr(a.x)+sqr(a.y)+sqr(a.z));
result.theta := arccos(a.z/result.r);
// ArcTan2(Y,X) calculates ArcTan(Y/X), and returns an angle in the correct quadrant.
result.psi := arctan2(a.y,a.x);
end;
{ TVectorCyl }
{
class operator TVectorCyl.Explicit(a: TVectorCyl): TVectorS;
begin
result.r:=sqrt(sqr(a.r)+sqr(a.h));
result.psi:=a.psi;
result.theta:=arctan(a.r/a.h);
end;
class operator TVectorCyl.Implicit(a: TVectorCyl): TVectorS;
begin
result.r:=sqrt(sqr(a.r)+sqr(a.h));
result.psi:=a.psi;
result.theta:=arctan2(a.r,a.h);
end;
}
end.