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
|
{%norun}
program project1;
{$mode delphi}
uses
SysUtils, Math;
type
TVector3 = record
public
// constructor
// constructor create(const x: Single; const y: Single; const z: Single);
// overloaded operators
class operator negative(const a : TVector3) : TVector3; inline;
class operator add(const a, b : TVector3) : TVector3; inline;
class operator subtract(const a, b : TVector3) : TVector3; inline;
class operator multiply(const a, b : TVector3) : TVector3; inline;
class operator multiply(const a : single; b : TVector3) : TVector3; inline;
class operator multiply(const a : TVector3; b : single) : TVector3; inline;
class operator equal(const a, b : TVector3) : boolean; inline;
// methods
function normalize : TVector3; inline; // normalized vector
function length : single; inline; // length of a vector
function dot(const v : TVector3) : single; inline; // dot product
function cross(const v : TVector3) : TVector3; inline; // cross product
// variant record part
case Boolean of
TRUE: (x : single;
y : single;
z : single; );
FALSE: (e : array[0..2] of single; );
end;
// constants
const
X_AXIS : TVector3 = (x: 1.0; y: 1.0; z: 1.0);
Y_AXIS : TVector3 = (x: 0.0; y: 1.0; z: 0.0);
Z_AXIS : TVector3 = (x: 0.0; y: 0.0; z: 1.0);
ZERO : TVector3 = (x: 0.0; y: 0.0; z: 0.0);
ONE : TVector3 = (x: 1.0; y: 1.0; z: 1.0);
JITTERED_X_AXIS : TVector3 = (x: 1.0; y: 0.0072; z: 0.0034);
JITTERED_Y_AXIS : TVector3 = (x: 0.0072; y: 1.0; z: 0.0034);
JITTERED_Z_AXIS : TVector3 = (x: 0.0034; y: 0.0072; z: 1.0);
// constructor
{
constructor TVector3.create(const x: Single; const y: Single; const z: Single);
begin
self.x := x;
self.y := y;
self.z := z;
end;
}
// overloaded operators
class operator TVector3.negative(const a : TVector3) : TVector3;
begin
Result.x := -a.x;
Result.y := -a.y;
Result.z := -a.z;
end;
class operator TVector3.add(const a, b : TVector3) : TVector3;
begin
Result.x := a.x + b.x;
Result.y := a.y + b.y;
Result.z := a.z + b.z;
end;
class operator TVector3.subtract(const a, b : TVector3) : TVector3;
begin
Result.x := a.x - b.x;
Result.y := a.y - b.y;
Result.z := a.z - b.z;
end;
class operator TVector3.multiply(const a, b : TVector3) : TVector3;
begin
Result.x := a.x * b.x;
Result.y := a.y * b.y;
Result.z := a.z * b.z;
end;
class operator TVector3.multiply(const a : single; b : TVector3) : TVector3;
begin
Result.x := a * b.x;
Result.y := a * b.y;
Result.z := a * b.z;
end;
class operator TVector3.multiply(const a : TVector3; b : single) : TVector3;
begin
Result.x := a.x * b;
Result.y := a.y * b;
Result.z := a.z * b;
end;
class operator TVector3.equal(const a, b : TVector3) : boolean;
begin
Result := (a.x = b.x) and (a.y = b.y) and (a.z = b.z);
end;
// methods
function TVector3.normalize : TVector3;
var
invLen : single;
begin
invLen := 1.0 / sqrt(x * x + y * y + z * z);
Result.x := x * invLen;
Result.y := y * invLen;
Result.z := z * invLen;
end;
function TVector3.length : single;
begin
Result := sqrt(x * x + y * y + z * z);
end;
function TVector3.dot(const v : TVector3) : single;
begin
Result := x * v.x + y * v.y + z * v.z;
end;
function TVector3.cross(const v : TVector3) : TVector3;
begin
Result.x := y * v.z - z * v.y;
Result.y := z * v.x - x * v.z;
Result.z := x * v.y - y * v.x;
end;
// functions
function init(vx, vy, vz : single) : TVector3;
begin
Result.x := vx;
Result.y := vy;
Result.z := vz;
end;
function unitize(const v : TVector3) : TVector3;
var
invLen : single;
begin
invLen := 1.0 / sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
Result.x := v.x * invLen;
Result.y := v.y * invLen;
Result.z := v.z * invLen;
end;
function dot(const a, b : TVector3) : single;
begin
Result := a.x * b.x + a.y * b.y + a.z * b.z;
end;
function cross(const a, b : TVector3) : TVector3;
begin
Result.x := a.y * b.z - a.z * b.y;
Result.y := a.z * b.x - a.x * b.z;
Result.z := a.x * b.y - a.y * b.x;
end;
// output functions
function str(v : TVector3) : string; overload;
begin
Result := '[ '
+ FloatToStr(v.x) + ', '
+ FloatToStr(v.y) + ', '
+ FloatToStr(v.z)
+ '] ';
end;
begin
end.
|