summaryrefslogtreecommitdiff
path: root/hw6/src/vec3d.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'hw6/src/vec3d.cpp')
-rw-r--r--hw6/src/vec3d.cpp287
1 files changed, 287 insertions, 0 deletions
diff --git a/hw6/src/vec3d.cpp b/hw6/src/vec3d.cpp
new file mode 100644
index 0000000..3b20ad5
--- /dev/null
+++ b/hw6/src/vec3d.cpp
@@ -0,0 +1,287 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+
+#include <cassert>
+#include <algorithm>
+#include "vec3d.h"
+
+//////////////////
+// Constructors //
+//////////////////
+vec3d::vec3d(vec3d::const_reference value)
+{
+ x = y = z = value;
+}
+
+
+vec3d::vec3d(vec3d::const_reference x, vec3d::const_reference y, vec3d::const_reference z)
+{
+ this->x = x;
+ this->y = y;
+ this->z = z;
+}
+
+
+vec3d::vec3d(const vec3d& v)
+{
+ x = v.x;
+ y = v.y;
+ z = v.z;
+}
+
+
+////////////////
+// Inspectors //
+////////////////
+vec3d::const_reference vec3d::operator[](size_t index) const
+{
+ assert(index < size());
+ return data[index];
+}
+
+
+vec3d::reference vec3d::operator[](size_t index)
+{
+ assert(index < size());
+ return data[index];
+}
+
+
+vec3d::iterator vec3d::begin(void)
+{
+ return data;
+}
+
+
+vec3d::const_iterator vec3d::begin(void) const
+{
+ return data;
+}
+
+
+vec3d::iterator vec3d::end(void)
+{
+ return begin() + size();
+}
+
+
+vec3d::const_iterator vec3d::end(void) const
+{
+ return begin() + size();
+}
+
+
+///////////////
+// Operators //
+///////////////
+vec3d& vec3d::operator=(const vec3d& v)
+{
+ _assign(v);
+ return *this;
+}
+
+
+bool vec3d::operator==(const vec3d& v) const
+{
+ return (x == v.x) && (y == v.y) && (z == v.z);
+}
+
+
+bool vec3d::operator!=(const vec3d& v) const
+{
+ return (x != v.x) || (y != v.y) || (z != v.z);
+}
+
+
+vec3d vec3d::operator-(void) const
+{
+ return vec3d(-x, -y, -z);
+}
+
+
+vec3d vec3d::operator+(const vec3d& v) const
+{
+ return vec3d(x + v.x, y + v.y, z + v.z);
+}
+
+
+vec3d vec3d::operator-(const vec3d& v) const
+{
+ return vec3d(x - v.x, y - v.y, z - v.z);
+}
+
+
+vec3d vec3d::operator*(const vec3d& v) const
+{
+ return vec3d(x * v.x, y * v.y, z * v.z);
+}
+
+
+vec3d vec3d::operator*(vec3d::const_reference scale) const
+{
+ return vec3d(x * scale, y * scale, z * scale);
+}
+
+
+vec3d vec3d::operator/(const vec3d& v) const
+{
+ return vec3d(x / v.x, y / v.y, z / v.z);
+}
+
+
+vec3d vec3d::operator/(vec3d::const_reference scale) const
+{
+ return vec3d(x / scale, y / scale, z / scale);
+}
+
+
+vec3d& vec3d::operator+=(const vec3d& v)
+{
+ x += v.x;
+ y += v.y;
+ z += v.z;
+ return *this;
+}
+
+
+vec3d& vec3d::operator-=(const vec3d& v)
+{
+ x -= v.x;
+ y -= v.y;
+ z -= v.z;
+ return *this;
+}
+
+
+vec3d& vec3d::operator*=(const vec3d& v)
+{
+ x *= v.x;
+ y *= v.y;
+ z *= v.z;
+ return *this;
+}
+
+
+vec3d& vec3d::operator*=(vec3d::const_reference scale)
+{
+ x *= scale;
+ y *= scale;
+ z *= scale;
+ return *this;
+}
+
+
+vec3d& vec3d::operator/=(const vec3d& v)
+{
+ x /= v.x;
+ y /= v.y;
+ z /= v.z;
+ return *this;
+}
+
+
+vec3d& vec3d::operator/=(vec3d::const_reference scale)
+{
+ x /= scale;
+ y /= scale;
+ z /= scale;
+ return *this;
+}
+
+
+
+///////////////
+// Modifiers //
+///////////////
+vec3d::value_type vec3d::dot(const vec3d& v) const
+{
+ return (x*v.x + y*v.y + z*v.z);
+}
+
+
+vec3d::value_type vec3d::squared_length(void) const
+{
+ return dot(*this);
+}
+
+
+vec3d::value_type vec3d::length(void) const
+{
+ return sqrt(squared_length());
+}
+
+
+vec3d::value_type vec3d::squared_distance(const vec3d& v) const
+{
+ return (*this - v).squared_length();
+}
+
+
+vec3d::value_type vec3d::distance(const vec3d& v) const
+{
+ return sqrt(squared_distance(v));
+}
+
+
+vec3d vec3d::cross(const vec3d& v) const
+{
+ return vec3d(y*v.z - z*v.y,
+ z*v.x - x*v.z,
+ x*v.y - y*v.x);
+}
+
+
+///////////////
+// Modifiers //
+///////////////
+vec3d& vec3d::abs(void)
+{
+ std::for_each(begin(), end(), [](reference val)
+ {
+ if(val < 0) val = -val;
+ });
+ return *this;
+}
+
+
+vec3d& vec3d::clamp(vec3d::value_type lower, vec3d::value_type upper)
+{
+ std::for_each(begin(), end(), [&](reference val)
+ {
+ if(val < lower) val = lower;
+ else if(val > upper) val = upper;
+ });
+ return *this;
+}
+
+
+vec3d& vec3d::normalize(void)
+{
+ *this /= length();
+ return *this;
+}
+
+
+/////////////////////
+// Private Methods //
+/////////////////////
+void vec3d::_assign(const vec3d& v)
+{
+ x = v.x;
+ y = v.y;
+ z = v.z;
+}
+
+
+void vec3d::_swap(vec3d& v)
+{
+ std::swap(x, v.x);
+ std::swap(y, v.y);
+ std::swap(z, v.z);
+}