-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinate.js
More file actions
61 lines (54 loc) · 1.85 KB
/
Copy pathcoordinate.js
File metadata and controls
61 lines (54 loc) · 1.85 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
// CoordinateSystem
class CoordinateSystem {
// A CoordinateSystem has 3 numbers describing how much its x, y, and z axes
// have been rotated relative to the world system's axes, and a Coordinate
// describing its origin's coordinate in the world system.
// NOTES:
// - Mathematically, the CoordinateSystem is created by rotating the axes
// first, then translating the origin. The arguments' order reflects this.
// - CoordinateSystems are right-handed.
// - The origin Coordinate must be specified relative to the world system.
constructor(xRot, yRot, zRot, origin) {
this.xRot = xRot;
this.yRot = yRot;
this.zRot = zRot;
if (origin.system != null) {
throw new Error("CoordinateSystem's origin Coordinate isn't given relative to the world system.")
}
this.origin = origin;
}
// rotate the CoordinateSystem about its own x axis by the specified angle
// (positive direction given by the right-hand rule)
rotateX(angle) {
this.yRot += angle;
this.zRot += angle;
}
// rotate the CoordinateSystem about its own y axis by the specified angle
// (positive direction given by the right-hand rule)
rotateY(angle) {
this.xRot += angle;
this.zRot += angle;
}
// rotate the CoordinateSystem about its own z axis by the specified angle
// (positive direction given by the right-hand rule)
rotateZ(angle) {
this.xRot += angle;
this.yRot += angle;
}
}
class Coordinate {
// A Coordinate has a position (x, y, z) in some CoordinateSystem.
// If the CoordinateSystem is not specified, it is assumed to be the
// world system.
constructor(x, y, z, system = null) {
this.x = x;
this.y = y;
this.z = z;
this.system = system;
}
// Returns the Coordinate's position as a Vector [x, y, z, 1].
// This format is useful for switching between coordinate systems.
getPositionVector() {
return new Vector([this.x, this.y, this.z, 1]);
}
}