-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
71 lines (50 loc) · 1.74 KB
/
Copy pathVector2D.cpp
File metadata and controls
71 lines (50 loc) · 1.74 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
#include "Vector2D.h"
// Конструкторы
// Методы установки значений с проверкой границ
void Vector2D::setX(float newX, float minX, float maxX) {
x = std::max(minX, std::min(maxX, newX));
}
void Vector2D::setY(float newY, float minY, float maxY) {
y = std::max(minY, std::min(maxY, newY));
}
void Vector2D::setPosition(float newX, float newY, float minX, float maxX, float minY, float maxY) {
setX(newX, minX, maxX);
setY(newY, minY, maxY);
}
// Операторы
Vector2D Vector2D::operator+(const Vector2D& other) const {//зачем 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;
}
// Проверка на нулевой вектор
bool Vector2D::isZero() const {
return x == 0 && y == 0;
}