Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.55
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 
125 
129  double length() const;
130 
133 #if GEOM_PSEUDO3D==GEOM_TRUE
134 
135  double operator*(const Vector2& other) const;
136 #else
137 
138  double operator*(const Vector2& other) const;
139 #endif
140 
141 
144 #if GEOM_PSEUDO3D==GEOM_TRUE
145 
146  Vector2 operator*(double val) const;
147 #else
148 
149  Vector2 operator*(double val) const;
150 #endif
151 
154 #if GEOM_PSEUDO3D==GEOM_TRUE
155 
156  Vector2 operator/(double val) const;
157 #else
158 
159  Vector2 operator/(double val) const;
160 #endif
161 
162 
163 protected:
164  double valX;
165  double valY;
166 #if GEOM_PSEUDO3D==GEOM_TRUE
167  double valZ;
168 #endif
169 };
170 
171 
172 
173 
174 // Free functions
175 
176 CLASS_DECLSPEC
177 inline std::ostream &operator<<(std::ostream &stream, const Vector2& vec)
178 {
179 #if GEOM_PSEUDO3D==GEOM_TRUE
180  stream << "Vector2: "<<vec.x()<<", "<<vec.y()<<", "<<vec.z();
181 #else
182  stream << "Vector2: "<<vec.x()<<", "<<vec.y();
183 #endif
184  return stream;
185 }
186 
187 #if GEOM_PSEUDO3D==GEOM_TRUE
188 
190 CLASS_DECLSPEC
191 inline Vector2 crossProduct(const Vector2& vec0,const Vector2& vec1)
192 {
193 
194  double x=vec0.y() * vec1.z() - vec0.z() * vec1.y();
195  double y=vec0.z() * vec1.x() - vec0.x() * vec1.z();
196  double z=vec0.x() * vec1.y() - vec0.y() * vec1.x();
197  return Vector2(x,y,z);
198 }
199 #endif
200 
201 
202 
203 
206 CLASS_DECLSPEC
207 inline Vector2 normalize(const Vector2& other)
208 {
209  double len(other.length());
210 #if GEOM_PSEUDO3D==GEOM_TRUE
211  if(len>0)
212  {
213  return Vector2(other.x()/len,other.y()/len,other.z()/len);
214  }
215  else
216  {
217  std::cout<<"warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
218  return Vector2(0,0,0);
219  }
220 #else
221  if(len>0)
222  {
223 
224  return Vector2(other.x()/len,other.y()/len);
225  }
226  else
227  {
228  std::cout<<"warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
229  return Vector2(0,0);
230  }
231 #endif
232 }
233 
234 CLASS_DECLSPEC
235 inline Vector2 operator-(const Vector2& in)
236 {
237 #if GEOM_PSEUDO3D==GEOM_TRUE
238  return Vector2(-in.x(),-in.y(),-in.z());
239 #else
240  return Vector2(-in.x(),-in.y());
241 #endif
242 }
243 
244 CLASS_DECLSPEC
245 inline Vector2 operator*(double d,const Vector2& vec)
246 {
247 #if GEOM_PSEUDO3D==GEOM_TRUE
248  return Vector2(d*vec.x(),d*vec.y(),d*vec.z());
249 #else
250  return Vector2(d*vec.x(),d*vec.y());
251 #endif
252 }
253 
254 
255 } // (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