-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
99 lines (79 loc) · 2.22 KB
/
vector.cpp
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
#include "vector.hpp"
#include <ostream>
#include <cmath>
#include <string>
using namespace std;
Vector::Vector(double ix, double iy) {
this->x = ix;
this->y = iy;
}
Vector::Vector() {
this->x = 0;
this->y = 0;
}
double Vector::get_x() {
return this->x;
}
double Vector::get_y() {
return this->y;
}
double Vector::get_magnitude() {
return sqrt(this->x*this->x + this->y*this->y);
}
Vector Vector::get_normalized() {
double mag = this->get_magnitude();
return Vector(this->x/mag, this->y/mag);
}
string Vector::to_string() {
return std::to_string(this->x) + "," + std::to_string(this->y);
}
void Vector::set_x(double ix) {
this->x = ix;
}
void Vector::set_y(double iy) {
this->y = iy;
}
double Vector::distanceTo(Vector next) {
return sqrt((next.get_x()-this->get_x())*(next.get_x()-this->get_x())+(next.get_y()-this->get_y())*(next.get_y()-this->get_y()));
}
bool Vector::isBetween(Vector v1, Vector v2) {
if (v1.get_x() > v2.get_x())
if(this->get_x() > v1.get_x() || this->get_x() < v2.get_x()) return false;
else if (v1.get_x() < v2.get_x())
if (this->get_x() < v1.get_x() || this->get_x() > v2.get_x()) return false;
else
if (this->get_x() != v1.get_x()) return false;
if (v1.get_y() > v2.get_y())
if (this->get_y() > v1.get_y() || this->get_y() < v2.get_y()) return false;
else if (v1.get_y() < v2.get_y())
if (this->get_y() < v1.get_y() || this->get_y() > v2.get_y()) return false;
else
if (this->get_y() != v1.get_y()) return false;
return true;
}
ostream& operator<<(ostream& os, Vector v) {
os << v.to_string();
return os;
}
Vector operator*(double mult, Vector v) {
Vector out = Vector(v.get_x()*mult, v.get_y()*mult);
return out;
}
Vector operator*(Vector v, double mult) {
Vector out = Vector(v.get_x()*mult, v.get_y()*mult);
return out;
}
Vector operator+(Vector v1, Vector v2) {
Vector out = Vector(v1.get_x()+v2.get_x(), v1.get_y()+v2.get_y());
return out;
}
Vector operator-(Vector v1, Vector v2) {
Vector out = Vector(v1.get_x()-v2.get_x(), v1.get_y()-v2.get_y());
return out;
}
bool operator==(Vector v1, Vector v2) {
return v1.get_x()==v2.get_x() && v1.get_y()==v2.get_y();
}
bool operator !=(Vector v1, Vector v2) {
return !(v1 == v2);
}