Commit | Line | Data |
---|---|---|
ad9f1fb6 PG |
1 | #include "Vector2.h" |
2 | #include "mathw.h" | |
3 | ||
4 | Vector2::Vector2() | |
5 | : x(0), y(0) | |
6 | { | |
7 | } | |
8 | Vector2::Vector2(float x, float y) | |
9 | : x(x), y(y) | |
10 | { | |
11 | } | |
12 | Vector2::Vector2(const Vector2& vec) | |
13 | : x(vec.x), y(vec.y) | |
14 | { | |
15 | } | |
16 | ||
17 | void Vector2::zero() | |
18 | { | |
19 | x = 0; | |
20 | y = 0; | |
21 | } | |
22 | void Vector2::unit() | |
23 | { | |
24 | float len = length(); | |
25 | ||
26 | x /= len; | |
27 | y /= len; | |
28 | } | |
29 | ||
30 | float Vector2::angle() const | |
31 | { | |
27b74820 PG |
32 | //return atan2A(y,x); |
33 | return 0; | |
ad9f1fb6 PG |
34 | } |
35 | float Vector2::length() const | |
36 | { | |
37 | return sqrt(x*x + y*y); | |
38 | } | |
39 | ||
40 | ||
41 | string Vector2::toString() const | |
42 | { | |
43 | char* strX = new char[50]; // long just to be safe | |
44 | char* strY = new char[50]; // long just to be safe | |
45 | sprintf(strX, "%f", x); | |
46 | sprintf(strY, "%f", y); | |
47 | ||
48 | string val = (string)"Vector2 x: " + strX + ", y: " + strY; | |
49 | delete []strX; // deletes the memory allocated, not just what is used by sprintf | |
50 | delete []strY; // deletes the memory allocated, not just what is used by sprintf | |
51 | ||
52 | return val; | |
53 | } | |
54 | void Vector2::print() const | |
55 | { | |
56 | printf("%s\n",toString().c_str()); | |
57 | } | |
58 | ||
59 | ||
60 | Vector2 Vector2::add(const Vector2& vec) const | |
61 | { | |
62 | return Vector2(x+vec.x, y+vec.y); | |
63 | } | |
64 | Vector2 Vector2::subtract(const Vector2& vec) const | |
65 | { | |
66 | return Vector2(x-vec.x, y-vec.y); | |
67 | } | |
68 | Vector2 Vector2::multiply(float c) const | |
69 | { | |
70 | return Vector2(x*c, y*c); | |
71 | } | |
72 | Vector2 Vector2::divide(float c) const | |
73 | { | |
74 | return Vector2(x/c, y/c); | |
75 | } | |
76 | ||
77 | ||
78 | Vector2 operator+(const Vector2& vec1, const Vector2& vec2) | |
79 | { | |
80 | return vec1.add(vec2); | |
81 | } | |
82 | Vector2 operator-(const Vector2& vec1, const Vector2& vec2) | |
83 | { | |
84 | return vec1.subtract(vec2); | |
85 | } | |
86 | Vector2 operator*(float c, const Vector2& vec) | |
87 | { | |
88 | return vec.multiply(c); | |
89 | } | |
90 | Vector2 operator*(const Vector2& vec, float c) | |
91 | { | |
92 | return vec.multiply(c); | |
93 | } | |
94 | Vector2 operator/(const Vector2& vec, float c) | |
95 | { | |
96 | return vec.divide(c); | |
97 | } | |
98 | ||
99 | ||
100 | void operator+=(Vector2& vec1, const Vector2& vec2) | |
101 | { | |
102 | vec1.x += vec2.x; | |
103 | vec1.y += vec2.y; | |
104 | } | |
105 | void operator-=(Vector2& vec1, const Vector2& vec2) | |
106 | { | |
107 | vec1.x -= vec2.x; | |
108 | vec1.y -= vec2.y; | |
109 | } | |
110 | void operator*=(Vector2& vec, float c) | |
111 | { | |
112 | vec.x *= c; | |
113 | vec.y *= c; | |
114 | } | |
115 | void operator/=(Vector2& vec, float c) | |
116 | { | |
117 | vec.x /= c; | |
118 | vec.y /= c; | |
119 | } |