1 #ifndef _vec2_h
2 #define _vec2_h
3
4 #ifndef _real_h
5 # include "real.h"
6 #endif
7
8 #if (Vec2IsVector)
9
10 #ifndef _vector_h
11 # include "vector.h"
12 #endif
13
14 class Vec2 : public Vector {
15 public:
X()16 const Real &X() const { return data[0]; }
Y()17 const Real &Y() const { return data[1]; }
18
Vec2(double x,double y)19 Vec2( double x, double y ) : Vector( 2, x, y ) { }
Vec2(const Vector & v)20 Vec2( const Vector &v ) : Vector( v ) { Resize(2); }
Vec2()21 Vec2() { }
22
23 const Vec2& operator=(const Vector &v)
24 { Vector::operator=(v); return *this; }
25
TurnLeft()26 Vec2 TurnLeft() const { return Vec2( -Y(), X() ); }
TurnRight()27 Vec2 TurnRight() const { return Vec2( Y(), -X() ); }
28 Vec2 TurnAngleRad( const Real &angle ) const;
TurnAngleDeg(const Real & angle)29 Vec2 TurnAngleDeg( const Real &angle ) const
30 { return TurnAngleRad(angle/Real(180/M_PI)); }
31
32 void Split( const Vec2 &d, Vec2 *vx, Vec2 *vy ) const;
33 void Split( const Vec2 &d, Vec2 *vx ) const;
34
35 Real AngleRadial( const Vec2 &d ) const;
AngleDeg(const Vec2 & d)36 Real AngleDeg( const Vec2 &d ) const { return AngleRadial(d)*Real(180/M_PI); }
37
38 static int Solve( const Vec2 &p1, const Vec2 &d1,
39 const Vec2 &p2, const Vec2 &d2, Real *t1 );
40 };
41 #else
42 //
43 // -------------------------------------------------------------------------
44 // class Vec2 : Vektorklasse, die einfach die komplexen Zahlen erweitert
45 // -------------------------------------------------------------------------
46 //
47
48 class Vec2 {
49 private:
50 Real x_val;
51 Real y_val;
52
53 public:
X()54 const Real &X() const { return x_val; }
Y()55 const Real &Y() const { return y_val; }
56
IsZero()57 int IsZero() const { return X()==0.0&&Y()==0.0; }
58
Vec2(const Real & x,const Real & y)59 Vec2( const Real &x, const Real &y ) { x_val=x; y_val=y; }
Vec2(const Vec2 & v)60 Vec2( const Vec2 &v ) { x_val=v.X(); y_val=v.Y(); }
Vec2()61 Vec2() { }
62
63 Real SqrNorm() const;
64 Real Norm() const;
65 Vec2 Norm1() const;
66
TurnLeft()67 Vec2 TurnLeft() const { return Vec2( -Y(), X() ); }
TurnRight()68 Vec2 TurnRight() const { return Vec2( Y(), -X() ); }
69 Vec2 TurnAngleRad( const Real &angle ) const;
TurnAngleDeg(const Real & angle)70 Vec2 TurnAngleDeg( const Real &angle ) const
71 { return TurnAngleRad(angle/Real(180/M_PI)); }
72
73 void Split( const Vec2 &d, Vec2 *vx, Vec2 *vy ) const;
74 void Split( const Vec2 &d, Vec2 *vx ) const;
75
76 Real AngleRadial( const Vec2 &d ) const;
AngleDeg(const Vec2 & d)77 Real AngleDeg( const Vec2 &d ) const { return AngleRadial(d)*Real(180/M_PI); }
78
79 static int Solve( const Vec2 &p1, const Vec2 &d1,
80 const Vec2 &p2, const Vec2 &d2, Real *t1 );
81
ScaleX(const Real & sx)82 Vec2 ScaleX(const Real &sx) { return Vec2(x_val*sx,y_val); }
ScaleY(const Real & sy)83 Vec2 ScaleY(const Real &sy) { return Vec2(x_val,y_val*sy); }
84
85 inline const Vec2& operator=(const Vec2 &v);
86
87 // Binary Operator Functions
88
89 inline Vec2 operator+(const Vec2&) const;
90 inline Vec2 operator-(const Vec2&) const;
91
92 #ifndef __TURBOC__
93 friend inline Real operator*(const Vec2&, const Vec2&);
94 friend inline Vec2 operator*(const Real&, const Vec2&);
95 friend inline int operator==(const Vec2&, const Vec2&);
96 friend inline int operator!=(const Vec2&, const Vec2&);
97 #else
98 friend Real operator*(const Vec2&, const Vec2&);
99 friend Vec2 operator*(const Real&, const Vec2&);
100 friend int operator==(const Vec2&, const Vec2&);
101 friend int operator!=(const Vec2&, const Vec2&);
102 #endif
103
104 inline Vec2 operator*(const Real&) const;
105 inline Vec2 operator/(const Real&) const;
106
107 inline const Vec2& operator+=(const Vec2&);
108 inline const Vec2& operator-=(const Vec2&);
109 inline const Vec2& operator*=(const Real&);
110 inline const Vec2& operator/=(const Real&);
111 inline Vec2 operator+() const;
112 inline Vec2 operator-() const;
113
114 };
115
116 inline const Vec2& Vec2::operator=(const Vec2 &v) {
117 x_val = v.x_val;
118 y_val = v.y_val;
119 return *this;
120 }
121
122 inline Vec2 Vec2::operator+() const
123 {
124 return *this;
125 }
126
127 inline Vec2 Vec2::operator-() const
128 {
129 return Vec2(-x_val, -y_val);
130 }
131
132
133 // Definitions of compound-assignment operator member functions
134
135 inline const Vec2& Vec2::operator+=(const Vec2& z2)
136 {
137 x_val += z2.x_val;
138 y_val += z2.y_val;
139 return *this;
140 }
141
142 inline const Vec2& Vec2::operator-=(const Vec2& z2)
143 {
144 x_val -= z2.x_val;
145 y_val -= z2.y_val;
146 return *this;
147 }
148
149 inline const Vec2& Vec2::operator*=(const Real& val)
150 {
151 x_val *= val;
152 y_val *= val;
153 return *this;
154 }
155
156 inline const Vec2& Vec2::operator/=(const Real& val)
157 {
158 x_val /= val;
159 y_val /= val;
160 return *this;
161 }
162
163
164 // Definitions of non-member binary operator functions
165
166 inline Vec2 Vec2::operator+(const Vec2& z2) const
167 {
168 return Vec2(x_val + z2.x_val, y_val + z2.y_val);
169 }
170 inline Vec2 Vec2::operator-(const Vec2& z2) const
171 {
172 return Vec2(x_val - z2.x_val, y_val - z2.y_val);
173 }
174
175
176 inline Real operator*(const Vec2& z1, const Vec2& z2)
177 {
178 return z1.x_val*z2.x_val + z1.y_val*z2.y_val;
179 }
180 inline Vec2 Vec2::operator*(const Real& val2) const
181 {
182 return Vec2(x_val*val2, y_val*val2);
183 }
184 inline Vec2 operator*(const Real& val, const Vec2& z2)
185 {
186 return Vec2(z2.x_val*val, z2.y_val*val);
187 }
188
189 inline Vec2 Vec2::operator/(const Real& val) const
190 {
191 return Vec2(x_val/val, y_val/val);
192 }
193
194 inline int operator==(const Vec2& z1, const Vec2& z2)
195 {
196 return z1.x_val == z2.x_val && z1.y_val == z2.y_val;
197 }
198
199 inline int operator!=(const Vec2& z1, const Vec2& z2)
200 {
201 return z1.x_val != z2.x_val || z1.y_val != z2.y_val;
202 }
203
SqrNorm()204 inline Real Vec2::SqrNorm() const { return X()*X()+Y()*Y(); }
Norm()205 inline Real Vec2::Norm() const { return sqrt(SqrNorm()); }
Norm1()206 inline Vec2 Vec2::Norm1() const { return *this / Norm(); }
207
208 #endif
209
210 extern Vec2 Vec2Zero;
211
212 #endif
213