diff options
author | Julian T <julian@jtle.dk> | 2020-07-26 12:56:27 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-07-26 12:56:27 +0200 |
commit | 893176a0b18a2281abe09def716ccc3db5583c3f (patch) | |
tree | a34da79b7dc0fcdbdd39e2a3f4000cc6a1c0a896 /src/vector.cpp | |
parent | 18960c4b88ce912e08b12182b835a7de75388b78 (diff) |
Implemented object intersection and startet work on render gui
Diffstat (limited to 'src/vector.cpp')
-rw-r--r-- | src/vector.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/vector.cpp b/src/vector.cpp new file mode 100644 index 0000000..070f956 --- /dev/null +++ b/src/vector.cpp @@ -0,0 +1,53 @@ +#include "vector.hpp" + +#include <math.h> + +Vec3d::Vec3d() { + set(0, 0, 0); +} + +Vec3d::Vec3d(double x, double y, double z) { + set(x, y, z); +} + +void Vec3d::set(double x, double y, double z) { + m_x = x; + m_y = y; + m_z = z; +} + +void Vec3d::normalize() { + auto len = length(); + if (len == 0) { + throw "Normalizing zero vector"; + } + + m_x /= len; + m_y /= len; + m_z /= len; +} + +double Vec3d::length() const { + return sqrt(m_x * m_x + m_y * m_y + m_z * m_z); +} + +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::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 { + return Vec3d( + -m_x, + -m_y, + -m_z + ); +} + |