-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple.cpp
More file actions
65 lines (53 loc) · 2.02 KB
/
Copy pathtuple.cpp
File metadata and controls
65 lines (53 loc) · 2.02 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
#include "tuple.h"
#include <cmath>
// tuple
RayTuple::RayTuple(double x, double y, double z, double w)
: x(x), y(y), z(z), w(w) {}
void RayTuple::iswhat() const {
if (w == 1.0)
std::cout << "is a point" << std::endl;
if (w == 0.0)
std::cout << "is a vector" << std::endl;
}
void RayTuple::print_tuple() const {
std::cout << "x: " << x << " y: " << y << " z: " << z << " w: " << w
<< std::endl;
}
RayTuple RayTuple::operator-(const RayTuple &other) const {
return RayTuple(x - other.x, y - other.y, z - other.z, w - other.w);
}
RayTuple RayTuple::operator+(const RayTuple &other) const {
return RayTuple(x + other.x, y + other.y, z + other.z, w + other.w);
}
RayTuple RayTuple::operator-() const { return RayTuple(-x, -y, -z, -w); }
RayTuple RayTuple::operator*(const double scalar) const {
return RayTuple(x * scalar, y * scalar, z * scalar, w * scalar);
}
RayTuple RayTuple::operator/(const double scalar) const {
return RayTuple(x / scalar, y / scalar, z / scalar, w / scalar);
}
// point
RayPoint::RayPoint(double x, double y, double z) : RayTuple(x, y, z, 1.0) {}
RayPoint::RayPoint(const RayTuple &t) : RayTuple(t.x, t.y, t.z, 1.0) {}
// vector
RayVector::RayVector(double x, double y, double z) : RayTuple(x, y, z, 0.0) {}
RayVector::RayVector(const RayTuple &t) : RayTuple(t.x, t.y, t.z, 0.0) {}
RayVector RayVector::cross(const RayVector &other) const {
return RayVector(y * other.z - z * other.y, z * other.x - x * other.z,
x * other.y - y * other.x);
}
RayVector RayVector::normalize() const {
return RayVector(RayTuple::normalize());
}
RayVector RayVector::reflect(const RayVector &normal) const {
return (*this) - (normal * 2.0) * (this->dot(normal));
}
// utilities
double RayTuple::magnitude() const { return std::sqrt(x * x + y * y + z * z); }
RayTuple RayTuple::normalize() const {
double mv = this->magnitude();
return RayTuple(x / mv, y / mv, z / mv, w);
}
double RayTuple::dot(const RayTuple &other) const {
return x * other.x + y * other.y + z * other.z + w * other.w;
}