-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2d.cpp
106 lines (86 loc) · 1.53 KB
/
vector2d.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
#include "vector2d.h"
#include <cmath>
using std::sqrt;
Vector2D::Vector2D(double _x, double _y)
:
x(_x), y(_y)
{
}
Vector2D::~Vector2D()
{
}
Vector2D &Vector2D::operator +=(const Vector2D &rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Vector2D operator +(const Vector2D <, const Vector2D &rt)
{
Vector2D temp(lt);
temp += rt;
return temp;
}
Vector2D &Vector2D::operator -=(const Vector2D &rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
Vector2D operator -(const Vector2D <, const Vector2D &rt)
{
Vector2D temp(lt);
temp -= rt;
return temp;
}
Vector2D &Vector2D::operator *=(double a)
{
x *= a;
y *= a;
return *this;
}
Vector2D &Vector2D::operator /=(double a)
{
return this->operator*=(1.0 / a);
}
Vector2D operator *(const Vector2D <, double a)
{
Vector2D temp(lt);
temp *= a;
return temp;
}
Vector2D operator *(double a, const Vector2D &rt)
{
Vector2D temp(rt);
temp *= a;
return temp;
}
Vector2D operator /(const Vector2D <, double a)
{
Vector2D temp = lt;
temp /= a;
return temp;
}
bool operator==(const Vector2D <, const Vector2D &rt)
{
return (lt.x == rt.x) && (lt.x == rt.y);
}
bool operator!=(const Vector2D <, const Vector2D &rt)
{
return !(lt == rt);
}
Vector2D &Vector2D::normalize()
{
double a = sqrt(x*x + y*y);
y = y / a;
x = x / a;
return *this;
}
Vector2D Vector2D::normal()
{
return Vector2D(*this).normalize();
}
double Vector2D::operator()(const Vector2D & rhs)
{
return x * rhs.x + y * rhs.y;
}