-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ray.cpp
81 lines (59 loc) · 1.94 KB
/
Ray.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
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
#include "Ray.h"
//not dependent
#include <iostream>
Ray::Ray()
{
}
Ray::Ray(const Vector3f& origin, const Vector3f& direction)
: origin(origin), direction(direction)
{
}
/* Takes a parameter t and returns the point accoring to t. t is the parametric variable in the ray equation o+t*d.*/
Vector3f Ray::getPoint(float t) const
{
/***********************************************
* *
* TODO: Implement this function *
* *
***********************************************
*/
Vector3f pointAtT;
pointAtT = vectorAddition(origin,scalarMultiplication(t, direction));
return pointAtT;
}
/* Takes a point p and returns the parameter t according to p such that p = o+t*d. */
float Ray::gett(const Vector3f & p) const
{
/***********************************************
* *
* TODO: Implement this function *
* *
***********************************************
*/
Vector3f vectorFromOriginToPoint = vectorSubtraction(p, origin);
return dotProduct(vectorFromOriginToPoint, direction) / dotProduct(direction, direction);
}
Vector3f Ray::scalarMultiplication(float t, Vector3f direction) const{
Vector3f result;
result.x = direction.x * t;
result.y = direction.y * t;
result.z = direction.z * t;
return result;
}
Vector3f Ray::vectorSubtraction(Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
return result;
}
Vector3f Ray::vectorAddition(Vector3f a, Vector3f b) const{
Vector3f result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
float Ray::dotProduct(Vector3f a, Vector3f b) const{
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}