-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
76 lines (60 loc) · 1.89 KB
/
Sphere.cpp
File metadata and controls
76 lines (60 loc) · 1.89 KB
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
#include "Sphere.h"
Sphere::Sphere(){
center = Vector(0,0,0);
radius = 1.0;
color = Color(0.5, 0.5, 0.5, 0.0);
}
Sphere::Sphere(Vector centerValue, double radiusValue, Color colorValue){
center = centerValue;
radius = radiusValue;
color = colorValue;
}
Color Sphere::getColor(){
return color;
}
Vector Sphere::getNormalAt(Vector point){
//normal always ponts away from the center of a sphere
Vector normalVector = point.add(center.negative()).normalize();
return normalVector;
}
double Sphere::findIntersection(Ray ray){
Vector rayOrigin = ray.getRayOrigin();
double rayOriginX = rayOrigin.getVectorX();
double rayOriginY = rayOrigin.getVectorY();
double rayOriginZ = rayOrigin.getVectorZ();
Vector rayDirection = ray.getRayDirection();
double rayDirectionX = rayDirection.getVectorX();
double rayDirectionY = rayDirection.getVectorY();
double rayDirectionZ = rayDirection.getVectorZ();
Vector sphereCenter = center;
double sphereCenterX = sphereCenter.getVectorX();
double sphereCenterY = sphereCenter.getVectorY();
double sphereCenterZ = sphereCenter.getVectorZ();
double a = 1; //normalized
double b = (2 * (rayOriginX - sphereCenterX) * rayDirectionX)
+ (2 * (rayOriginY - sphereCenterY) * rayDirectionY)
+ (2 * (rayOriginZ - sphereCenterZ) * rayDirectionZ);
double c = pow(rayOriginX - sphereCenterX, 2)
+ pow(rayOriginY - sphereCenterY, 2)
+ pow(rayOriginZ - sphereCenterZ, 2)
- (radius * radius);
double discriminant = b*b - 4*a*c;
if (discriminant > 0.0){
//the ray instercepts the sphere in two sides
//first root
double root1 = ((-1*b - sqrt(discriminant)) / 2) - 0.000001;
if (root1 > 0.0){
//the first root is the smallest positive root
return root1;
}
else{
//the second root is the smallest positive root
double root2 = ((sqrt(discriminant) - b ) / 2) - 0.000001;
return root2;
}
}
else{
// the ray missed the sphere
return -1;
}
}