-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2.cpp
127 lines (99 loc) · 2.13 KB
/
Vector2.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
116
117
118
119
120
121
122
123
124
125
126
127
#include "Vector2.h"
#include <math.h>
Vector2::Vector2()
:x(0.0f),y(0.0f){}
Vector2::Vector2(double _x,double _y)
:x(_x),y(_y){}
Vector2::Vector2(const Vector2 &_orig)
:x(_orig.getX()),y(_orig.getY()){}
double Vector2::operator[](int i)const{
if(i==0)
return x;
if(i==1)
return y;
return *((double *)this+i);
}
double &Vector2::operator[](int i){
if(i==0)
return x;
if(i==1)
return y;
return *((double *)this+i);
}
double Vector2::getX()const{
return x;
}
double Vector2::getY()const{
return y;
}
void Vector2::setX(double _x){
x=_x;
}
void Vector2::setY(double _y){
y=_y;
}
Vector2 operator+(const Vector2 &v1,const Vector2 &v2){
return Vector2(v1.x+v2.x,v1.y+v2.y);
}
Vector2 operator-(const Vector2 &v1,const Vector2 &v2){
return Vector2(v1.x-v2.x,v1.y-v2.y);
}
Vector2 operator*(const Vector2 &v1,double scale){
return Vector2(v1.x*scale,v1.y*scale);
}
Vector2 operator*(double scale,const Vector2 &v1){
return Vector2(v1.x*scale,v1.y*scale);
}
Vector2 operator/(const Vector2 &v1,double scale){
return Vector2(v1.x/scale,v1.y/scale);
}
Vector2 operator/(double scale,const Vector2 &v1){
return Vector2(v1.x/scale,v1.y/scale);
}
double dot(const Vector2 &v1,const Vector2 &v2){
return v1.x*v2.x+v1.y*v2.y;
}
double cross(const Vector2 &v1,const Vector2 &v2){
return v1.x*v2.y-v1.y*v2.x;
}
bool operator==(const Vector2 &v1,const Vector2 &v2){
if(v1.x!=v2.x)
return false;
if(v1.y!=v2.y)
return false;
return true;
}
bool operator!=(const Vector2 &v1,const Vector2 &v2){
return !(v1==v2);
}
const Vector2 &Vector2::operator+()const{
return *this;
}
Vector2 Vector2::operator-()const{
return Vector2(-x,-y);
}
Vector2 &Vector2::operator+=(const Vector2 &v1){
*this=*this+v1;
return *this;
}
Vector2 &Vector2::operator-=(const Vector2 &v1){
*this=*this-v1;
return *this;
}
Vector2 &Vector2::operator*=(double scale){
*this=*this*scale;
return *this;
}
Vector2 &Vector2::operator/=(double scale){
*this=*this/scale;
return *this;
}
double Vector2::length()const{
return sqrt(x*x+y*y);
}
double Vector2::sqrLength()const{
return x*x+y*y;
}
Vector2 normalize(const Vector2 &v){
return v/v.length();
}