-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVector2D.cpp
More file actions
176 lines (138 loc) · 3.78 KB
/
Copy pathVector2D.cpp
File metadata and controls
176 lines (138 loc) · 3.78 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Created by Ryan Strauss on 12/9/19.
//
#include <cmath>
#include <stdexcept>
#include <random>
#include "Vector2D.h"
float Vector2D::get_random_float() {
static std::random_device rd;
static std::mt19937 engine(rd());
static std::uniform_real_distribution<float> dist(0, 1);
return dist(engine);
}
Vector2D::Vector2D(float x, float y) : x{x}, y{y} {}
Vector2D::Vector2D(const Vector2D &other) = default;
Vector2D::~Vector2D() = default;
Vector2D &Vector2D::operator=(const Vector2D &other) = default;
Vector2D &Vector2D::operator=(float scalar) {
x = scalar;
y = scalar;
return *this;
}
Vector2D Vector2D::operator+(const Vector2D &other) const {
return Vector2D{x + other.x, y + other.y};
}
Vector2D Vector2D::operator-(const Vector2D &other) const {
return Vector2D{x - other.x, y - other.y};
}
Vector2D Vector2D::operator+(float scalar) const {
return Vector2D{x + scalar, y + scalar};
}
Vector2D Vector2D::operator-(float scalar) const {
return Vector2D{x - scalar, y - scalar};
}
Vector2D &Vector2D::operator+=(const Vector2D &other) {
x += other.x;
y += other.y;
return *this;
}
Vector2D &Vector2D::operator-=(const Vector2D &other) {
x -= other.x;
y -= other.y;
return *this;
}
Vector2D &Vector2D::operator+=(float scalar) {
x += scalar;
y += scalar;
return *this;
}
Vector2D &Vector2D::operator-=(float scalar) {
x -= scalar;
y -= scalar;
return *this;
}
Vector2D Vector2D::operator*(const Vector2D &other) const {
return Vector2D{x * other.x, y * other.y};
}
Vector2D Vector2D::operator*(float scalar) const {
return Vector2D{x * scalar, y * scalar};
}
Vector2D &Vector2D::operator*=(const Vector2D &other) {
x *= other.x;
y *= other.y;
return *this;
}
Vector2D &Vector2D::operator*=(float scalar) {
x *= scalar;
y *= scalar;
return *this;
}
Vector2D Vector2D::operator/(const Vector2D &other) const {
return Vector2D{x / other.x, y / other.y};
}
Vector2D Vector2D::operator/(float scalar) const {
return Vector2D{x / scalar, y / scalar};
}
Vector2D &Vector2D::operator/=(const Vector2D &other) {
if (other.x == 0 || other.y == 0)
throw std::invalid_argument("Divide by zero.");
x /= other.x;
y /= other.y;
return *this;
}
Vector2D &Vector2D::operator/=(float scalar) {
if (scalar == 0)
throw std::invalid_argument("Divide by zero.");
x /= scalar;
y /= scalar;
return *this;
}
bool Vector2D::operator==(const Vector2D &other) const {
return x == other.x && y == other.y;
}
bool Vector2D::operator!=(const Vector2D &other) const {
return !(x == other.x && y == other.y);
}
Vector2D Vector2D::operator-() const {
return Vector2D(-x, -y);
}
float Vector2D::distance(const Vector2D &other) const {
float dx = x - other.x;
float dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
float Vector2D::toroidal_distance2(const Vector2D &other, float width, float height) const {
float dx = x - other.x;
float dy = y - other.y;
if (dx > width / 2)
dx = width - dx;
if (dy > height / 2)
dy = height - dy;
return dx * dx + dy * dy;
}
float Vector2D::toroidal_distance(const Vector2D &other, float width, float height) const {
return sqrt(toroidal_distance2(other, width, height));
}
float Vector2D::norm() const {
return sqrt(x * x + y * y);
}
Vector2D &Vector2D::normalize() {
float magnitude = norm();
if (magnitude != 0) {
x /= magnitude;
y /= magnitude;
}
return *this;
}
Vector2D &Vector2D::limit(float max) {
float magnitude = norm();
if (magnitude > max) {
x *= max / magnitude;
y *= max / magnitude;
}
return *this;
}
Vector2D Vector2D::random() {
return Vector2D{get_random_float(), get_random_float()};
}