-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.cpp
More file actions
94 lines (83 loc) · 1.82 KB
/
Vector2.cpp
File metadata and controls
94 lines (83 loc) · 1.82 KB
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
#include "Vector2.h"
#include <cmath>
/* Constructors */
Vector2::Vector2() : x(0), y(0) {}
Vector2::Vector2(double x, double y) : x(x), y(y) {}
/* OPERATORS */
Vector2 Vector2::operator+(const Vector2 &other) const
{
return Vector2(x + other.x, y + other.y);
}
Vector2 &Vector2::operator+=(const Vector2 &other)
{
x += other.x;
y += other.y;
return *this;
}
Vector2 Vector2::operator-(const Vector2 &other) const
{
return Vector2(x - other.x, y - other.y);
}
Vector2 &Vector2::operator-=(const Vector2 &other)
{
x -= other.x;
y -= other.y;
return *this;
}
Vector2 Vector2::operator*(double scalar) const
{
return Vector2(scalar * x, scalar * y);
}
Vector2 &Vector2::operator*=(double scalar)
{
x *= scalar;
y *= scalar;
return *this;
}
Vector2 Vector2::operator/(double scalar) const
{
return Vector2(x / scalar, y / scalar);
}
Vector2 &Vector2::operator/=(double scalar)
{
x /= scalar;
y /= scalar;
return *this;
}
bool Vector2::operator==(const Vector2 &other) const
{
return x == other.x && y == other.y;
}
/* FUNCTIONS */
double Vector2::magnitude() const
{
return (std::sqrt((x * x) + (y * y)));
}
Vector2 Vector2::normalize() const
{
double magnitude = this->magnitude();
if (magnitude == 0)
return Vector2(0, 0);
double normalX = x / magnitude;
double normalY = y / magnitude;
return Vector2(normalX, normalY);
}
double Vector2::dot(const Vector2 &other) const
{
return x * other.x + y * other.y;
}
double Vector2::distanceTo(const Vector2 &other) const
{
return (*this - other).magnitude();
}
Vector2 Vector2::rotate(double angleRadians) const
{
double cosA = std::cos(angleRadians);
double sinA = std::sin(angleRadians);
return Vector2(x * cosA - y * sinA, x * sinA + y * cosA);
}
void Vector2::zero()
{
x = 0;
y = 0;
}