-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.js
More file actions
45 lines (35 loc) · 888 Bytes
/
Copy pathshape.js
File metadata and controls
45 lines (35 loc) · 888 Bytes
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
class Sphere {
constructor(center, radius, material) {
this.center = center;
this.radius = radius;
this.material = material;
}
getIntersection(ray) {
const cp = ray.origin.minus(this.center);
const a = ray.direction.dot(ray.direction);
const b = 2 * cp.dot(ray.direction);
const c = cp.dot(cp) - this.radius * this.radius;
const discriminant = b * b - 4 * a * c;
if (discriminant < 0) {
// no intersection
return null;
}
const sqrt = Math.sqrt(discriminant);
const ts = [];
const sub = (-b - sqrt) / (2 * a);
if (sub >= 0) {
ts.push(sub);
}
const add = (-b + sqrt) / (2 * a);
if (add >= 0) {
ts.push(add);
}
if (ts.length == 0) {
return null;
}
return Math.min.apply(null, ts);
}
normalAt(point) {
return point.minus(this.center).normalized();
}
}