立方体由 p0,p1,p2,p3 定义
iHioGf

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
class Vector3 {
constructor(x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
sub(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
}
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
}

function boxTest(p0, p1, p2, p3, pv) {
const i = p1.sub(p0);
const j = p2.sub(p0);
const k = p3.sub(p0);
const v = pv.sub(p0);
if (v.dot(i) > 0 && v.dot(i) < i.dot(i) && v.dot(j) > 0 && v.dot(j) < j.dot(j) && v.dot(k) > 0 && v.dot(k) < k.dot(k)) {
return true;
} else {
return false;
}
}
const p0 = new Vector3(0, 0, 0);
const p1 = new Vector3(1, 0, 0);
const p2 = new Vector3(0, 2, 0);
const p3 = new Vector3(0, 0, 3);

const pv = new Vector3(0.5, 0.5, 0.5);
console.log(boxTest(p0, p1, p2, p3, pv));