-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
58 lines (49 loc) · 1.47 KB
/
Copy pathVector2D.cpp
File metadata and controls
58 lines (49 loc) · 1.47 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
#include "Vector2D.hpp"
#include <stdexcept>
namespace CGF
{
float Vector2D::angle(const Vector2D& other) const
{
float dotProduct = dot(other);
float magnitudes = magnitude() * other.magnitude();
if (magnitudes == 0) {
throw std::invalid_argument("Cannot calculate angle with zero magnitude vector.");
}
return acos(dotProduct / magnitudes);
}
float Vector2D::dot(const Vector2D& other) const
{
return mX * other.mX + mY * other.mY;
}
float Vector2D::magnitude() const
{
return sqrt(mX * mX + mY * mY);
}
Vector2D Vector2D::normalize() const
{
if (magnitude() == 0) {
throw std::invalid_argument("Cannot normalize a zero magnitude vector.");
}
float mag = magnitude();
return Vector2D(mX / mag, mY / mag);
}
Vector2D Vector2D::operator+(const Vector2D& other) const
{
return Vector2D(mX + other.mX, mY + other.mY);
}
Vector2D Vector2D::operator-(const Vector2D& other) const
{
return Vector2D(mX - other.mX, mY - other.mY);
}
Vector2D Vector2D::operator*(float scalar) const
{
return Vector2D(mX * scalar, mY * scalar);
}
Vector2D Vector2D::operator/(float scalar) const
{
if (scalar == 0) {
throw std::invalid_argument("Division by zero is not allowed.");
}
return Vector2D(mX / scalar, mY / scalar);
}
} // namespace CGF