-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2.cpp
More file actions
168 lines (137 loc) · 3.37 KB
/
Copy pathvector2.cpp
File metadata and controls
168 lines (137 loc) · 3.37 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
/**
* @file vector2.cpp
* Implementation of the Vector2 class.
*/
#include "vector2.h"
#include <cmath>
Vector2::Vector2() : x_(0), y_(0)
{
/* Nothing. See initialization list */
}
Vector2::Vector2(double px, double py) : x_(px), y_(py)
{
/* Nothing. See initialization list */
}
Vector2 Vector2::operator-(const Vector2& other) const
{
return Vector2(this->x_ - other.x_, this->y_ - other.y_);
}
Vector2 Vector2::operator+(const Vector2& other) const
{
return Vector2(this->x_ + other.x_, this->y_ + other.y_);
}
Vector2 Vector2::operator*(int constant)
{
return Vector2(this->x_ * constant, this->y_ * constant);
}
Vector2 Vector2::operator/(int constant)
{
return Vector2(this->x_ / constant, this->y_ / constant);
}
Vector2 Vector2::leftHandNormal() const
{
return Vector2(this->y_, -1 * this->x_);
}
Vector2 Vector2::rightHandNormal() const
{
return Vector2(-1 * this->y_, this->x_);
}
double Vector2::distanceTo(const Vector2& other) const
{
const double delta_x = this->x() - other.x();
const double delta_y = this->y() - other.y();
const double distanceSquared = (delta_x * delta_x) + (delta_y * delta_y);
const double distance = sqrt(distanceSquared);
return distance;
}
double Vector2::dotProduct(const Vector2& other) const
{
const double dot_product
= ((this->x() * other.x()) + (this->y() * other.y()));
return dot_product;
}
double Vector2::crossProduct(const Vector2& other) const
{
return (this->x() * other.y()) - (this->y() * other.x());
}
Vector2 Vector2::projectionOn(const Vector2& b) const
{
const double dot_product = this->dotProduct(b);
const double tlength2 = b.length2();
const double temp = dot_product / tlength2;
return Vector2(temp * b.x(), temp * b.y());
}
double Vector2::length2() const
{
return (this->x_ * this->x_) + (this->y_ * this->y_);
}
double Vector2::length() const
{
return sqrt(this->length2());
}
double Vector2::magnitude() const
{
return this->length();
}
Vector2 Vector2::normalize() const
{
const double tlength = this->length();
Vector2 tnormalize;
if (tlength > 0) {
tnormalize.setX(this->x() / tlength);
tnormalize.setY(this->y() / tlength);
}
return tnormalize;
}
bool Vector2::operator==(const Vector2& other) const
{
return (this->x_ == other.x_) && (this->y_ == other.y_);
}
double Vector2::x() const
{
return this->x_;
}
void Vector2::setX(double px)
{
this->x_ = px;
}
double Vector2::y() const
{
return this->y_;
}
void Vector2::setY(double py)
{
this->y_ = py;
}
bool Vector2::isNorthOf(const Vector2& other) const
{
return this->y() < other.y();
}
bool Vector2::isNorthWestOf(const Vector2& other) const
{
return this->isNorthOf(other) && this->isWestOf(other);
}
bool Vector2::isNorthEastOf(const Vector2& other) const
{
return this->isNorthOf(other) && this->isEastOf(other);
}
bool Vector2::isSouthOf(const Vector2& other) const
{
return this->y() > other.y();
}
bool Vector2::isSouthWestOf(const Vector2& other) const
{
return this->isSouthOf(other) && this->isWestOf(other);
}
bool Vector2::isSouthEastOf(const Vector2& other) const
{
return this->isSouthOf(other) && this->isEastOf(other);
}
bool Vector2::isWestOf(const Vector2& other) const
{
return this->x() < other.x();
}
bool Vector2::isEastOf(const Vector2& other) const
{
return this->x() > other.x();
}