-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sphere.cpp
53 lines (43 loc) · 1.19 KB
/
Sphere.cpp
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
#include "Sphere.h"
using namespace rt;
Intersection Sphere::getIntersection(const Line& line, float minDist, float maxDist) {
Intersection in;
Vector dx = line.dx();
Vector x0 = line.x0();
float A = dx * dx;
float B = 2 * (dx * (x0 - _center));
float C = (x0 - _center) * (x0 - _center);
float delta = B*B - 4 * A * (C - _radius * _radius);
if( delta < 0) {
return Intersection{};
}
else
{
float x1 = (-B - sqrt(delta)) / (2 * A);
float x2 = (-B + sqrt(delta)) / (2 * A);
if(x1 == x2) {
if(minDist < x1 && x1 < maxDist)
return Intersection{true, this, &line, x1};
}
if(x1 < x2)
{
if(minDist < x1 && x1 < maxDist)
{
return Intersection{true, this, &line, x1};
}
return Intersection{};
}
else{
if(minDist < x2 && x2 < maxDist)
{
return Intersection{true, this, &line, x2};
}
return Intersection{};
}
}
}
const Vector Sphere::normal(const Vector& vec) const {
Vector n = vec - _center;
n.normalize();
return n;
}