1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | function Point3d(x, y, z) {
|
8 | this.x = x !== undefined ? x : 0;
|
9 | this.y = y !== undefined ? y : 0;
|
10 | this.z = z !== undefined ? z : 0;
|
11 | }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Point3d.subtract = function(a, b) {
|
20 | var sub = new Point3d();
|
21 | sub.x = a.x - b.x;
|
22 | sub.y = a.y - b.y;
|
23 | sub.z = a.z - b.z;
|
24 | return sub;
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Point3d.add = function(a, b) {
|
34 | var sum = new Point3d();
|
35 | sum.x = a.x + b.x;
|
36 | sum.y = a.y + b.y;
|
37 | sum.z = a.z + b.z;
|
38 | return sum;
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | Point3d.avg = function(a, b) {
|
48 | return new Point3d(
|
49 | (a.x + b.x) / 2,
|
50 | (a.y + b.y) / 2,
|
51 | (a.z + b.z) / 2
|
52 | );
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | Point3d.crossProduct = function(a, b) {
|
63 | var crossproduct = new Point3d();
|
64 |
|
65 | crossproduct.x = a.y * b.z - a.z * b.y;
|
66 | crossproduct.y = a.z * b.x - a.x * b.z;
|
67 | crossproduct.z = a.x * b.y - a.y * b.x;
|
68 |
|
69 | return crossproduct;
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | Point3d.prototype.length = function() {
|
78 | return Math.sqrt(
|
79 | this.x * this.x +
|
80 | this.y * this.y +
|
81 | this.z * this.z
|
82 | );
|
83 | };
|
84 |
|
85 | module.exports = Point3d;
|