PageSourceSearch

https://www.nan.ma/lightsout/lib/point3d.js

js nan.ma collected 2026-10-02 05:58:16 UTC 3,835 bytes, 154 lines download raw bytes

1var EPSILON = 0.00000000001;
2
3area = function(p1, p2, p3) {
4  return (p1.x * p2.y - p1.y * p2.x) + (p2.x * p3.y - p2.y * p3.x) + (p3.x * p1.y - p3.y * p1.x);
5}
6
7var rotationMatrix = [[1,0,0],[0,1,0],[0,0,1]];
8
9resetRotationMatrix = function() {
10  for(var i = 0; i<3; i++){
11    for(var j = 0; j<3; j++){
12      if (i === j) {
13        rotationMatrix[i][j] = 1;
14      } else {
15        rotationMatrix[i][j] = 0;
16      }
17    }  
18  }
19}
20
21setRotationMatrix = function(axis, t) {
22  axis.normalize();
23
24  var ux = axis.x;
25  var uy = axis.y;
26  var uz = axis.z;
27
28  var ct = Math.cos(t);
29  var st = Math.sin(t);
30
31  rotationMatrix[0][0] = (ct + ux * ux * (1-ct));
32  rotationMatrix[0][1] = (ux * uy *(1-ct) -uz * st);
33  rotationMatrix[0][2] = (ux*uz *(1-ct) + uy*st);
34  rotationMatrix[1][0] = (uy * ux *(1-ct) + uz* st);
35  rotationMatrix[1][1] = (ct + uy * uy *(1-ct));
36  rotationMatrix[1][2] = (uy*uz*(1-ct)-ux*st);
37  rotationMatrix[2][0] = (uz*ux*(1-ct) - uy*st);
38  rotationMatrix[2][1] = (uz*uy*(1-ct) +ux*st);
39  rotationMatrix[2][2] = (ct + uz*uz*(1-ct));
40}
41
42dedupOnInnerProd = function(inputArray) {
43  var outputArray = [];
44  inputArray.forEach(function(input) {
45    if (input.indexIn(outputArray) < 0) {
46      outputArray.push(input);
47    }
48  });
49  return outputArray;
50}
51
52function Point3D(x,y,z) {
53  this.x = x;
54  this.y = y;
55  this.z = z;
56
57
58  this.normalize = function() {
59    var norm_sq = this.x*this.x + this.y*this.y + this.z*this.z;
60    if (norm_sq > 0) {
61      norm = Math.sqrt(norm_sq);
62      this.x = this.x / norm;
63      this.y = this.y / norm;
64      this.z = this.z / norm;
65    }
66  }
67
68  this.norm = function() {
69    var norm_sq = this.x*this.x + this.y*this.y + this.z*this.z;
70    return Math.sqrt(norm_sq);
71  }
72
73  this.rotate = function() {
74    
75    var coord = [this.x, this.y, this.z].slice(0);
76    var output = [0,0,0];
77    for (var i=0; i<3; i++) {
78      for (var j=0; j<3; j++) {
79        output[i] += rotationMatrix[i][j] * coord[j];
80      }
81    }
82    this.x = output[0];
83    this.y = output[1];
84    this.z = output[2];
85  }
86
87  this.project = function() {
88    // project this 3D point to 2D, viewed from the +x direction, considering the origin of 2D is the center of canvas
89    
90    var origin_projection = this.project_origin();
91    return new Point2D(origin_projection.x + viewWidth/2, origin_projection.y + viewHeight/2)
92  }
93
94  this.project_origin = function() {
95    // project this 3D point to 2D, viewed from the +z direction, 3D origin -> 2D origin
96    var factor, x, y, viewDistance;
97    
98    factor = viewHeight * 0.45 / (1 - this.z / 15);
99    
100    x = this.x * factor;
101    y = -this.y * factor;
102    return new Point2D(x, y);
103  }
104
105  this.innerProd = function(p2) {
106    var output = 0;
107    output += this.x * p2.x;
108    output += this.y * p2.y;
109    output += this.z * p2.z;
110    return output;
111  }
112
113  this.outerProd = function(p2) {
114    var x, y, z;
115    x = this.y * p2.z - this.z * p2.y;
116    y = this.z * p2.x - this.x * p2.z;
117    z = this.x * p2.y - this.y * p2.x;
118    return new Point3D(x, y, z);
119  }
120
121  this.scale = function(s) {
122    return new Point3D(s * this.x, s * this.y, s * this.z);
123  }
124
125  this.clone = function() {
126    return this.scale(1);
127  }
128
129  this.reverse = function() {
130    return this.scale(-1);
131  }
132
133  this.indexIn = function(arr) {
134    for (var i = 0; i < arr.length; i++) {
135      if (this.innerProd(arr[i]) > this.norm() * arr[i].norm() - EPSILON) {
136        return i;
137      }
138    }
139    return -1;
140  }
141}
142
143function Point2D(x,y) {
144  this.x = x;
145  this.y = y;
146  this.norm = Math.sqrt(this.x * this.x + this.y * this.y);
147
148  this.distance = function(p2) {
149    var dx = this.x - p2.x;
150    var dy = this.y - p2.y;
151
152    return Math.sqrt(dx*dx + dy*dy);
153  }
154}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.