-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvector.js
More file actions
193 lines (153 loc) · 4.48 KB
/
vector.js
File metadata and controls
193 lines (153 loc) · 4.48 KB
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
// Copyright 2011 Robert Scott Dionne. All rights reserved.
/**
* @fileoverview Defines the vector object.
* @author robertsdionne@gmail.com (Robert Scott Dionne)
*/
/**
* Constructs a new vector from the given coordinates.
* @param {number=} opt_x
* @param {number=} opt_y
* @param {number=} opt_z
* @constructor
* @extends {bouncingball.Quaternion}
*/
bouncingball.Vector = function(opt_x, opt_y, opt_z) {
/**
* @type {number}
*/
this.scalar = 0;
/**
* @type {bouncingball.Vector}
*/
this.vector = this;
/**
* @type {number}
*/
this.x = opt_x || 0;
/**
* @type {number}
*/
this.y = opt_y || 0;
/**
* @type {number}
*/
this.z = opt_z || 0;
};
bouncingball.inherits(bouncingball.Vector, bouncingball.Quaternion);
bouncingball.Vector.I = new bouncingball.Vector(1, 0, 0);
bouncingball.Vector.J = new bouncingball.Vector(0, 1, 0);
bouncingball.Vector.K = new bouncingball.Vector(0, 0, 1);
/**
* @return {bouncingball.Vector} The negation of this vector.
*/
bouncingball.Vector.prototype.negate = function() {
return new bouncingball.Vector(-this.x, -this.y, -this.z);
};
/**
* @return {bouncingball.Vector} this vector normalized
*/
bouncingball.Vector.prototype.normalized = function() {
return this.over(this.magnitude());
};
bouncingball.Vector.prototype.toDual = function() {
return new bouncingball.DualVector(
new bouncingball.DualNumber(this.x),
new bouncingball.DualNumber(this.y),
new bouncingball.DualNumber(this.z));
};
/**
* @return {number} The magnitude of this vector.
*/
bouncingball.Vector.prototype.magnitude = function() {
return Math.sqrt(this.dot(this));
};
/**
* @return {number} The square magnitude of this vector.
*/
bouncingball.Vector.prototype.magnitudeSquared = function() {
return this.dot(this);
};
/**
* @return {bouncingball.Vector|bouncingball.Quaternion} The sum of this and that.
* @param {bouncingball.Vector|number|bouncingball.Quaternion} that
*/
bouncingball.Vector.prototype.plus = function(that) {
if (that instanceof bouncingball.Vector) {
return new bouncingball.Vector(
this.x + that.x,
this.y + that.y,
this.z + that.z);
} else {
return bouncingball.Quaternion.prototype.plus.call(this, that);
}
};
/**
* @return {bouncingball.Vector|bouncingball.Quaternion} The difference of this and that.
* @param {bouncingball.Vector|number|bouncingball.Quaternion} that
*/
bouncingball.Vector.prototype.minus = function(that) {
if (that instanceof bouncingball.Vector) {
return new bouncingball.Vector(
this.x - that.x,
this.y - that.y,
this.z - that.z);
} else {
return bouncingball.Quaternion.prototype.plus.call(this, that);
}
};
/**
* @return {bouncingball.Vector|bouncingball.Quaternion} The product of this and that.
* @param {number|bouncingball.Quaternion} that
*/
bouncingball.Vector.prototype.times = function(that) {
if (typeof that === 'number') {
return new bouncingball.Vector(this.x * that, this.y * that, this.z * that);
} else {
return bouncingball.Quaternion.prototype.times.call(this, that);
}
};
/**
* @return {bouncingball.Vector|bouncingball.Quaternion} The quotient of this and that.
* @param {number|bouncingball.Quaternion} that
*/
bouncingball.Vector.prototype.over = function(that) {
if (typeof that === 'number') {
return new bouncingball.Vector(this.x / that, this.y / that, this.z / that);
} else {
return bouncingball.Quaternion.prototype.over.call(this, that);
}
};
/**
* @return {bouncingball.Vector} The cross product of this and that.
* @param {bouncingball.Vector} that
*/
bouncingball.Vector.prototype.cross = function(that) {
return new bouncingball.Vector(
this.y * that.z - this.z * that.y,
this.z * that.x - this.x * that.z,
this.x * that.y - this.y * that.x);
};
/**
* @return {number} The dot product of this and that.
* @param {bouncingball.Vector} that
*/
bouncingball.Vector.prototype.dot = function(that) {
return this.x * that.x + this.y * that.y + this.z * that.z;
};
bouncingball.Vector.prototype.toMatrix = function() {
return [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
-this.x, -this.y, -this.z, 1.0
];
};
bouncingball.Vector.prototype.toArray = function() {
return [this.x, this.y, this.z];
};
/**
* @return {string} A string representation of this vector.
*/
bouncingball.Vector.prototype.toString = function() {
return this.x + 'i + ' + this.y + 'j + ' + this.z + 'k';
};