added GPL copyrights
[physics.git] / src / Vector2.cpp
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
18 #include "Vector2.h"
19 #include "mathw.h"
20
21 /// ***** Constructors/Destructors *****
22
23 Vector2::Vector2()
24   : x(0), y(0)
25 {
26
27 }
28 Vector2::Vector2(float x, float y)
29   : x(x), y(y)
30 {
31
32 }
33 Vector2::Vector2(const Vector2& vec)
34   : x(vec.x), y(vec.y)
35 {
36
37 }
38 Vector2::~Vector2()
39 {
40
41 }
42
43 /// ***** Public Class Methods *****
44
45 void Vector2::zero()
46 {
47     x = 0;
48     y = 0;
49 }
50 void Vector2::unit()
51 {
52     float len = length();
53
54     x /= len;
55     y /= len;
56 }
57
58 float Vector2::angle() const
59 {
60     //TODO
61     //return atan2A(y,x);
62     return 0;
63 }
64 float Vector2::length() const
65 {
66     return sqrt(x*x + y*y);
67 }
68
69
70 string Vector2::toString() const
71 {
72      // long just to be safe
73     char* strX = new char[50];
74     char* strY = new char[50];
75
76     sprintf(strX, "%f", x);
77     sprintf(strY, "%f", y);
78
79     string val = (string)"Vector2  x: " + strX + ", y: " + strY;
80
81      // deletes the allocated memory, not just what is used by sprintf
82     delete []strX;
83     delete []strY;
84
85     return val;
86 }
87 void Vector2::print() const
88 {
89     printf("%s\n",toString().c_str());
90 }
91
92
93 Vector2 Vector2::add(const Vector2& vec) const
94 {
95     return Vector2(x+vec.x, y+vec.y);
96 }
97 Vector2 Vector2::subtract(const Vector2& vec) const
98 {
99     return Vector2(x-vec.x, y-vec.y);
100 }
101 Vector2 Vector2::multiply(float c) const
102 {
103     return Vector2(x*c, y*c);
104 }
105 Vector2 Vector2::divide(float c) const
106 {
107     return Vector2(x/c, y/c);
108 }
109
110 /// ***** Public Methods *****
111
112 Vector2 operator+(const Vector2& vec1, const Vector2& vec2)
113 {
114     return vec1.add(vec2);
115 }
116 Vector2 operator-(const Vector2& vec1, const Vector2& vec2)
117 {
118     return vec1.subtract(vec2);
119 }
120 Vector2 operator*(float c, const Vector2& vec)
121 {
122     return vec.multiply(c);
123 }
124 Vector2 operator*(const Vector2& vec, float c)
125 {
126     return vec.multiply(c);
127 }
128 Vector2 operator/(const Vector2& vec, float c)
129 {
130     return vec.divide(c);
131 }
132
133
134 void operator+=(Vector2& vec1, const Vector2& vec2)
135 {
136     vec1.x += vec2.x;
137     vec1.y += vec2.y;
138 }
139 void operator-=(Vector2& vec1, const Vector2& vec2)
140 {
141     vec1.x -= vec2.x;
142     vec1.y -= vec2.y;
143 }
144 void operator*=(Vector2& vec, float c)
145 {
146     vec.x *= c;
147     vec.y *= c;
148 }
149 void operator/=(Vector2& vec, float c)
150 {
151     vec.x /= c;
152     vec.y /= c;
153 }