-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
50 lines (38 loc) · 1010 Bytes
/
Copy pathVector2D.cpp
File metadata and controls
50 lines (38 loc) · 1010 Bytes
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
#include "Vector2D.h"
#include <cmath>
Vector2D::Vector2D() : x(0), y(0) {}
Vector2D::Vector2D(float x, float y) : x(x), y(y) {}
float Vector2D::getX() const {
return x;
}
float Vector2D::getY() const {
return y;
}
void Vector2D::setX(float x) {
this->x = x;
}
void Vector2D::setY(float y) {
this->y = y;
}
float Vector2D::dot(const Vector2D& other) const {
return x * other.x + y * other.y;
}
void Vector2D::normalize() {
float length = std::sqrt(x * x + y * y);
if (length > 0) {
x /= length;
y /= length;
}
}
float Vector2D::length() const {
return std::sqrt(x * x + y * y);
}
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);
}