/
Coordinate.cpp
154 lines (125 loc) · 2.75 KB
/
Coordinate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Coordinate.cpp
*
* Created on: 17 Sep 2014
* Author: Jacques
*/
#include "Coordinate.h"
Coordinate::Coordinate() :
_x(0),
_y(0)
{ }
Coordinate::Coordinate(float x, float y) :
_x(x),
_y(y)
{ }
Coordinate::Coordinate(const Coordinate& pos) :
_x(pos._x),
_y(pos._y)
{ }
float Coordinate::x() const
{ return this->_x;}
float Coordinate::y() const
{ return _y;}
float Coordinate::angle() const
{ return atan2(_y, _x) + 2*PI;}
float Coordinate::magSquared() const
{ return _x*_x + _y*_y;}
float Coordinate::magnitude() const
{ return sqrt(magSquared());}
void Coordinate::print() const
{
std::cout << x() << " " << y() << std::endl;
}
void Coordinate::setX(float x)
{ _x = x;}
void Coordinate::setY(float y)
{ _y = y;}
Coordinate Coordinate::operator+(const Coordinate& coord) const
{
Coordinate coord1{x() + coord.x(), y() + coord.y()};
return coord1;
}
Coordinate Coordinate::operator-(const Coordinate& coord) const
{
Coordinate coord1{x() - coord.x(), y() - coord.y()};
return coord1;
}
void Coordinate::operator+=(const Coordinate& coord)
{
setX(_x + coord.x());
setY(_y + coord.y());
}
void Coordinate::operator-=(const Coordinate& coord)
{
setX(_x - coord.x());
setY(_y - coord.y());
}
float Coordinate::operator*(const Coordinate& coord) const
{
float dot = x() * coord.x() + y() * coord.y();
return dot;
}
bool Coordinate::operator==(const Coordinate& coord) const
{
if (coord.x() == x() && coord.y() == y())
return true;
return false;
}
bool Coordinate::operator>(const Coordinate& coord) const
{
if (this->magSquared() > coord.magSquared())
return true;
else return false;
}
bool Coordinate::operator<(const Coordinate& coord) const
{
if (this->magSquared() < coord.magSquared())
return true;
else return false;
}
Coordinate Coordinate::operator*(const float& rhs) const
{
float x1 = x()*rhs;
float y1 = y()*rhs;
Coordinate tempCoord{x1, y1};
return tempCoord;
}
Coordinate Coordinate::operator /(const float& rhs) const
{
float rhs1 = 1/rhs;
return this->operator *(rhs1);
}
bool Coordinate::operator>(const float& rhs) const
{
if (this->magSquared() > rhs*rhs)
return true;
return false;
}
bool Coordinate::operator<(const float& rhs) const
{
if (this->magSquared() < rhs*rhs)
return true;
return false;
}
bool Coordinate::operator==(const float& rhs) const
{
if (this->magSquared() == rhs*rhs)
return true;
return false;
}
void Coordinate::rotate(const float angle, const Coordinate& center)
{
this->operator-=(center);
rotate(angle);
this->operator +=(center);
}
void Coordinate::rotate(const float angle)
{
Coordinate col1{(float) cos(angle), (float) sin(angle)};
Coordinate col2{(float) -sin(angle), (float) cos(angle)};
float x0 = (*this)*col1;
float y0 = (*this)*col2;
setX(x0);
setY(y0);
}