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
|
//==================================================================
// Copyright 2002, softSurfer (www.softsurfer.com)
// This code may be freely used and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from it's use.
// Users of this code must verify correctness for their application.
//==================================================================
#include "point.h"
#include "vector.h"
//==================================================================
// Vector Class Methods
//==================================================================
//------------------------------------------------------------------
// Unary Ops
//------------------------------------------------------------------
// Unary minus
Vector Vector::operator-() {
Vector v;
v.x = -x; v.y = -y; v.z = -z;
v.dimn = dimn;
return v;
}
// Unary 2D perp operator
Vector Vector::operator~() {
if (dimn != 2) err = Edim; // and continue anyway
Vector v;
v.x = -y; v.y = x; v.z = z;
v.dimn = dimn;
return v;
}
//------------------------------------------------------------------
// Scalar Ops
//------------------------------------------------------------------
// Scalar multiplication
Vector operator*( int c, Vector w ) {
Vector v;
v.x = c * w.x;
v.y = c * w.y;
v.z = c * w.z;
v.dimn = w.dim();
return v;
}
Vector operator*( double c, Vector w ) {
Vector v;
v.x = c * w.x;
v.y = c * w.y;
v.z = c * w.z;
v.dimn = w.dim();
return v;
}
Vector operator*( Vector w, int c ) {
Vector v;
v.x = c * w.x;
v.y = c * w.y;
v.z = c * w.z;
v.dimn = w.dim();
return v;
}
Vector operator*( Vector w, double c ) {
Vector v;
v.x = c * w.x;
v.y = c * w.y;
v.z = c * w.z;
v.dimn = w.dim();
return v;
}
// Scalar division
Vector operator/( Vector w, int c ) {
Vector v;
v.x = w.x / c;
v.y = w.y / c;
v.z = w.z / c;
v.dimn = w.dim();
return v;
}
Vector operator/( Vector w, double c ) {
Vector v;
v.x = w.x / c;
v.y = w.y / c;
v.z = w.z / c;
v.dimn = w.dim();
return v;
}
//------------------------------------------------------------------
// Arithmetic Ops
//------------------------------------------------------------------
Vector Vector::operator+( Vector w ) {
Vector v;
v.x = x + w.x;
v.y = y + w.y;
v.z = z + w.z;
v.dimn = max( dimn, w.dim());
return v;
}
Vector Vector::operator-( Vector w ) {
Vector v;
v.x = x - w.x;
v.y = y - w.y;
v.z = z - w.z;
v.dimn = max( dimn, w.dim());
return v;
}
//------------------------------------------------------------------
// Products
//------------------------------------------------------------------
// Inner Dot Product
double Vector::operator*( Vector w ) {
return (x * w.x + y * w.y + z * w.z);
}
// 2D Exterior Perp Product
double Vector::operator|( Vector w ) {
if (dimn != 2) err = Edim; // and continue anyway
return (x * w.y - y * w.x);
}
// 3D Exterior Cross Product
Vector Vector::operator^( Vector w ) {
Vector v;
v.x = y * w.z - z * w.y;
v.y = z * w.x - x * w.z;
v.z = x * w.y - y * w.x;
v.dimn = 3;
return v;
}
//------------------------------------------------------------------
// Shorthand Ops
//------------------------------------------------------------------
Vector& Vector::operator*=( double c ) { // vector scalar mult
x *= c;
y *= c;
z *= c;
return *this;
}
Vector& Vector::operator/=( double c ) { // vector scalar div
x /= c;
y /= c;
z /= c;
return *this;
}
Vector& Vector::operator+=( Vector w ) { // vector increment
x += w.x;
y += w.y;
z += w.z;
dimn = max(dimn, w.dim());
return *this;
}
Vector& Vector::operator-=( Vector w ) { // vector decrement
x -= w.x;
y -= w.y;
z -= w.z;
dimn = max(dimn, w.dim());
return *this;
}
Vector& Vector::operator^=( Vector w ) { // 3D exterior cross product
double ox=x, oy=y, oz=z;
x = oy * w.z - oz * w.y;
y = oz * w.x - ox * w.z;
z = ox * w.y - oy * w.x;
dimn = 3;
return *this;
}
//------------------------------------------------------------------
// Special Operations
//------------------------------------------------------------------
void Vector::normalize() { // convert to unit length
double ln = sqrt( x*x + y*y + z*z );
if (ln == 0) return; // do nothing for nothing
x /= ln;
y /= ln;
z /= ln;
}
Vector sum( int n, int c[], Vector w[] ) { // vector sum
int maxd = 0;
Vector v;
for (int i=0; i<n; i++) {
if (w[i].dim() > maxd)
maxd = w[i].dim();
}
v.dimn = maxd;
for (int i=0; i<n; i++) {
v.x += c[i] * w[i].x;
v.y += c[i] * w[i].y;
v.z += c[i] * w[i].z;
}
return v;
}
Vector sum( int n, double c[], Vector w[] ) { // vector sum
int maxd = 0;
Vector v;
for (int i=0; i<n; i++) {
if (w[i].dim() > maxd)
maxd = w[i].dim();
}
v.dimn = maxd;
for (int i=0; i<n; i++) {
v.x += c[i] * w[i].x;
v.y += c[i] * w[i].y;
v.z += c[i] * w[i].z;
}
return v;
}
|