-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2.h
62 lines (46 loc) · 970 Bytes
/
vector2.h
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
#pragma once
#include <cmath>
template<class T>
class Vector2
{
public:
T x = 0;
T y = 0;
public:
Vector2() = default;
~Vector2() = default;
Vector2(T x, T y) :x(x), y(y) {}
Vector2<T> operator+(const Vector2<T>& vec)const {
return Vector2<T>(x + vec.x, y + vec.y);
}
Vector2<T> operator-(const Vector2<T>& vec)const {
return Vector2<T>(x - vec.x, y - vec.y);
}
T operator*(const Vector2<T>& vec)const//向量相乘 返回值为数值
{
return x * vec.x + y * vec.y;
}
Vector2<T> operator*(T val)const//向量乘数值 返回值为向量
{
return Vector2<T>(val * x, val * y);
}
void operator+=(const Vector2<T>& vec) {
x += vec.x;
y += vec.y;
}
void operator-=(const Vector2<T>& vec) {
x -= vec.x;
y -= vec.y;
}
float length() //返回向量长度
{
return sqrt(x * x + y * y);
}
Vector2<T> normalize()//向量标准化
{
float len = length();
if (len == 0) return Vector2<T>(0, 0);
return Vector2<T>(x / len, y / len);
}
private:
};