source: GTP/trunk/Lib/Vis/Preprocessing/src/Vector2.cpp @ 1975

Revision 1975, 2.8 KB checked in by bittner, 18 years ago (diff)

gauss sampling

Line 
1// ===================================================================
2// $Id: vector2.cpp,v 1.3 2005/11/30 15:46:50 havran Exp $
3//
4// vector2.cpp
5//     CVector2D class implements an 2D vector
6//
7// Licence: the use and distribution of this file is severely limited, please
8// see the file 'doc/Licence.txt'. Any non-authorized use can be prosecuted under
9// International Law. For further questions, please, e-mail to VHavran@seznam.cz
10// or mail to Vlastimil Havran, Pohodli 27, 57001 Litomysl, the Czech Republic.
11// REPLACEMENT_STRING
12//
13// Initial coding by Jiri Bittner
14
15// GOLEM headers
16#include "Vector2.h"
17
18namespace GtpVisibilityPreprocessor {
19
20// Overload << operator for C++-style output
21ostream&
22operator<< (ostream &s, const Vector2 &A)
23{
24  return s << "(" << A.x() << ", " << A.y() << ")";
25}
26
27// Overload >> operator for C++-style input
28istream&
29operator>> (istream &s, Vector2 &A)
30{
31  char a;
32  // read "(x, y)"
33  return s >> a >> A.x() >> a >> A.y() >> a;
34}
35
36
37// Precompute which.  which is 0 if the normal is dominant
38// in the X direction, 1 if the Y direction.
39int
40Vector2::DominantAxis()
41{
42  return (fabs(x()) > fabs(y())) ?  0 : 1;
43}
44
45void
46Vector2::ExtractVerts(float *p, int dominant) const
47{
48  if (dominant == 0)
49    *p = y();
50  else
51    *p = x();
52}
53
54float
55Vector2::Normalize()
56{
57  float s = Size();
58
59  if (s != 0.0) {
60    xx /= s;
61    yy /= s;
62  }
63  return s;
64}
65
66Vector2
67Vector2::operator-(const Vector2 &v) const
68{
69  Vector2 u;
70  u.SetX(x()-v.x());
71  u.SetY(y()-v.y());
72  return u;
73}
74
75Vector2
76Vector2::operator+(const Vector2 &v) const
77{
78  Vector2 u;
79  u.SetX(x()+v.x());
80  u.SetY(y()+v.y());
81  return u;
82}
83
84Vector2
85Vector2::operator*(const float t) const
86{
87  Vector2 u;
88  u.SetX(t*x());
89  u.SetY(t*y());
90  return u;
91}
92
93int
94Vector2::Equal(const Vector2 &u, float trash) const
95{
96 return ( (fabs(x() - u.x()) < trash) && (fabs(y() - u.y()) < trash) );
97}
98
99
100// get the angle between two vectors in range 0 - PI
101
102float
103Vector2::Angle(const Vector2 &v) const
104{
105  float cosine;
106  cosine = DotProd(*this, v) / (Size() * v.Size());
107  return acos(cosine);
108}
109
110float
111Vector2::Cosine(const Vector2 &v) const
112{
113  return DotProd(*this, v)  /  (Size() * v.Size());
114}
115
116// cosine assuming that this vector is normalized
117float
118Vector2::CosineN(const Vector2 &v) const
119{
120  return DotProd(*this, v) / v.Size();
121}
122
123// if a given vector is smaller in one of coordinates than this, update
124Vector2&
125Vector2::UpdateMin(const Vector2 &v)
126{
127  if (x() > v.x())
128    SetX(v.x());
129  if (y() > v.y())
130    SetY(v.y());
131  return *this;
132}
133
134Vector2&
135Vector2::UpdateMax(const Vector2 &v)
136{
137  if ( x() < v.x() )
138    SetX(v.x());
139  if ( y() < v.y() )
140    SetY(v.y());
141  return *this;
142}
143
144
145}
Note: See TracBrowser for help on using the repository browser.