Commit | Line | Data |
---|---|---|
e68f847b PG |
1 | /* |
2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
ad9f1fb6 PG |
18 | #include "Vector2.h" |
19 | #include "mathw.h" | |
20 | ||
617dcc71 PG |
21 | /// ***** Constructors/Destructors ***** |
22 | ||
ad9f1fb6 PG |
23 | Vector2::Vector2() |
24 | : x(0), y(0) | |
25 | { | |
617dcc71 | 26 | |
ad9f1fb6 PG |
27 | } |
28 | Vector2::Vector2(float x, float y) | |
29 | : x(x), y(y) | |
30 | { | |
617dcc71 | 31 | |
ad9f1fb6 PG |
32 | } |
33 | Vector2::Vector2(const Vector2& vec) | |
34 | : x(vec.x), y(vec.y) | |
35 | { | |
617dcc71 PG |
36 | |
37 | } | |
38 | Vector2::~Vector2() | |
39 | { | |
40 | ||
ad9f1fb6 PG |
41 | } |
42 | ||
617dcc71 PG |
43 | /// ***** Public Class Methods ***** |
44 | ||
ad9f1fb6 PG |
45 | void Vector2::zero() |
46 | { | |
617dcc71 PG |
47 | x = 0; |
48 | y = 0; | |
ad9f1fb6 PG |
49 | } |
50 | void Vector2::unit() | |
51 | { | |
617dcc71 | 52 | float len = length(); |
ad9f1fb6 | 53 | |
617dcc71 PG |
54 | x /= len; |
55 | y /= len; | |
ad9f1fb6 PG |
56 | } |
57 | ||
58 | float Vector2::angle() const | |
59 | { | |
617dcc71 PG |
60 | //TODO |
61 | //return atan2A(y,x); | |
62 | return 0; | |
ad9f1fb6 PG |
63 | } |
64 | float Vector2::length() const | |
65 | { | |
54fe85c5 PG |
66 | return sqrt(sqrLength()); |
67 | } | |
68 | float Vector2::sqrLength() const | |
69 | { | |
70 | return x*x + y*y; | |
71 | } | |
72 | ||
73 | float Vector2::dot(const Vector2& v) const | |
74 | { | |
75 | return x*v.x + y*v.y; | |
ad9f1fb6 PG |
76 | } |
77 | ||
78 | ||
79 | string Vector2::toString() const | |
80 | { | |
617dcc71 PG |
81 | // long just to be safe |
82 | char* strX = new char[50]; | |
83 | char* strY = new char[50]; | |
84 | ||
85 | sprintf(strX, "%f", x); | |
86 | sprintf(strY, "%f", y); | |
87 | ||
88 | string val = (string)"Vector2 x: " + strX + ", y: " + strY; | |
ad9f1fb6 | 89 | |
617dcc71 PG |
90 | // deletes the allocated memory, not just what is used by sprintf |
91 | delete []strX; | |
92 | delete []strY; | |
ad9f1fb6 | 93 | |
617dcc71 | 94 | return val; |
ad9f1fb6 PG |
95 | } |
96 | void Vector2::print() const | |
97 | { | |
617dcc71 | 98 | printf("%s\n",toString().c_str()); |
ad9f1fb6 PG |
99 | } |
100 | ||
101 | ||
102 | Vector2 Vector2::add(const Vector2& vec) const | |
103 | { | |
617dcc71 | 104 | return Vector2(x+vec.x, y+vec.y); |
ad9f1fb6 PG |
105 | } |
106 | Vector2 Vector2::subtract(const Vector2& vec) const | |
107 | { | |
617dcc71 | 108 | return Vector2(x-vec.x, y-vec.y); |
ad9f1fb6 PG |
109 | } |
110 | Vector2 Vector2::multiply(float c) const | |
111 | { | |
617dcc71 | 112 | return Vector2(x*c, y*c); |
ad9f1fb6 PG |
113 | } |
114 | Vector2 Vector2::divide(float c) const | |
115 | { | |
617dcc71 | 116 | return Vector2(x/c, y/c); |
ad9f1fb6 PG |
117 | } |
118 | ||
617dcc71 | 119 | /// ***** Public Methods ***** |
ad9f1fb6 PG |
120 | |
121 | Vector2 operator+(const Vector2& vec1, const Vector2& vec2) | |
122 | { | |
617dcc71 | 123 | return vec1.add(vec2); |
ad9f1fb6 PG |
124 | } |
125 | Vector2 operator-(const Vector2& vec1, const Vector2& vec2) | |
126 | { | |
617dcc71 | 127 | return vec1.subtract(vec2); |
ad9f1fb6 PG |
128 | } |
129 | Vector2 operator*(float c, const Vector2& vec) | |
130 | { | |
617dcc71 | 131 | return vec.multiply(c); |
ad9f1fb6 PG |
132 | } |
133 | Vector2 operator*(const Vector2& vec, float c) | |
134 | { | |
617dcc71 | 135 | return vec.multiply(c); |
ad9f1fb6 PG |
136 | } |
137 | Vector2 operator/(const Vector2& vec, float c) | |
138 | { | |
617dcc71 | 139 | return vec.divide(c); |
ad9f1fb6 PG |
140 | } |
141 | ||
142 | ||
143 | void operator+=(Vector2& vec1, const Vector2& vec2) | |
144 | { | |
617dcc71 PG |
145 | vec1.x += vec2.x; |
146 | vec1.y += vec2.y; | |
ad9f1fb6 PG |
147 | } |
148 | void operator-=(Vector2& vec1, const Vector2& vec2) | |
149 | { | |
617dcc71 PG |
150 | vec1.x -= vec2.x; |
151 | vec1.y -= vec2.y; | |
ad9f1fb6 PG |
152 | } |
153 | void operator*=(Vector2& vec, float c) | |
154 | { | |
617dcc71 PG |
155 | vec.x *= c; |
156 | vec.y *= c; | |
ad9f1fb6 PG |
157 | } |
158 | void operator/=(Vector2& vec, float c) | |
159 | { | |
617dcc71 PG |
160 | vec.x /= c; |
161 | vec.y /= c; | |
ad9f1fb6 | 162 | } |