File: vec2.h

package info (click to toggle)
xjig 2.4-14.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 892 kB
  • sloc: cpp: 10,177; makefile: 1,142; perl: 23
file content (212 lines) | stat: -rw-r--r-- 5,331 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef _vec2_h
#define _vec2_h

#ifndef _real_h
#	include "real.h"
#endif

#if (Vec2IsVector)

#ifndef _vector_h
#	include "vector.h"
#endif

class Vec2 : public Vector {
	public:
		const Real &X() const 							{ return data[0]; }
		const Real &Y() const 							{ return data[1]; }

		Vec2( double x, double y ) : Vector( 2, x, y )		{ }
		Vec2( const Vector &v ) : Vector( v )					{ Resize(2); }
		Vec2()															{ }

		const Vec2& operator=(const Vector &v)
		{ Vector::operator=(v); return *this; }

		Vec2 TurnLeft() const					{ return Vec2( -Y(), X() ); }
		Vec2 TurnRight() const					{ return Vec2( Y(), -X() ); }
		Vec2 TurnAngleRad( const Real &angle ) const;
		Vec2 TurnAngleDeg( const Real &angle ) const
			{ return TurnAngleRad(angle/Real(180/M_PI)); }

		void Split( const Vec2 &d, Vec2 *vx, Vec2 *vy ) const;
		void Split( const Vec2 &d, Vec2 *vx ) const;

		Real AngleRadial( const Vec2 &d ) const;
		Real AngleDeg( const Vec2 &d ) const		{ return AngleRadial(d)*Real(180/M_PI); }

		static int Solve(	const Vec2 &p1, const Vec2 &d1,
						const Vec2 &p2, const Vec2 &d2, Real *t1 );
};
#else
//
// -------------------------------------------------------------------------
// class Vec2 : Vektorklasse, die einfach die komplexen Zahlen erweitert
// -------------------------------------------------------------------------
//

class Vec2 {
	private:
		Real	x_val;
		Real	y_val;

	public:
		const Real &X() const 							{ return x_val; }
		const Real &Y() const 							{ return y_val; }

		int	IsZero() const								{ return X()==0.0&&Y()==0.0; }

		Vec2( const Real &x, const Real &y )		{ x_val=x; y_val=y; }
		Vec2( const Vec2 &v )						{ x_val=v.X(); y_val=v.Y(); }
		Vec2()												{ }

		Real SqrNorm() const;
		Real Norm() const;
		Vec2 Norm1() const;

		Vec2 TurnLeft() const				{ return Vec2( -Y(), X() ); }
		Vec2 TurnRight() const			{ return Vec2( Y(), -X() );	}
		Vec2 TurnAngleRad( const Real &angle ) const;
		Vec2 TurnAngleDeg( const Real &angle ) const
			{ return TurnAngleRad(angle/Real(180/M_PI)); }

		void Split( const Vec2 &d, Vec2 *vx, Vec2 *vy ) const;
		void Split( const Vec2 &d, Vec2 *vx ) const;

		Real AngleRadial( const Vec2 &d ) const;
		Real AngleDeg( const Vec2 &d ) const		{ return AngleRadial(d)*Real(180/M_PI); }

		static int Solve(	const Vec2 &p1, const Vec2 &d1,
						const Vec2 &p2, const Vec2 &d2, Real *t1 );

	Vec2 ScaleX(const Real &sx)	{ return Vec2(x_val*sx,y_val); }
	Vec2 ScaleY(const Real &sy)	{ return Vec2(x_val,y_val*sy); }

	inline const Vec2& operator=(const Vec2 &v);

	// Binary Operator Functions

	inline Vec2 operator+(const Vec2&) const;
	inline Vec2 operator-(const Vec2&) const;

#ifndef __TURBOC__
	friend inline Real operator*(const Vec2&, const Vec2&);
	friend inline Vec2 operator*(const Real&, const Vec2&);
	friend inline int operator==(const Vec2&, const Vec2&);
	friend inline int operator!=(const Vec2&, const Vec2&);
#else
	friend Real operator*(const Vec2&, const Vec2&);
	friend Vec2 operator*(const Real&, const Vec2&);
	friend int operator==(const Vec2&, const Vec2&);
	friend int operator!=(const Vec2&, const Vec2&);
#endif

	inline Vec2 operator*(const Real&) const;
	inline Vec2 operator/(const Real&) const;

	inline const Vec2& operator+=(const Vec2&);
	inline const Vec2& operator-=(const Vec2&);
	inline const Vec2& operator*=(const Real&);
	inline const Vec2& operator/=(const Real&);
	inline Vec2 operator+() const;
	inline Vec2 operator-() const;

};

inline const Vec2& Vec2::operator=(const Vec2 &v) {
	x_val = v.x_val;
	y_val = v.y_val;
	return *this;
}

inline Vec2 Vec2::operator+() const
{
	return *this;
}

inline Vec2 Vec2::operator-() const
{
	return Vec2(-x_val, -y_val);
}


// Definitions of compound-assignment operator member functions

inline const Vec2& Vec2::operator+=(const Vec2& z2)
{
	x_val += z2.x_val;
	y_val += z2.y_val;
	return *this;
}

inline const Vec2& Vec2::operator-=(const Vec2& z2)
{
	x_val -= z2.x_val;
	y_val -= z2.y_val;
	return *this;
}

inline const Vec2& Vec2::operator*=(const Real& val)
{
	x_val *= val;
	y_val *= val;
	return *this;
}

inline const Vec2& Vec2::operator/=(const Real& val)
{
	x_val /= val;
	y_val /= val;
	return *this;
}


// Definitions of non-member binary operator functions

inline Vec2 Vec2::operator+(const Vec2& z2) const
{
		  return Vec2(x_val + z2.x_val, y_val + z2.y_val);
}
inline Vec2 Vec2::operator-(const Vec2& z2) const
{
		  return Vec2(x_val - z2.x_val, y_val - z2.y_val);
}


inline Real operator*(const Vec2& z1, const Vec2& z2)
{
		  return z1.x_val*z2.x_val + z1.y_val*z2.y_val;
}
inline Vec2 Vec2::operator*(const Real& val2) const
{
		  return Vec2(x_val*val2, y_val*val2);
}
inline Vec2 operator*(const Real& val, const Vec2& z2)
{
		  return Vec2(z2.x_val*val, z2.y_val*val);
}

inline Vec2 Vec2::operator/(const Real& val) const
{
		  return Vec2(x_val/val, y_val/val);
}

inline int operator==(const Vec2& z1, const Vec2& z2)
{
		  return z1.x_val == z2.x_val && z1.y_val == z2.y_val;
}

inline int operator!=(const Vec2& z1, const Vec2& z2)
{
		  return z1.x_val != z2.x_val || z1.y_val != z2.y_val;
}

inline Real Vec2::SqrNorm() const				{ return X()*X()+Y()*Y(); }
inline Real Vec2::Norm() const					{ return sqrt(SqrNorm()); }
inline Vec2 Vec2::Norm1() const				{ return *this / Norm(); }

#endif

extern Vec2 Vec2Zero;

#endif