aboutsummaryrefslogtreecommitdiff
path: root/src/vector.cpp
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-08-06 19:21:49 +0200
committerJulian T <julian@jtle.dk>2020-08-06 19:22:37 +0200
commit4348cc9581bfea05359485c5d2d074132d0271da (patch)
tree0c6d92a90ac4cf9acd326f632dcdc962ddca013a /src/vector.cpp
parent893176a0b18a2281abe09def716ccc3db5583c3f (diff)
Renders scenes with a single hardcoded light and green objects
Diffstat (limited to 'src/vector.cpp')
-rw-r--r--src/vector.cpp27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/vector.cpp b/src/vector.cpp
index 070f956..1e4f5a1 100644
--- a/src/vector.cpp
+++ b/src/vector.cpp
@@ -1,6 +1,7 @@
#include "vector.hpp"
#include <math.h>
+#include <stdexcept>
Vec3d::Vec3d() {
set(0, 0, 0);
@@ -19,7 +20,7 @@ void Vec3d::set(double x, double y, double z) {
void Vec3d::normalize() {
auto len = length();
if (len == 0) {
- throw "Normalizing zero vector";
+ throw std::runtime_error("Normalizing zero vector");
}
m_x /= len;
@@ -35,6 +36,22 @@ double Vec3d::dot(const Vec3d &vec) const {
return m_x * vec.m_x + m_y * vec.m_y + m_z * vec.m_z;
}
+Vec3d Vec3d::cross(const Vec3d &vec) const {
+ return Vec3d(
+ m_y * vec.m_z - m_z * vec.m_y,
+ m_z * vec.m_x - m_x * vec.m_z,
+ m_x * vec.m_y - m_y * vec.m_x
+ );
+}
+
+Vec3d Vec3d::operator+(const Vec3d &vec) const {
+ return Vec3d(
+ m_x + vec.m_x,
+ m_y + vec.m_y,
+ m_z + vec.m_z
+ );
+}
+
Vec3d Vec3d::operator-(const Vec3d &vec) const {
return Vec3d(
m_x - vec.m_x,
@@ -51,3 +68,11 @@ Vec3d Vec3d::operator-() const {
);
}
+
+Vec3d Vec3d::operator*(double op) const {
+ return Vec3d(
+ m_x * op,
+ m_y * op,
+ m_z * op
+ );
+}