-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCylinder.h
103 lines (76 loc) · 2.48 KB
/
Cylinder.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef _CYLINDER_H
#define _CYLINDER_H
#include "math.h"
#include "Object.h"
#include "Vect.h"
#include "Color.h"
class Cylinder : public Object{
Vect center;
double baseRadius, capRadius, height;
Color color;
public:
Cylinder ();
Cylinder (Vect, double, double, double, Color);
// method functions
Vect getCylinderCenter () { return center; }
double getCylinderBaseRadius () { return baseRadius; }
double getCylinderCapRadius () { return capRadius; }
double getCylinderHeight () {return height; }
virtual Color getColor () { return color; }
virtual Vect getNormalAt(Vect point){
// normal always points away from the center of a Cylinder
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();
// if z is less than 0 or more than height return -1
// test if ray hits outer wall
double d = (capRadius - baseRadius) * ray_direction_x;
double F = baseRadius + (capRadius - baseRadius) * ray_origin_z;
double a = pow(ray_direction_x,2) + pow(ray_direction_y,2) - pow(d,2);
double b = (ray_origin_x*ray_direction_x + ray_origin_y * ray_direction_y) - F * d;
double c = pow(ray_origin_x, 2) + pow(ray_origin_y, 2) - F*F;
double discriminant = pow(2*b,2) - 4*a*c;
if (discriminant > 0){
// the ray intersects the Cylinder
// the first root
double root_1 = ((-1 * b - sqrt(discriminant))/2) - 0.000001;
if (root_1 > 0){
// the first root is the smallest positive root
return root_1;
}
else{
// the second root is the smallest positive root
double root_2 = ((sqrt(discriminant) - b)/2) - 0.000001;
return root_2;
}
}
else{
// the ray misses the Cylinder
return -1;
}
}
};
Cylinder::Cylinder(){
center = Vect (0,0,0);
baseRadius = 1.0;
capRadius = 0.5;
height = 1.0;
color = Color (0.5,0.5,0.5,0.0);
}
Cylinder::Cylinder( Vect centerValue, double baseRadiusValue, double capRadiusValue, double heightValue, Color colorValue){
center = centerValue;
baseRadius = baseRadiusValue;
capRadius = capRadiusValue;
height = heightValue;
color = colorValue;
}
#endif