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