-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsphere.cpp
49 lines (41 loc) · 1.09 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
//sphere.cpp
#include "sphere.h"
#include "lightsource.h"
bool Sphere::intersect(Ray& r) const
{
Vector3D centerVector = r.getOrigin() - position;
double a = 1.0;
double b = 2*dotProduct(r.getDirection(), centerVector);
double c = dotProduct(centerVector, centerVector) - radius*radius;
double discriminant = b*b - 4.0*a*c;
//now check if discriminant is positive or zero, then only we have an intersection!
if(discriminant >=0.0)
{
if(discriminant == 0)
{
double t = -b/(2.0*a);
Vector3D p = r.getOrigin() + t*r.getDirection();
r.setParameter(t, this, p - position);
return true;
}
else
{
//Calculate both roots
double D = sqrt(discriminant);
double t1 = (-b +D)/(2.0*a);
double t2 = (-b -D)/(2.0*a);
Vector3D p = r.getOrigin() + t1*r.getDirection();
bool b1 = r.setParameter(t1, this, p - position);
p = r.getOrigin() + t2*r.getDirection();
bool b2 = r.setParameter(t2, this, p - position);
return b1||b2;
}
}
return false;
}
const Vector3D &Sphere::getPosition() const {
return position;
}
double Sphere::getRadius() const {
return radius;
}