2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
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.
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.
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/>.
21 /// ***** Constructors/Destructors *****
28 Vector2::Vector2(float x, float y)
33 Vector2::Vector2(const Vector2& vec)
43 /// ***** Public Class Methods *****
58 float Vector2::angle() const
64 float Vector2::length() const
66 return sqrt(sqrLength());
68 float Vector2::sqrLength() const
73 float Vector2::dot(const Vector2& v) const
79 string Vector2::toString() const
81 // long just to be safe
82 char* strX = new char[50];
83 char* strY = new char[50];
85 sprintf(strX, "%f", x);
86 sprintf(strY, "%f", y);
88 string val = (string)"Vector2 x: " + strX + ", y: " + strY;
90 // deletes the allocated memory, not just what is used by sprintf
96 void Vector2::print() const
98 printf("%s\n",toString().c_str());
102 Vector2 Vector2::add(const Vector2& vec) const
104 return Vector2(x+vec.x, y+vec.y);
106 Vector2 Vector2::subtract(const Vector2& vec) const
108 return Vector2(x-vec.x, y-vec.y);
110 Vector2 Vector2::multiply(float c) const
112 return Vector2(x*c, y*c);
114 Vector2 Vector2::divide(float c) const
116 return Vector2(x/c, y/c);
119 /// ***** Public Methods *****
121 Vector2 operator+(const Vector2& vec1, const Vector2& vec2)
123 return vec1.add(vec2);
125 Vector2 operator-(const Vector2& vec1, const Vector2& vec2)
127 return vec1.subtract(vec2);
129 Vector2 operator*(float c, const Vector2& vec)
131 return vec.multiply(c);
133 Vector2 operator*(const Vector2& vec, float c)
135 return vec.multiply(c);
137 Vector2 operator/(const Vector2& vec, float c)
139 return vec.divide(c);
143 void operator+=(Vector2& vec1, const Vector2& vec2)
148 void operator-=(Vector2& vec1, const Vector2& vec2)
153 void operator*=(Vector2& vec, float c)
158 void operator/=(Vector2& vec, float c)