-
Notifications
You must be signed in to change notification settings - Fork 5
/
vector2.h
executable file
·107 lines (88 loc) · 1.78 KB
/
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
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
/******************************************************
g-Matrix3D Neo Engine
Copyright (c)2003 Kim Seong Wan (kaswan, Â𻧱ͽÅ)
E-mail: kaswan@hitel.net
http://www.g-matrix.pe.kr
*******************************************************/
#ifndef VECTOR2_H
#define VECTOR2_H
#include "matrix.h"
struct Vector2 {
union {
float x;
float u;
};
union {
float y;
float v;
};
Vector2(){}
Vector2(float a, float b)
{
x = a;
y = b;
}
~Vector2(){}
Vector2 operator-(void) const
{
return Vector2(-x, -y);
}
Vector2 operator+(const Vector2 &rhs) const
{
return Vector2(x + rhs.x, y + rhs.y);
};
Vector2 operator-(const Vector2 &rhs) const
{
return Vector2(x - rhs.x, y - rhs.y);
};
Vector2 operator*(const float scalar) const
{
return Vector2(x * scalar, y * scalar);
};
Vector2 operator/(const float scalar) const
{
return Vector2(x / scalar, y / scalar);
};
friend Vector2 operator*(const float scalar, const Vector2 &rhs);
float operator*(const Vector2 &rhs) const
{
return (x * rhs.x + y * rhs.y);
};
Vector2 operator*(const Matrix2 m) const
{
Vector2 temp;
temp.x = x * m._11 + y * m._21;
temp.y = x * m._12 + y * m._22;
return temp;
};
float operator^(const Vector2 &rhs) const
{
return (x * rhs.y - y * rhs.x);
}
Vector2 & operator+=(const Vector2 &rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Vector2 & operator-=(const Vector2 &rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
Vector2 & operator*=(const float scalar)
{
x *= scalar;
y *= scalar;
return *this;
}
Vector2 & operator/=(const float scalar)
{
x /= scalar;
y /= scalar;
return *this;
}
void Normalize(void);
};
#endif