-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
145 lines (126 loc) · 4.15 KB
/
Copy pathvector.js
File metadata and controls
145 lines (126 loc) · 4.15 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
// Vector class
class Vector {
constructor(x,y,z) {
this.set(x,y,z);
} // end constructor
// sets the components of a vector
set(x,y,z) {
if ((typeof(x) !== "number") || (typeof(y) !== "number") || (typeof(z) !== "number"))
throw "vector component not a number";
else
this.x = x; this.y = y; this.z = z;
} // end vector set
// copy the passed vector into this one
copy(v) {
try {
if (!(v instanceof Vector))
throw "Vector.copy: non-vector parameter";
else
this.x = v.x; this.y = v.y; this.z = v.z;
} // end try
catch(e) {
console.log(e);
}
}
length() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2));
}
toConsole(prefix) {
console.log(prefix+"["+this.x+","+this.y+","+this.z+"]");
} // end to console
// static dot method
static dot(v1,v2) {
if (!(v1 instanceof Vector) || !(v2 instanceof Vector))
throw "Vector.dot: non-vector parameter";
else
return(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
} // end dot static method
// static cross method
static cross(v1,v2) {
try {
if (!(v1 instanceof Vector) || !(v2 instanceof Vector))
throw "Vector.cross: non-vector parameter";
else
return new Vector(
v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x
);
} // end try
catch(e) {
console.log(e);
return(NaN);
}
} // end cross static method
// static add method
static add(v1,v2) {
if (!(v1 instanceof Vector) || !(v2 instanceof Vector))
throw "Vector.add: non-vector parameter";
else
return(new Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z));
} // end add static method
// static subtract method, v1-v2
static subtract(v1,v2) {
if (!(v1 instanceof Vector) || !(v2 instanceof Vector))
throw "Vector.subtract: non-vector parameter";
else {
var v = new Vector(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z);
//v.toConsole("Vector.subtract: ");
return(v);
}
} // end subtract static method
// static divide method, v1.x/v2.x etc
static divide(v1,v2) {
try {
if (!(v1 instanceof Vector) || !(v2 instanceof Vector))
throw "Vector.divide: non-vector parameter";
else {
var v = new Vector(v1.x/v2.x,v1.y/v2.y,v1.z/v2.z);
//v.toConsole("Vector.divide: ");
return(v);
}
} // end try
catch(e) {
console.log(e);
return(new Vector(NaN,NaN,NaN));
}
} // end divide static method
// static divide method, v1.x/v2.x etc
static multiply(v1,v2) {
try {
if (!(v1 instanceof Vector) || !(v2 instanceof Vector))
throw "Vector.multiply: non-vector parameter";
else {
var v = new Vector(v1.x*v2.x,v1.y*v2.y,v1.z*v2.z);
//v.toConsole("Vector.divide: ");
return(v);
}
} // end try
catch(e) {
console.log(e);
return(new Vector(NaN,NaN,NaN));
}
} // end multiply static method
// static scale method
static scale(c,v) {
try {
if (!(typeof(c) === "number") || !(v instanceof Vector))
throw "Vector.scale: malformed parameter";
else
return(new Vector(c*v.x,c*v.y,c*v.z));
} // end try
catch(e) {
console.log(e);
return(new Vector(NaN,NaN,NaN));
}
} // end scale static method
// static normalize method
static normalize(v) {
if (!(v instanceof Vector))
throw "Vector.normalize: parameter not a vector";
else {
var lenDenom = 1/Math.sqrt(Vector.dot(v,v));
return(Vector.scale(lenDenom,v));
}
} // end scale static method
} // end Vector class