-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec2f.cpp
115 lines (89 loc) · 1.6 KB
/
Vec2f.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
#include "Vec2f.h"
#include <math.h>
Vec2f::Vec2f()
{
}
Vec2f::Vec2f(float X, float Y)
: x(X), y(Y)
{
}
bool Vec2f::operator==(const Vec2f &other) const
{
if(x == other.x && y == other.y)
return true;
return false;
}
Vec2f Vec2f::operator*(float scale) const
{
return Vec2f(x * scale, y * scale);
}
Vec2f Vec2f::operator/(float scale) const
{
return Vec2f(x / scale, y / scale);
}
Vec2f Vec2f::operator+(const Vec2f &other) const
{
return Vec2f(x + other.x, y + other.y);
}
Vec2f Vec2f::operator-(const Vec2f &other) const
{
return Vec2f(x - other.x, y - other.y);
}
Vec2f Vec2f::operator-() const
{
return Vec2f(-x, -y);
}
const Vec2f &Vec2f::operator*=(float scale)
{
x *= scale;
y *= scale;
return *this;
}
const Vec2f &Vec2f::operator/=(float scale)
{
x /= scale;
y /= scale;
return *this;
}
const Vec2f &Vec2f::operator+=(const Vec2f &other)
{
x += other.x;
y += other.y;
return *this;
}
const Vec2f &Vec2f::operator-=(const Vec2f &other)
{
x -= other.x;
y -= other.y;
return *this;
}
float Vec2f::Magnitude() const
{
return sqrt(x * x + y * y);
}
float Vec2f::MagnitudeSquared() const
{
return x * x + y * y;
}
Vec2f Vec2f::Normalize() const
{
float m = sqrt(x * x + y * y);
return Vec2f(x / m, y / m);
}
float Vec2f::Dot(const Vec2f &other) const
{
return x * other.x + y * other.y;
}
float Vec2f::Cross(const Vec2f &other) const
{
return x * other.y - y * other.x;
}
Vec2f operator*(float scale, const Vec2f &v)
{
return v * scale;
}
std::ostream &operator<<(std::ostream &output, const Vec2f &v)
{
std::cout << '(' << v.x << ", " << v.y << ')';
return output;
}