-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
54 lines (43 loc) · 1.18 KB
/
Vector2D.cpp
File metadata and controls
54 lines (43 loc) · 1.18 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
#include "Vector2D.h"
Vector2D Vector2D::operator+(const Vector2D &other) const {
return {xPos + other.xPos, yPos + other.yPos};
}
Vector2D Vector2D::operator-(const Vector2D &other) const {
return {xPos - other.xPos, yPos - other.yPos};
}
Vector2D Vector2D::operator*(float scalar) const {
return {xPos * scalar, yPos * scalar};
}
Vector2D Vector2D::operator/(float scalar) const {
if (std::abs(scalar) < 1e-8f) return {0.0f, 0.0f};
return {xPos / scalar, yPos / scalar};
}
Vector2D &Vector2D::operator+=(const Vector2D &other) {
xPos += other.xPos;
yPos += other.yPos;
return *this;
}
Vector2D &Vector2D::operator-=(const Vector2D &other) {
xPos -= other.xPos;
yPos -= other.yPos;
return *this;
}
Vector2D &Vector2D::operator*=(float scalar) {
xPos *= scalar;
yPos *= scalar;
return *this;
}
float Vector2D::dot(const Vector2D &other) const {
return xPos * other.xPos + yPos * other.yPos;
}
float Vector2D::lengthSquared() const {
return xPos * xPos + yPos * yPos;
}
float Vector2D::length() const {
return std::sqrt(lengthSquared());
}
Vector2D Vector2D::normalized() const {
float len = length();
if (len < 1e-8f) return {0.0f, 0.0f};
return {xPos / len, yPos / len};
}