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