-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3D.cpp
130 lines (87 loc) · 3.16 KB
/
Vector3D.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// This file contains the definition of the class Vector3D
#include <math.h>
#include "Vector3D.h"
#include "Normal.h"
#include "Point3D.h"
// ---------------------------------------------------------- default constructor
Vector3D::Vector3D(void)
: x(0.0), y(0.0), z(0.0)
{}
// ---------------------------------------------------------- constructor
Vector3D::Vector3D(double a)
: x(a), y(a), z(a)
{}
// ---------------------------------------------------------- constructor
Vector3D::Vector3D(double _x, double _y, double _z)
: x(_x), y(_y), z(_z)
{}
// ---------------------------------------------------------- copy constructor
Vector3D::Vector3D(const Vector3D& vector)
: x(vector.x), y(vector.y), z(vector.z)
{}
// ---------------------------------------------------------- constructor
// constructs a vector from a normal
Vector3D::Vector3D(const Normal& n)
: x(n.x), y(n.y), z(n.z)
{}
// ---------------------------------------------------------- constructor
// constructs a vector from a point
// this is used in the ConcaveHemisphere hit functions
Vector3D::Vector3D(const Point3D& p)
: x(p.x), y(p.y), z(p.z)
{}
// ---------------------------------------------------------- destructor
Vector3D::~Vector3D (void)
{}
// ---------------------------------------------------------- assignment operator
Vector3D&
Vector3D::operator= (const Vector3D& rhs) {
if (this == &rhs)
return (*this);
x = rhs.x; y = rhs.y; z = rhs.z;
return (*this);
}
// ----------------------------------------------------------- assignment operator
// assign a Normal to a vector
Vector3D&
Vector3D::operator= (const Normal& rhs) {
x = rhs.x; y = rhs.y; z = rhs.z;
return (*this);
}
// ---------------------------------------------------------- assignment operator
// assign a point to a vector
Vector3D&
Vector3D::operator= (const Point3D& rhs) {
x = rhs.x; y = rhs.y; z = rhs.z;
return (*this);
}
// ---------------------------------------------------------- length
// length of the vector
double
Vector3D::length(void) {
return (sqrt(x * x + y * y + z * z));
}
// ---------------------------------------------------------- normalize
// converts the vector to a unit vector
void
Vector3D::normalize(void) {
double length = sqrt(x * x + y * y + z * z);
x /= length; y /= length; z /= length;
}
// ---------------------------------------------------------- hat
// converts the vector to a unit vector and returns the vector
Vector3D&
Vector3D::hat(void) {
double length = sqrt(x * x + y * y + z * z);
x /= length; y /= length; z /= length;
return (*this);
}
// non-member function
// ---------------------------------------------------------- operator*
// multiplication by a matrix on the left
Vector3D
operator* (const Matrix& mat, const Vector3D& v) {
return (Point3D(mat.m[0][0] * v.x + mat.m[0][1] * v.y + mat.m[0][2] * v.z,
mat.m[1][0] * v.x + mat.m[1][1] * v.y + mat.m[1][2] * v.z,
mat.m[2][0] * v.x + mat.m[2][1] * v.y + mat.m[2][2] * v.z));
}