-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.cpp
More file actions
86 lines (81 loc) · 1.95 KB
/
Vector2D.cpp
File metadata and controls
86 lines (81 loc) · 1.95 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
#include "Vector2D.h"
double Vector2D::Length() const
{
return sqrtf((float)x*(float)x + (float)y*(float)y);
}
double Vector2D::Distance(const Vector2D& vec) const
{
return sqrtf(((float)vec.x - (float)x)*((float)vec.x - (float)x) + ((float)vec.y - (float)y)*((float)vec.y - (float)y));
}
Vector2D& Vector2D::Normalize()
{
double xxyy = x*x + y*y;
double invLength = 1.0 / sqrtf((float)xxyy);
x *= invLength;
y *= invLength;
return *this;
}
double Vector2D::Dot(const Vector2D& rhs) const
{
return (x*rhs.x + y*rhs.y);
}
Vector2D Vector2D::operator-() const
{
return Vector2D(-x, -y);
}
Vector2D Vector2D::operator+(const Vector2D& rhs) const
{
return Vector2D(x + rhs.x, y + rhs.y);
}
Vector2D Vector2D::operator-(const Vector2D& rhs) const
{
return Vector2D(x - rhs.x, y - rhs.y);
}
Vector2D& Vector2D::operator+=(const Vector2D& rhs)
{
x += rhs.x; y += rhs.y; return *this;
}
Vector2D& Vector2D::operator-=(const Vector2D& rhs)
{
x -= rhs.x; y -= rhs.y; return *this;
}
Vector2D Vector2D::operator*(const double a) const
{
return Vector2D(x*a, y*a);
}
Vector2D Vector2D::operator*(const Vector2D& rhs) const
{
return Vector2D(x*rhs.x, y*rhs.y);
}
Vector2D& Vector2D::operator*=(const double a)
{
x *= a; y *= a; return *this;
}
Vector2D& Vector2D::operator*=(const Vector2D& rhs)
{
x *= rhs.x; y *= rhs.y; return *this;
}
Vector2D Vector2D::operator/(const double a) const
{
return Vector2D(x / a, y / a);
}
Vector2D& Vector2D::operator/=(const double a)
{
x /= a; y /= a; return *this;
}
bool Vector2D::operator==(const Vector2D& rhs) const
{
return (x == rhs.x) && (y == rhs.y);
}
bool Vector2D::operator!=(const Vector2D& rhs) const
{
return (x != rhs.x) || (y != rhs.y);
}
bool Vector2D::operator<(const Vector2D& rhs) const
{
if (x < rhs.x) return true;
if (x > rhs.x) return false;
if (y < rhs.y) return true;
if (y > rhs.y) return false;
return false;
}