solution for assignment 2.1
[MicroTrace.git] / Vec3f.cxx
index c063913..f656d29 100644 (file)
--- a/Vec3f.cxx
+++ b/Vec3f.cxx
@@ -4,12 +4,17 @@
 
 Vec3f::Vec3f()
 {
-
+  for(int i = 0; i < 3; ++i)
+    {
+      m_values[i] = 0.0f;
+    }
 }
 
 Vec3f::Vec3f(float x, float y, float z)
 {
-
+  m_values[0] = x;
+  m_values[1] = y;
+  m_values[2] = z;
 }
 
 Vec3f::~Vec3f()
@@ -18,77 +23,103 @@ Vec3f::~Vec3f()
 
 Vec3f::Vec3f(const Vec3f& o)
 {
-
+  for(int i = 0; i < 3; ++i)
+    {
+      m_values[i] = o.m_values[i];
+    }
 }
 
 Vec3f& 
 Vec3f::operator=(const Vec3f& o)
 {
-  
+  if(this != &o)
+    {
+      for(int i = 0; i < 3; ++i)
+       {
+         m_values[i] = o.m_values[i];
+       } 
+    }
   return *this;
 }
 
 float 
-Vec3f::operator|(const Vec3f& o)
+Vec3f::operator|(const Vec3f& o) const
 {
-  
-  return 0.0f;
+  return this->dot(o);
 }
 
 float 
-Vec3f::dot(const Vec3f& o)
+Vec3f::dot(const Vec3f& o) const
 {
-  
-  return 0.0f;
+  float dot = 0.0f;
+  for(int i = 0; i < 3; ++i)
+    {
+      dot += m_values[i]*o.m_values[i];
+    }
+  return dot;
 }
   
 Vec3f 
-Vec3f::operator%(const Vec3f& o)
+Vec3f::operator%(const Vec3f& o) const
 {
-
-  return Vec3f();
+  return this->cross(o);
 }
 
 Vec3f 
-Vec3f::cross(const Vec3f& o)
+Vec3f::cross(const Vec3f& o) const
 {
+  float x = m_values[1]*o.m_values[2] - m_values[2]*o.m_values[1];
+  float y = m_values[2]*o.m_values[0] - m_values[0]*o.m_values[2];
+  float z = m_values[0]*o.m_values[1] - m_values[1]*o.m_values[0];
   
-  return Vec3f();
+  return Vec3f(x,y,z);
 }
   
 void 
 Vec3f::normalize()
 {
-  
+  float norm = this->norm();
+  *this *= 1.0f/norm;
 }
 
 float 
 Vec3f::norm() const
 {
-  return 0.0f;
+  float n = this->dot(*this);
+  return sqrtf(n);
 }
 
 Vec3f 
 Vec3f::operator*(const float t) const
 {
-  return Vec3f();
+  Vec3f v(*this);
+  return v *= t;
 }
 
 Vec3f& 
 Vec3f::operator*=(const float t)
 {
+  for(int i = 0; i < 3; ++i)
+    {
+      m_values[i] *= t;
+    }
   return *this;
 }
 
 Vec3f 
 Vec3f::operator/(const float t) const
 {
-  return Vec3f();
+  Vec3f v(*this);
+  return v /= t;
 }
 
 Vec3f& 
 Vec3f::operator/=(const float t)
 {
+  for(int i = 0; i < 3; ++i)
+    {
+      m_values[i] /= t;
+    }
   return *this;
 }
 
@@ -116,43 +147,62 @@ Vec3f::operator+=(const Vec3f& o)
 Vec3f 
 Vec3f::operator-(const Vec3f& o) const
 {
-  return Vec3f();
+  Vec3f v(*this);
+  
+  return v -= o;
 }
 
 Vec3f& 
 Vec3f::operator-=(const Vec3f& o)
 {
+  for(unsigned int i = 0; i < 3; ++i)
+    {
+      m_values[i] -= o.m_values[i];
+    }
   return *this;
 }
   
 Vec3f 
 Vec3f::operator*(const Vec3f& o) const
 {
-  return Vec3f();
+  Vec3f v(*this);
+
+  return v *= o;
 }
 
 Vec3f& 
 Vec3f::operator*=(const Vec3f& o)
 {
+  for(unsigned int i = 0; i < 3; ++i)
+    {
+      m_values[i] *= o.m_values[i];
+    }
   return *this;
 }
 
 Vec3f 
 Vec3f::operator/(const Vec3f& o) const
 {
-  return Vec3f();
+  Vec3f v(*this);
+  
+  return v /= o;
 }
 
 Vec3f& 
 Vec3f::operator/=(const Vec3f& o)
 {
+  for(unsigned int i = 0; i < 3; ++i)
+    {
+      m_values[i] /= o.m_values[i];
+    }
   return *this;
 }
 
 float 
 Vec3f::operator[](unsigned int i) const
 {
-  return 0.0f;
+  assert( i < 3 );
+  return m_values[i];
 }
 
 float& 
This page took 0.032148 seconds and 4 git commands to generate.