-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.h
81 lines (65 loc) · 1.95 KB
/
Sphere.h
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
76
77
78
79
80
81
#ifndef SPHERE_H
#define SPHERE_H
#include "math.h"
#include "Object.h"
#include "Vect.h"
#include "Color.h"
class Sphere : public Object {
Vect center;
double radius;
Color color;
public:
Sphere();
Sphere(Vect, double, Color);
Vect getSphereCenter() { return center; }
double getSphereRadius() { return radius; }
virtual Color getColor() { return color; }
virtual Vect getNormalAt(Vect point){
Vect normal_Vect = point.vectAdd(center.negative()).normalize();
return normal_Vect;
}
virtual double findIntersection(Ray ray){
Vect ray_origin = ray.getRayOrigin();
double ray_origin_x = ray_origin.getVectX();
double ray_origin_y = ray_origin.getVectY();
double ray_origin_z = ray_origin.getVectZ();
Vect ray_direction = ray.getRayDirection();
double ray_direction_x = ray_direction.getVectX();
double ray_direction_y = ray_direction.getVectY();
double ray_direction_z = ray_direction.getVectZ();
Vect sphere_center = center;
double sphere_center_x = sphere_center.getVectX();
double sphere_center_y = sphere_center.getVectY();
double sphere_center_z = sphere_center.getVectZ();
double a = 1;
double b = (2*(ray_origin_x - sphere_center_x)*ray_direction_x) + (2*(ray_origin_y - sphere_center_y)*ray_direction_y) + (2*(ray_origin_z - sphere_center_z)*ray_direction_z);
double c = pow(ray_origin_x - sphere_center_x, 2) + pow(ray_origin_y - sphere_center_y, 2) + pow(ray_origin_z - sphere_center_z, 2) - (radius*radius);
double discriminant = b*b - 4*c;
if (discriminant > 0){
//gotcha
double root_1 = ((-1*b - sqrt(discriminant))/2) - 0.000001;
if (root_1 > 0){
return root_1;
}
else {
double root_2 = ((sqrt(discriminant) - b)/2) - 0.000001;
return root_2;
}
}
else{
//fail
return -1;
}
}
};
Sphere::Sphere(){
center = Vect(0, 0, 0);
radius = 1.0;
color = Color(0.5, 0.5, 0.5, 0);
}
Sphere::Sphere(Vect p, double r, Color c){
center = p;
radius = r;
color = c;
}
#endif