OverSim
Vector2D.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <Vector2D.h>
25 #include <algorithm>
26 
28 {
29  x = 0.0;
30  y = 0.0;
31 }
32 
33 Vector2D::Vector2D(double x, double y)
34 {
35  this->x = x;
36  this->y = y;
37 }
38 
40 {
41  double temp;
42  temp = sqrt(x * x + y * y);
43  if(temp != 0.0) {
44  x /= temp;
45  y /= temp;
46  }
47 }
48 
49 double Vector2D::distanceSqr(const Vector2D v) const
50 {
51  double dx, dy;
52  dx = x - v.x;
53  dy = y - v.y;
54  return dx * dx + dy * dy;
55 }
56 
57 double Vector2D::xyMaxDistance(const Vector2D v) const
58 {
59  return std::max(fabs(x - v.x), fabs(y - v.y));
60 }
61 
62 double Vector2D::cosAngle(const Vector2D& v) const
63 {
64  return (x * v.x + y * v.y) / (sqrt(x * x + y * y) * sqrt(v.x * v.x + v.y * v.y));
65 }
66 
78 int Vector2D::getQuadrant(const Vector2D& v) const
79 {
80  int quad = 0;
81  // v.y <= this.y -> quadrant 1 or 2
82  if( v.y <= y ) quad = 1;
83  // v.x <= this.x -> quadrant 2 or 3
84  if( v.x <= x ) quad ^= 3;
85  return quad;
86 }
87 
89 {
90  x = v.x;
91  y = v.y;
92  return *this;
93 }
94 
96 {
97  x += v.x;
98  y += v.y;
99  return *this;
100 }
101 
103 {
104  x -= v.x;
105  y -= v.y;
106  return *this;
107 }
108 
110 {
111  x *= s;
112  y *= s;
113  return *this;
114 }
115 
117 {
118  x /= s;
119  y /= s;
120  return *this;
121 }
122 
124 {
125  Vector2D temp;
126  temp.x = x + v.x;
127  temp.y = y + v.y;
128  return temp;
129 }
130 
132 {
133  Vector2D temp;
134  temp.x = x - v.x;
135  temp.y = y - v.y;
136  return temp;
137 }
138 
139 Vector2D Vector2D::operator*(const double s) const
140 {
141  Vector2D temp;
142  temp.x = x * s;
143  temp.y = y * s;
144  return temp;
145 }
146 
147 Vector2D Vector2D::operator/(const double s) const
148 {
149  Vector2D temp;
150  temp.x = x / s;
151  temp.y = y / s;
152  return temp;
153 }
154 
155 bool Vector2D::operator==(const Vector2D& v) const
156 {
157  if(x == v.x && y == v.y)
158  return true;
159  else
160  return false;
161 }
162 
163 bool Vector2D::operator!=(const Vector2D& v) const
164 {
165  if(x != v.x || y != v.y)
166  return true;
167  else
168  return false;
169 }
170 
171 bool operator<(const Vector2D& a, const Vector2D& b)
172 {
173  if(a.y == b.y)
174  return a.x < b.x;
175  else
176  return a.y < b.y;
177 }
178 
179 std::ostream& operator<<(std::ostream& Stream, const Vector2D& v)
180 {
181  return Stream << std::fixed << "[" << v.x << ", " << v.y << "]";
182 }
183 
184 void Vector2D::netPack(cCommBuffer *b)
185 {
186  //cMessage::netPack(b);
187  doPacking(b, this->x);
188  doPacking(b, this->y);
189 }
190 
191 void Vector2D::netUnpack(cCommBuffer *b)
192 {
193  //cMessage::netUnpack(b);
194  doUnpacking(b, this->x);
195  doUnpacking(b, this->y);
196 }