-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec2.cpp
101 lines (79 loc) · 1.36 KB
/
vec2.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
#include "vec2.hpp"
#include <cmath>
#include <cassert>
#include <iostream>
vec2::vec2()
:x(0.0f),y(0.0f)
{}
vec2::vec2(float const x_param,float const y_param)
:x(x_param),y(y_param)
{}
vec2& operator+=(vec2& lhs,vec2 const& rhs)
{
lhs.x+=rhs.x;
lhs.y+=rhs.y;
return lhs;
}
vec2& operator-=(vec2& lhs,const vec2& rhs)
{
lhs.x-=rhs.x;
lhs.y-=rhs.y;
return lhs;
}
vec2& operator*=(vec2& v,float const s)
{
v.x*=s; v.y*=s;
return v;
}
vec2& operator/=(vec2& v,float const s)
{
assert(fabs(s)>10e-6);
v.x/=s; v.y/=s;
return v;
}
std::ostream& operator<<(std::ostream& sout,vec2 const& v)
{
sout<<"["<<v.x<<";"<<v.y<<"]";
return sout;
}
float norm(vec2 const& v)
{
return std::sqrt(v.x*v.x+v.y*v.y);
}
float dot(vec2 const& v0,vec2 const& v1)
{
return v0.x*v1.x+v0.y*v1.y;
}
vec2 normalize(vec2 const& v)
{
vec2 temp=v;
temp/=norm(v);
return temp;
}
vec2 operator+(vec2 const& v0,vec2 const& v1)
{
vec2 temp=v0;
temp+=v1;
return temp;
}
vec2 operator-(vec2 const& v0,vec2 const& v1)
{
vec2 temp=v0;
temp-=v1;
return temp;
}
vec2 operator*(vec2 const& v0,float const s)
{
vec2 temp=v0;
temp*=s;
return temp;
}
vec2 operator*(float s,vec2 const& v0)
{
return v0*s;
}
vec2 operator/(vec2 const& v0,float const s)
{
vec2 temp=v0;temp/=s;
return temp;
}