Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.63
Vector2.h
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the Fade2D library. The student license is free
4 // of charge and covers personal non-commercial research. Licensees
5 // holding a commercial license may use this file in accordance with
6 // the Commercial License Agreement.
7 //
8 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
9 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
10 // FOR A PARTICULAR PURPOSE.
11 //
12 // Please contact the author if any conditions of this licensing are
13 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
17 
18 
19 #pragma once
20 #include "common.h"
21 
22 
23 #if GEOM_PSEUDO3D==GEOM_TRUE
24  namespace GEOM_FADE25D {
25 #elif GEOM_PSEUDO3D==GEOM_FALSE
26  namespace GEOM_FADE2D {
27 #else
28  #error GEOM_PSEUDO3D is not defined
29 #endif
30 
31 class CLASS_DECLSPEC Vector2;
32 
37 class Vector2
38 {
39 public:
40 
41 #if GEOM_PSEUDO3D==GEOM_TRUE
42 
46  Vector2(const double x_,const double y_,const double z_);
47 
53  Vector2();
54 
60  Vector2(const Vector2& v_);
61 
62 #else
63 
68  Vector2(const double x_,const double y_);
74  Vector2();
80  Vector2(const Vector2& v_);
81 #endif
82 
83 
84 
85 #if GEOM_PSEUDO3D==GEOM_TRUE
86 
90 #else
91 
93 #endif
94 
95  Vector2 orthogonalVector() const;
96 
100  double x() const;
101 
105  double y() const;
106 
107 #if GEOM_PSEUDO3D==GEOM_TRUE
108 
111  double z() const;
112 #endif
113 
114 #if GEOM_PSEUDO3D==GEOM_TRUE
115 
117  void set(const double x_,const double y_,const double z_);
118 
119 #else
120 
122  void set(const double x_,const double y_);
123 #endif
124 
127  double sqLength() const;
128 
129 
133  double length() const;
134 
137 #if GEOM_PSEUDO3D==GEOM_TRUE
138 
139  double operator*(const Vector2& other) const;
140 #else
141 
142  double operator*(const Vector2& other) const;
143 #endif
144 
145 
148 #if GEOM_PSEUDO3D==GEOM_TRUE
149 
150  Vector2 operator*(double val) const;
151 #else
152 
153  Vector2 operator*(double val) const;
154 #endif
155 
158 #if GEOM_PSEUDO3D==GEOM_TRUE
159 
160  Vector2 operator/(double val) const;
161 #else
162 
163  Vector2 operator/(double val) const;
164 #endif
165 
166 
167 protected:
168  double valX;
169  double valY;
170 #if GEOM_PSEUDO3D==GEOM_TRUE
171  double valZ;
172 #endif
173 };
174 
175 
176 
177 
178 // Free functions
179 
180 CLASS_DECLSPEC
181 inline std::ostream &operator<<(std::ostream &stream, const Vector2& vec)
182 {
183 #if GEOM_PSEUDO3D==GEOM_TRUE
184  stream << "Vector2: "<<vec.x()<<", "<<vec.y()<<", "<<vec.z();
185 #else
186  stream << "Vector2: "<<vec.x()<<", "<<vec.y();
187 #endif
188  return stream;
189 }
190 
191 #if GEOM_PSEUDO3D==GEOM_TRUE
192 
194 CLASS_DECLSPEC
195 inline Vector2 crossProduct(const Vector2& vec0,const Vector2& vec1)
196 {
197 
198  double x=vec0.y() * vec1.z() - vec0.z() * vec1.y();
199  double y=vec0.z() * vec1.x() - vec0.x() * vec1.z();
200  double z=vec0.x() * vec1.y() - vec0.y() * vec1.x();
201  return Vector2(x,y,z);
202 }
203 #endif
204 
205 
206 
207 
210 CLASS_DECLSPEC
211 inline Vector2 normalize(const Vector2& other)
212 {
213  double len(other.length());
214 #if GEOM_PSEUDO3D==GEOM_TRUE
215  if(len>0)
216  {
217  return Vector2(other.x()/len,other.y()/len,other.z()/len);
218  }
219  else
220  {
221  std::cout<<"warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
222  //int* i(NULL);
223  //++*i;
224  return Vector2(0,0,0);
225  }
226 #else
227  if(len>0)
228  {
229 
230  return Vector2(other.x()/len,other.y()/len);
231  }
232  else
233  {
234  std::cout<<"warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
235  return Vector2(0,0);
236  }
237 #endif
238 }
239 
240 CLASS_DECLSPEC
241 inline Vector2 operator-(const Vector2& in)
242 {
243 #if GEOM_PSEUDO3D==GEOM_TRUE
244  return Vector2(-in.x(),-in.y(),-in.z());
245 #else
246  return Vector2(-in.x(),-in.y());
247 #endif
248 }
249 
250 CLASS_DECLSPEC
251 inline Vector2 operator*(double d,const Vector2& vec)
252 {
253 #if GEOM_PSEUDO3D==GEOM_TRUE
254  return Vector2(d*vec.x(),d*vec.y(),d*vec.z());
255 #else
256  return Vector2(d*vec.x(),d*vec.y());
257 #endif
258 }
259 
260 CLASS_DECLSPEC
261 inline Vector2 operator+(const Vector2& vec0,const Vector2& vec1)
262 {
263 #if GEOM_PSEUDO3D==GEOM_TRUE
264  return Vector2(vec0.x()+vec1.x(), vec0.y()+vec1.y() , vec0.z()+vec1.z());
265 #else
266  return Vector2(vec0.x()+vec1.x(), vec0.y()+vec1.y() );
267 #endif
268 }
269 
270 CLASS_DECLSPEC
271 inline Vector2 operator-(const Vector2& vec0,const Vector2& vec1)
272 {
273 #if GEOM_PSEUDO3D==GEOM_TRUE
274  return Vector2(vec0.x()-vec1.x(), vec0.y()-vec1.y() , vec0.z()-vec1.z());
275 #else
276  return Vector2(vec0.x()-vec1.x(), vec0.y()-vec1.y() );
277 #endif
278 }
279 
280 
281 } // (namespace)
double y() const
Get the y-value.
double x() const
Get the x-value.
Vector.
Definition: Vector2.h:37
double length() const
Get the length of the vector.
Definition: Bbox2.h:25