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