-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVec2.cpp
More file actions
99 lines (79 loc) · 1.56 KB
/
Vec2.cpp
File metadata and controls
99 lines (79 loc) · 1.56 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
#include "Vec2.h"
#include <cmath>
Vec2::Vec2() : x(0.0f), y(0.0f)
{
}
Vec2::Vec2(float xin, float yin) : x(xin), y(yin)
{
}
bool Vec2::operator==(const Vec2& rhs) const
{
return (x == rhs.x && y == rhs.y);
}
bool Vec2::operator!=(const Vec2& rhs) const
{
return (x != rhs.x || y != rhs.y);
}
Vec2 Vec2::operator+(const Vec2& rhs) const
{
return Vec2(x + rhs.x, y + rhs.y);
}
Vec2 Vec2::operator*(const float value) const
{
return Vec2(x * value, y * value);
}
Vec2 Vec2::operator-(const Vec2& rhs) const
{
return Vec2(x - rhs.x, y - rhs.y);
}
Vec2 Vec2::operator*(const Vec2& rhs) const
{
return Vec2(x * rhs.x, y * rhs.y);
}
Vec2 Vec2::operator/(const Vec2& rhs) const
{
return Vec2(x / rhs.x , y / rhs.y);
}
Vec2 Vec2::operator+=(const Vec2& rhs)
{
x += rhs.x;
y += rhs.y;
return Vec2(x, y);
}
Vec2 Vec2::operator-=(const Vec2& rhs)
{
x -= rhs.x;
y -= rhs.y;
return Vec2(x,y);
}
Vec2 Vec2::operator*=(const Vec2& rhs)
{
return Vec2(x * rhs.x, y * rhs.y);
}
Vec2 Vec2::operator*=(const float value)
{
return Vec2(x * value, y * value);
}
Vec2 Vec2::operator/=(const float value)
{
return Vec2(x / value , y / value);
}
float Vec2::dist(const Vec2 rhs) const
{
//return distance between two vectors - sweet pythagoras
return sqrtf((x - rhs.x) * (x - rhs.x) + (y - rhs.y) * (y - rhs.y));
}
float Vec2::magnitude() const
{
return sqrtf((x * x) + (y * y));
}
Vec2 Vec2::normalize()
{
float mag = sqrtf((x * x) + (y * y));
//avoid division by zero
if (mag == 0) {
return Vec2(0, 0);
}
//return unit vector in same direction
return Vec2(x / mag, y / mag);
}