-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvector2.h
195 lines (144 loc) · 4.84 KB
/
cvector2.h
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
/***************************************************************************
*
* Copyright (c) 2003 - 2011 Petri Purho
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
***************************************************************************/
#ifndef INC_CVECTOR2_H
#define INC_CVECTOR2_H
#include <math.h>
namespace ceng {
namespace math {
//=============================================================================
// Still in the works templated vector class
template< class Type >
class CVector2
{
public:
typedef Type unit_type;
//=========================================================================
CVector2() : x( Type() ), y( Type() ) { }
CVector2( const Type& x, const Type& y) : x( x ), y( y ) { }
template< class T >
explicit CVector2( const T& other ) : x( (Type)( other.x ) ), y( (Type)(other.y) ) { }
void Set( const Type& x_, const Type& y_) { x = x_; y = y_; }
//=========================================================================
bool operator== ( const CVector2< Type >& other ) const { return ( this->x == other.x && this->y == other.y ); }
bool operator!= ( const CVector2< Type >& other ) const { return !operator==( other ); }
//=========================================================================
CVector2< Type > operator -() const { return CVector2< Type >( -x, -y ); }
CVector2< Type >& operator += ( const CVector2< Type >& v )
{
x += v.x; y += v.y;
return *this;
}
CVector2< Type >& operator -= ( const CVector2< Type >& v )
{
x -= v.x; y -= v.y;
return *this;
}
CVector2< Type >& operator *= ( const Type& a )
{
x *= a; y *= a;
return *this;
}
//-------------------------------------------------------------------------
CVector2< Type > operator + ( const CVector2< Type >& other ) const
{
return CVector2< Type >( this->x + other.x, this->y + other.y );
}
CVector2< Type > operator - ( const CVector2< Type >& other ) const
{
return CVector2< Type >( this->x - other.x, this->y - other.y );
}
CVector2< Type > operator * ( float t ) const
{
return CVector2< Type >( this->x * t, this->y * t );
}
//=========================================================================
Type Dot( const CVector2< Type >& a, const CVector2< Type >& b ) const
{
return a.x * b.x + a.y * b.y;
}
Type Cross( const CVector2< Type >& a, const CVector2< Type >& b ) const
{
return a.x * b.y - a.y * b.x;
}
CVector2< Type > Cross( const CVector2< Type >& a, const Type& s ) const
{
return CVector2< Type >( s * a.y, -s * a.x );
}
CVector2< Type > Cross( const Type& s, const CVector2< Type >& a ) const
{
return CVector2< Type >( -s * a.y, s * a.x );
}
//=========================================================================
Type LengthSquared() const
{
return ( x * x + y * y );
}
Type Length() const
{
return (Type)sqrtf( (float)LengthSquared() );
}
CVector2< Type > Normalize() const
{
Type d = Length();
if( d > 0 )
return CVector2< Type >( x / d, y / d );
else
return CVector2< Type >( 0, 0 );
}
//=========================================================================
// ripped from:
// http://forums.indiegamer.com/showthread.php?t=10459
Type Angle() const
{
CVector2< Type > normal = Normalize();
Type angle = atan2( normal.y, normal.x );
return angle;
}
Type Angle( const CVector2< Type >& x ) const
{
Type dot = Dot( *this, x );
Type cross = Cross( *this, x );
// angle between segments
Type angle = (Type) atan2( cross, dot );
return angle;
}
CVector2< Type >& Rotate( Type angle )
{
Type tx = x;
x = (Type)x * (Type)cos(angle) - y * (Type)sin(angle);
y = (Type)tx * (Type)sin(angle) + y * (Type)cos(angle);
return *this;
}
CVector2< Type >& Rotate( const CVector2< Type >& centre, Type angle )
{
CVector2< Type > D = *this - centre;
D.Rotate( angle );
// *this = xCentre + D;
D += centre;
Set( D.x, D.y );
return *this;
}
//=========================================================================
Type x;
Type y;
};
} // end of namespace math
} // end of namespace ceng
#endif