-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.h
More file actions
60 lines (46 loc) · 1.41 KB
/
Vector2D.h
File metadata and controls
60 lines (46 loc) · 1.41 KB
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
#ifndef VECTOR3D_H
#define VECTOR3D_H
#include <math.h>
class Vector2D
{
public:
float x;
float y;
Vector2D ();
Vector2D ( float _x, float _y );
virtual ~Vector2D ();
Vector2D operator+ ( Vector2D v ) { return Vector2D( x + v.x, y + v.y ); }
Vector2D operator- ( Vector2D v ) { return Vector2D( x - v.x, y - v.y ); }
Vector2D operator* ( float value ) { return Vector2D( x * value, y * value ); }
Vector2D operator/ ( float value ) { return Vector2D( x / value, y / value ); }
Vector2D& operator+= ( Vector2D v ) { x += v.x; y += v.y; return *this; }
Vector2D& operator-= ( Vector2D v ) { x -= v.x; y -= v.y; return *this; }
Vector2D& operator*= ( float value ) { x *= value; y *= value; return *this; }
Vector2D& operator/= ( float value ) { x /= value; y /= value; return *this; }
Vector2D operator- () { return Vector2D ( -x, -y ); }
float length () { return sqrtf ( x*x + y*y ); }
void unitize () {
float length = this->length();
if ( length == 0 )
return;
x /= length;
y /= length;
}
Vector2D unit () {
float length = this->length();
if ( length == 0 )
return *this;
return Vector2D ( x / length, y / length );
}
};
Vector2D::Vector2D() {
x = 0;
y = 0;
}
Vector2D::Vector2D( float _x, float _y ) {
x = _x;
y = _y;
}
Vector2D::~Vector2D() {
}
#endif // VECTOR3D_H