-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.cpp
187 lines (134 loc) · 3.33 KB
/
Vector2D.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "Include/Vector2D.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
#undef min
#undef max
Vector2D::Vector2D(double x, double y)
{
this->x = x;
this->y = y;
}
void Vector2D::AddVec(Vector2D& vec)
{
this->x += vec.x;
this->y += vec.y;
}
void Vector2D::Fill(const double & XY){
x = y = XY;
}
void Vector2D::BorderStop(double* pos, const double MIN, const double MAX)
{
assert(MAX > MIN && "Vector2D::BorderStop()");
*pos = std::min(MAX, std::max(MIN, *pos));
}
void Vector2D::AddVecAngele(double * PosX, double * PosY, const double& angle, const double& force)
{
*PosX += std::cos(angle) * force;
*PosY += std::sin(angle) * force;
}
bool Vector2D::CirclePointCollision(const double& MyX, const double& MyY, const double& TarX, const double& TarY, const double& Radius)
{
const double& WIDTH = (TarX - MyX) * (TarX - MyX);
const double& HEIGHT = (TarY - MyY) * (TarY - MyY);
const double& DISTANCE = (WIDTH + HEIGHT);
const double& RADIUS = Radius * Radius;
bool isHit = (RADIUS >= DISTANCE);
return isHit;
}
bool Vector2D::CirclesCollision(const double & Range1, const double & Range2, const double & X1, const double & Y1, const double & X2, const double & Y2)
{
const double& hLengrth = (Range1 + Range2);
const double& xLength = (X1 - X2);
const double& yLength = (Y1 - Y2);
const bool& isHit = (hLengrth * hLengrth >= xLength * xLength + yLength * yLength);
return isHit;
}
bool Vector2D::RectanglePointCollision(const double & MyX, const double & MyY, const double & Width, const double & Height, const double & TarX, const double & TarY)
{
const double& MyX2 = (MyX + Width);
const double& MyY2 = (MyY + Height);
const bool& isHit = (MyX <= TarX && TarX <= MyX2 && MyY <= TarY && TarY <= MyY2);
return isHit;
}
Vector2D & Vector2D::GetVec(double x, double y)
{
return Vector2D(x, y);
}
Vector2D & Vector2D::GetVec2(const Vector2D & vec1, const Vector2D & vec2)
{
double x = vec2.x - vec1.x;
double y = vec2.y - vec1.y;
return Vector2D(x, y);
}
Vector2D & Vector2D::Normalize()
{
const double& len = Length();
if (len < FLT_EPSILON)
return Vector2D(0., 0.);
x /= len;
y /= len;
return *this;
}
double Vector2D::Length() const{
return std::sqrt(DotProduct(*this));
}
double Vector2D::DotProduct(const Vector2D & other) const {
return (x * other.x) + (y * other.y);
}
double Vector2D::CrossProduct(const Vector2D & other) const {
return (x * other.y) - (y * other.x);
}
Vector2D& Vector2D::operator=(const Vector2D& obj)
{
this->x = obj.x;
this->y = obj.y;
return *this;
}
Vector2D Vector2D::operator+()
{
return *this;
}
Vector2D Vector2D::operator-()
{
return Vector2D(-x, -y);
}
Vector2D& Vector2D::operator+=(const Vector2D& obj)
{
this->x += obj.x;
this->y += obj.y;
return *this;
}
Vector2D& Vector2D::operator-=(const Vector2D& obj)
{
this->x -= obj.x;
this->y -= obj.y;
return *this;
}
Vector2D& Vector2D::operator*=(const Vector2D& obj)
{
this->x *= obj.x;
this->y *= obj.y;
return *this;
}
Vector2D& Vector2D::operator/=(const Vector2D& obj)
{
this->x /= obj.x;
this->y /= obj.y;
return *this;
}
Vector2D operator+(const Vector2D& v1, const Vector2D& v2)
{
Vector2D tmp;
tmp.x = v1.x + v2.x;
tmp.y = v1.y + v2.y;
return tmp;
}
Vector2D operator-(const Vector2D& v1, const Vector2D& v2)
{
Vector2D tmp;
tmp.x = v1.x - v2.x;
tmp.y = v1.y - v2.y;
return tmp;
}
// EOF