-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.js
76 lines (49 loc) · 2.13 KB
/
Triangle.js
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
//basic triangular building block. takes in
//vector3 objects as the vertices
function Triangle(point0In, point1In, point2In){
this.point1 = point0In;
this.point2 = point1In;
this.point3 = point2In;
this.uVector = vector3Subtract(point1, point0);
this.vVector = vector3Subtract(point2, point0);
this.normalVector = vector3CrossProduct(this.uVector, this.vVector).normalize();
/*
a = (w dot u)(v dot v) - (w dot v)(u dot v) /
(u dot u)(v dot v) - (u dot v)^2
b = (w dot v)(u dot u) - (w dot u)(u dot v) /
(u dot u)(v dot v) - (u dot v)^2
simplified to:
a = (w dot u)(v dot v) - (w dot v)(u dot v) /
denominator
b = (w dot v)(u dot u) - (w dot u)(u dot v) /
denominator
*/
this.vDotV = vector3DotProduct(this.vVector, this.vVector);
this.uDotV = vector3DotProduct(this.uVector, this.vVector);
this.uDotU = vector3DotProduct(this.uVector, this.uVector);
this.denominator = (this.uDotU * this.vDotV) - Math.pow(this.uDotV, 2);
}
//the bad part of the Triangle class is that the user has to remember to call this
//before using the vectors. But after weighing the options within javascript,
//it seems like the only way to go.
Triangle.prototype.updateVectors = function(){
this.uVector = vector3Subtract(point1, point0);
this.vVector = vector3Subtract(point2, point0);
this.normalVector = vector3CrossProduct(this.uVector, this.vVector).normalize();
this.vDotV = vector3DotProduct(this.vVector, this.vVector);
this.uDotV = vector3DotProduct(this.uVector, this.vVector);
this.uDotU = vector3DotProduct(this.uVector, this.uVector);
this.denominator = (this.uDotU * this.vDotV) - Math.pow(this.uDotV, 2);
};
Triangle.prototype.isHit = function(intersectionPoint){
var pointW = vector3Subtract(intersectionPoint, this.point0);
var wDotU = vector3DotProduct(pointW, this.uVector);
var wDotV = vector3DotProduct(pointW, this.vVector);
var a = ((wDotU * this.vDotV) - (wDotV * this.uDotV)) / this.denominator;
var b = ((wDotV * this.uDotU) - (wDotU * this.uDotV)) / this.denominator;
if(a >= 0.0005 && a <= 0.995 && b >= 0.0005 && b<=0.9995 && (a+b) <= 0.9995){
return true;
} else{
return false;
}
};