V3FIT
vector3d.cpp
1 //
2 // vector3d.cpp
3 // lgrid
4 //
5 // Created by Cianciosa, Mark R. on 2/7/16.
6 // Copyright © 2016 Cianciosa, Mark R. All rights reserved.
7 //
8 
9 #include "vector3d.hpp"
10 #include <cmath>
11 
12 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
21 vector3d::vector3d(const double x_, const double y_, const double z_) :
22 x(x_), y(y_), z(z_) {
23 }
24 
25 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 const double vector3d::length() const {
33  return sqrt(dot_product(*this, *this));
34 }
35 
36 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
44  return *this/this->length();
45 }
46 
47 //------------------------------------------------------------------------------
55 //------------------------------------------------------------------------------
56 const vector3d vector3d::cross_product(const vector3d &v1, const vector3d &v2) {
57  return vector3d(v1.y*v2.z - v1.z*v2.y,
58  v1.z*v2.x - v1.x*v2.z,
59  v1.x*v2.y - v1.y*v2.x);
60 }
61 
62 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 const double vector3d::dot_product(const vector3d &v1, const vector3d &v2) {
72  return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
73 }
74 
75 //------------------------------------------------------------------------------
83 //------------------------------------------------------------------------------
84 const vector3d operator+(const vector3d &v1, const vector3d &v2) {
85  return vector3d(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
86 }
87 
88 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
97 const vector3d operator-(const vector3d &v1, const vector3d &v2) {
98  return vector3d(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
99 }
100 
101 //------------------------------------------------------------------------------
109 //------------------------------------------------------------------------------
110 const vector3d operator/(const vector3d &v1, const double d) {
111  return vector3d(v1.x/d, v1.y/d, v1.z/d);
112 }
vector3d::cross_product
static const vector3d cross_product(const vector3d &v1, const vector3d &v2)
Takes the cross product of two vectors.
Definition: vector3d.cpp:56
vector3d::vector3d
vector3d(const double x_, const double y_, const double z_)
vector3d constructor.
Definition: vector3d.cpp:21
vector3d
A vector.
Definition: vector3d.hpp:19
vector3d::x
const double x
x component.
Definition: vector3d.hpp:22
vector3d::dot_product
static const double dot_product(const vector3d &v1, const vector3d &v2)
Takes the doc product of two vectors.
Definition: vector3d.cpp:71
vector3d::normalize
const vector3d normalize() const
Normalize vector3d instance.
Definition: vector3d.cpp:43
vector3d::length
const double length() const
Length of a vector3d instance.
Definition: vector3d.cpp:32
vector3d::y
const double y
y component.
Definition: vector3d.hpp:24
vector3d::z
const double z
z component.
Definition: vector3d.hpp:26