/
Vector.cpp
178 lines (148 loc) · 3.45 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
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
177
178
#include "Vector.h"
#include <cmath>
#include <sstream>
Vector::Vector(): x(0), y(0){}
Vector::Vector(double _x, double _y): x(_x), y(_y){}
double Vector::getX() const
{
return x;
}
void Vector::setX(double _x)
{
x = _x;
return;
}
double Vector::getY() const
{
return y;
}
void Vector::setY(double _y)
{
y = _y;
return;
}
void Vector::setXY(double _x, double _y)
{
x = _x;
y = _y;
return;
}
double Vector::length() const // The length of the vector
{
return sqrt((*this)*(*this));
}
Vector Vector::unit() const // unit vector of the vector
{
if(this->length() == 0) return Vector(0,0);
else return (*this) / this->length();
}
double Vector::angle(const Vector & v) const // count the angle between two vectors
{
double lengthSquare = this->length() * v.length();
if(lengthSquare == 0) return 0;
else return acos(((*this) * v) / lengthSquare);
}
double Vector::cross(const Vector & v) const //count the scalar cross product
{
return x*(v.y) - y*(v.x);
}
Vector Vector::projectVector(const Vector & v) const // projection vector w.r.t v
{
if(v.length() == 0) return Vector(0,0);
return (((*this)*v) / (v*v))*v;
}
double Vector::projectLength(const Vector & v) const // projection length w.r.t v
{
if(v.length() == 0) return 0;
return (((*this)*v) / v.length());
}
Vector Vector::perp() const // perpendicular vector(on the right hand side !!
{
return Vector(y,-x);
}
Vector Vector::operator+(const Vector & v) const // Addition
{
return Vector(x + v.getX(), y + v.getY());
}
Vector Vector::operator-(const Vector & v) const // Subtraction
{
return Vector(x - v.getX(), y - v.getY());
}
Vector Vector::operator*(double c) const // Scalar multiplication
{
return Vector(x * c, y * c);
}
Vector operator*(double c,const Vector & v) // Scalar multiplication (It is friend function)
{
return Vector(c * v.getX(), c * v.getY());
}
double Vector::operator*(const Vector & v) const // Dot product
{
return x * v.getX() + y * v.getY();
}
Vector Vector::operator/(double c) const // Scalar multiplication
{
if(c == 0) throw "Cannot devide 0";
else return Vector(x / c, y / c);
}
Vector Vector::operator-() const // the inverse addition element
{
return Vector(-x, -y);
}
Vector& Vector::operator+=(const Vector & v) // define += operator
{
(*this) = (*this) + v;
return (*this);
}
Vector& Vector::operator-=(const Vector & v) // define -= operator
{
(*this) = (*this) - v;
return (*this);
}
Vector& Vector::operator*=(double c) // define -= operator, scalar muriplication
{
(*this) = (*this) * c;
return (*this);
}
Vector& Vector::operator/=(double c) // define /= operator, scalar muriplication
{
(*this) = (*this) / c;
return (*this);
}
bool Vector::operator==(const Vector v) const // judge if Vectors are equal
{
if(x == v.getX() && y == v.getY()) return true;
else false;
}
bool Vector::operator!=(const Vector v) const // judge if Vectors not equal
{
return !((*this) == v);
}
double& Vector::operator[](int i) // obtain Vector component by index i(0:x, 1:y)
{
switch(i)
{
case 0: return x;
case 1: return y;
default: throw "out of range";
}
}
Vector& Vector::operator=(const Vector & v) // define assignment operator
{
x = v.getX();
y = v.getY();
return (*this);
}
string Vector::toString() const // convert Vector to string form
{
string s;
stringstream ss;
ss<<"("<<x<<","<<y<<")";
ss>>s;
return s;
}
ostream& operator<<(ostream& output, const Vector & v) // define output stream (It is friend function)
{
output<<v.toString();
return output;
}