My Project
gvector.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "../stdafx.h"
3 #include "gvector.h"
4 #include "gmatrix.h"
5 
7 {
8  for (int i = 0; i <= 3; i++)
9  {
10  this->vec.push_back(0);
11  }
12 }
13 
14 GVector::GVector(double X, double Y, double Z, double hc)
15 {
16  this->vec.push_back(X);
17  this->vec.push_back(Y);
18  this->vec.push_back(Z);
19  this->vec.push_back(hc);
20 }
21 
23 {
24  this->vec = other.getVec();
25 }
26 
28 {
29  this->vec = other.getVec();
30 }
31 
33 {
34  this->vec.clear();
35 }
36 
38 {
39  this->vec = other.getVec();
40  return *this;
41 }
42 
44 {
45  GVector result;
46  result[0] = (*this)[0] + other[0];
47  result[1] = (*this)[1] + other[1];
48  result[2] = (*this)[2] + other[2];
49  result[3] = (*this)[3];
50  return result;
51 }
52 
54 {
55  GVector result;
56  result[0] = (*this)[0] - other[0];
57  result[1] = (*this)[1] - other[1];
58  result[2] = (*this)[2] - other[2];
59  result[3] = (*this)[3];
60  return result;
61 }
62 
63 GVector GVector::operator/(const double value)
64 {
65  GVector result;
66  result[0] = (*this)[0] / value;
67  result[1] = (*this)[1] / value;
68  result[2] = (*this)[2] / value;
69  result[3] = (*this)[3];
70  return result;
71 }
72 
73 GVector GVector::operator*(const double value)
74 {
75  GVector result;
76  result[0] = (*this)[0] * value;
77  result[1] = (*this)[1] * value;
78  result[2] = (*this)[2] * value;
79  result[3] = (*this)[3];
80  return result;
81 }
82 
83 double& GVector::operator[](const size_t index)
84 {
85  if (index > 3 || index < 0)
86  {
87  throw VectorIndexError();
88  }
89  return this->vec[index];
90 }
91 
92 const double& GVector::operator[](const size_t index) const
93 {
94  if (index > 3 || index < 0)
95  {
96  throw VectorIndexError();
97  }
98  return this->vec[index];
99 }
100 
101 vector<double> GVector::getVec() const
102 {
103  return this->vec;
104 }
105 
107 {
108  double X = (*this)[0];
109  double Y = (*this)[1];
110  double Z = (*this)[2];
111 
112  return sqrt(X * X + Y * Y + Z * Z);
113 }
114 
116 {
117  double length = (*this).length();
118  if (length == 0)
119  {
120  return *this;
121  }
122 
123  double norm = 1 / length;
124  for (int i = 0; i < 3; ++i)
125  {
126  (*this)[i] = (*this)[i] * norm;
127  }
128 
129  return *this;
130 }
131 
133 {
134  GVector result;
135  result[0] = first[1] * second[2] - first[2] * second[1]; // new X = Y1 * Z2 - Z1 * Y2
136  result[1] = first[2] * second[0] - first[0] * second[2]; // new Y = Z1 * X2 - X1 * Z2
137  result[2] = first[0] * second[1] - first[1] * second[0]; // new Z = X1 * Y2 - Y1 * X2
138  result[3] = 1;
139 
140  return result;
141 }
142 
143 double GVector::scalar(GVector first, GVector second)
144 {
145  return first[0] * second[0] + first[1] * second[1] + first[2] * second[2];
146 }
147 
148 double GVector::angle(GVector first, GVector second)
149 {
150  double angle = acos(scalar(first, second) / (first.length() * second.length()));
151  angle *= 180 / M_PI;
152 
153  if (angle > 180)
154  {
155  angle -= 180;
156  }
157 
158  return angle;
159 }
160 
162 {
163  GVector result;
164  for (size_t i = 0; i <= 3; i++)
165  {
166  for (size_t j = 0; j <= 3; j++)
167  {
168  result[i] = result[i] + matrix[j][i] * this->vec[j];
169  }
170  }
171  return result;
172 }
173 
174 bool GVector::operator==(const GVector& other) const
175 {
176  for (int i = 0; i < 4; i++)
177  {
178  if (round(this->vec[i] * 100) / 100. != round(other[i] * 100) / 100.)
179  {
180  return false;
181  }
182  }
183  return true;
184 }
185 
186 bool GVector::operator!=(const GVector& other) const
187 {
188  for (int i = 0; i < 4; i++)
189  {
190  if (round(this->vec[i] * 100) / 100. != round(other[i] * 100) / 100.)
191  {
192  return true;
193  }
194  }
195  return false;
196 }
GVector operator-(const GVector other)
Definition: gvector.cpp:53
GVector normalize()
Definition: gvector.cpp:115
Define geometric vector.
Definition: gvector.h:26
GVector operator/(const double value)
Definition: gvector.cpp:63
Define geometric matrix.
static double angle(GVector first, GVector second)
Definition: gvector.cpp:148
GVector operator+(const GVector other)
Definition: gvector.cpp:43
static GVector cross(GVector first, GVector second)
Definition: gvector.cpp:132
GVector operator*(const double value)
Definition: gvector.cpp:73
double & operator[](const size_t index)
Definition: gvector.cpp:83
GVector()
Definition: gvector.cpp:6
vector< double > getVec() const
Definition: gvector.cpp:101
~GVector()
Definition: gvector.cpp:32
bool operator==(const GVector &other) const
Definition: gvector.cpp:174
static double scalar(GVector first, GVector second)
Definition: gvector.cpp:143
GVector & operator=(GVector other)
Definition: gvector.cpp:37
Define geometric matrix.
Definition: gmatrix.h:24
Define geometric vector.
double length()
Definition: gvector.cpp:106
bool operator!=(const GVector &other) const
Definition: gvector.cpp:186