-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.cpp
63 lines (48 loc) · 1.26 KB
/
Vector2D.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
#include "Vector2D.h"
#include <cmath>
Vector2D::Vector2D(const float x, const float y) noexcept : x(x), y(y)
{
}
Vector2D Vector2D::operator+(const Vector2D& vector)
{
return { this->x + vector.x, this->y + vector.y };
}
Vector2D Vector2D::operator-(const Vector2D& vector)
{
return { this->x - vector.x, this->y - vector.y };
}
Vector2D& Vector2D::operator+=(const Vector2D& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
Vector2D& Vector2D::operator-=(const Vector2D& vector)
{
this->x -= vector.x;
this->y -= vector.y;
return *this;
}
Vector2D Vector2D::operator*(const float scalar)
{
return { this->x * scalar, this->y * scalar };
}
void Vector2D::normalize()
{
double magnitude = std::sqrt((this->x * this->x) + (this->y * this->y));
this->x /= magnitude;
this->y /= magnitude;
}
void Vector2D::rotate(const Vector2D& center, const float angle)
{
Vector2D old = *this - center;
this->x = old.x * std::cos(angle) - old.y * std::sin(angle) + center.x;
this->y = old.x * std::sin(angle) + old.y * std::cos(angle) + center.y;
}
float Vector2D::getAngle() const
{
return std::atan2(this->x, this->y);
}
Size::Size(const int width, const int height) noexcept : width(width), height(height)
{
}