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 |
|
---|
18 | namespace GtpVisibilityPreprocessor {
|
---|
19 |
|
---|
20 | // Overload << operator for C++-style output
|
---|
21 | ostream&
|
---|
22 | operator<< (ostream &s, const Vector2 &A)
|
---|
23 | {
|
---|
24 | return s << "(" << A.x() << ", " << A.y() << ")";
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Overload >> operator for C++-style input
|
---|
28 | istream&
|
---|
29 | operator>> (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.
|
---|
39 | int
|
---|
40 | Vector2::DominantAxis()
|
---|
41 | {
|
---|
42 | return (fabs(x()) > fabs(y())) ? 0 : 1;
|
---|
43 | }
|
---|
44 |
|
---|
45 | void
|
---|
46 | Vector2::ExtractVerts(float *p, int dominant) const
|
---|
47 | {
|
---|
48 | if (dominant == 0)
|
---|
49 | *p = y();
|
---|
50 | else
|
---|
51 | *p = x();
|
---|
52 | }
|
---|
53 |
|
---|
54 | float
|
---|
55 | Vector2::Normalize()
|
---|
56 | {
|
---|
57 | float s = Size();
|
---|
58 |
|
---|
59 | if (s != 0.0) {
|
---|
60 | xx /= s;
|
---|
61 | yy /= s;
|
---|
62 | }
|
---|
63 | return s;
|
---|
64 | }
|
---|
65 |
|
---|
66 | Vector2
|
---|
67 | Vector2::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 |
|
---|
75 | Vector2
|
---|
76 | Vector2::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 |
|
---|
84 | Vector2
|
---|
85 | Vector2::operator*(const float t) const
|
---|
86 | {
|
---|
87 | Vector2 u;
|
---|
88 | u.SetX(t*x());
|
---|
89 | u.SetY(t*y());
|
---|
90 | return u;
|
---|
91 | }
|
---|
92 |
|
---|
93 | int
|
---|
94 | Vector2::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 |
|
---|
102 | float
|
---|
103 | Vector2::Angle(const Vector2 &v) const
|
---|
104 | {
|
---|
105 | float cosine;
|
---|
106 | cosine = DotProd(*this, v) / (Size() * v.Size());
|
---|
107 | return acos(cosine);
|
---|
108 | }
|
---|
109 |
|
---|
110 | float
|
---|
111 | Vector2::Cosine(const Vector2 &v) const
|
---|
112 | {
|
---|
113 | return DotProd(*this, v) / (Size() * v.Size());
|
---|
114 | }
|
---|
115 |
|
---|
116 | // cosine assuming that this vector is normalized
|
---|
117 | float
|
---|
118 | Vector2::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
|
---|
124 | Vector2&
|
---|
125 | Vector2::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 |
|
---|
134 | Vector2&
|
---|
135 | Vector2::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 | }
|
---|