-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.cpp
243 lines (239 loc) · 5.71 KB
/
data.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <math.h>
#include <iostream>
#include <algorithm>
#include <assert.h>
class cPoint {
float _x;
float _y;
public:
cPoint(float x,float y) {
_x = x;
_y = y;
}
float x() {
return _x;
}
float y() {
return _y;
}
bool operator<(cPoint other) {
if(_x==other._x) return _y<other._y;
return _x<other._x;
}
bool operator==(cPoint other) {
return ((x() == other.x())&&(y() == other.y()));
}
double operator^(cPoint other) {
return x()*other.y()-y()*other.x();
}
bool operator!=(cPoint other) {
return ((x() != other.x())||(y() != other.y()));
}
bool equalTo(cPoint* other) {
return ((x() == other->x())&&(y() == other->y()));
}
float distance(cPoint* other){
return sqrt(pow((other->x()-x()),2.0)+pow((other->y()-y()),2.0));
}
float distance(cPoint other){
return sqrt(pow((other.x()-x()),2.0)+pow((other.y()-y()),2.0));
}
bool clockwise(cPoint* p1, cPoint* p2) {
cPoint A=*p1-(*this);
cPoint B=*p2-*p1;
return ((A^B)<0);
//float theta1 = atan2((p1->y()-y()),(p1->x()-x()));
//float theta2 = atan2((p2->y()-p1->y()),(p2->x()-p1->x()));
}
double three_points_angle(cPoint* p1, cPoint* p2){
cPoint A=*p1-(*this);
cPoint B=*p2-*p1;
return asin((A^B)/(A.abs()*B.abs()));
}
cPoint* four_points_crossing(cPoint* p1, cPoint* p2, cPoint* p3) {
float m1 = (x()-p1->x())/(y()-p1->y());
float m2 = (p2->x()-p3->x())/(p2->y()-p3->y());
float return_x = (m1*m2*(p3->y()-y())+m2*x()-m1*p3->x())/(m2-m1);
float return_y = (x()-p3->x()+m2*p3->y()-m1*y())/(m2-m1);
cPoint* return_point = new cPoint(return_x,return_y);
return return_point;
}
cPoint operator-(cPoint other) {
//std::cout<<x()<<" "<<other.x()<<" "<<y()<<" "<<other.y()<<std::endl;
cPoint out(x()-other.x(),y()-other.y());
return out;
}
float abs()
{
return sqrt(_x*_x+_y*_y);
}
float angle(cPoint other)
{
float now_angle = atan2(fabs(other.x()-x()),fabs(other.y()-y()));
if((other.x()>x())&&(other.y()>y())) return now_angle;
if((other.x()>x())&&(other.y()<y())) return 3.1416-now_angle;
if((other.x()<x())&&(other.y()<y())) return 3.1416+now_angle;
if((other.x()<x())&&(other.y()>y())) return 6.283-now_angle;
}
};
class Wall {
int _xs;
int _ys;
int _xe;
int _ye;
float wall_angle;
float wall_length;
public:
Wall (int xs, int ys, int xe, int ye) {
_xs = xs;
_ys = ys;
_xe = xe;
_ye = ye;
wall_length = sqrt(pow((_xs-_xe),2.0)+pow((_ys-_ye),2.0));
wall_angle = atan(((float)(_xe-_xs))/(_ye-_ys));
}
Wall (cPoint a, cPoint b)
{
_xs = a.x();
_ys = a.y();
_xe = b.x();
_ye = b.y();
wall_length = sqrt(pow((_xs-_xe),2.0)+pow((_ys-_ye),2.0));
wall_angle = atan(((float)(_xe-_xs))/(_ye-_ys));
}
bool operator==(Wall other)
{
if(s()==other.s()&&e()==other.e()) return true;
if(e()==other.s()&&s()==other.e()) return true;
return false;
}
bool pointLies(cPoint p)
{
if(p==s()||p==e()) return true;
if(fabs(((p.y()-e().y())*(p.x()-s().x()))-((p.y()-s().y())*(p.x()-e().x())))>0.001) return false;
if(p.x()<std::min(xs(),xe())||p.x()>std::max(xs(),xe()) ) return false;
if(p.y()<std::min(ys(),ye())||p.y()>std::max(ys(),ye()) ) return false;
return true;
}
cPoint s() {
return cPoint(_xs,_ys);
}
cPoint e() {
return cPoint(_xe,_ye);
}
void swap()
{
std::swap(_xs,_xe);
std::swap(_ys,_ye);
}
float length() {
return wall_length;
}
float angle() {
return wall_angle;
}
int xs() {
return _xs;
}
int xe() {
return _xe;
}
int ys() {
return _ys;
}
int ye() {
return _ye;
}
Wall* combine(Wall* other) {
//assert (other->angle()==angle());
Wall* return_wall;
if (((other->xs()==xs())&&(other->ys()==ys()))) {
return_wall = new Wall(other->xe(),other->ye(),xe(),ye());
}
if (((other->xs()==xe())&&(other->ys()==ye()))) {
return_wall = new Wall(other->xe(),other->ye(),xs(),ys());
}
if (((other->xe()==xe())&&(other->ye()==ye()))) {
return_wall = new Wall(other->xs(),other->ys(),xs(),ys());
}
if (((other->xe()==xs())&&(other->ye()==ys()))) {
return_wall = new Wall(other->xs(),other->ys(),xe(),ye());
}
return return_wall;
}
bool can_combine(Wall* other) {
if (((other->xs()==xs())&&(other->ys()==ys())) || ((other->xe()==xe())&&(other->ye()==ye())) || ((other->xs()==xe())&&(other->ys()==ye())) || ((other->xe()==xs())&&(other->ye()==ys()))) {
if ((other->ys() == other->ye())&& (ys()==ye())) {
return true;
}
else {
return ((((float)(other->xs()-other->xe()))/(other->ys()-other->ye())) == (((float)(xs()-xe()))/(ys()-ye())));
}
}
else {return false;}
}
};
class Location {
float _x;
float _y;
float _theta;
public:
Location(float xval, float yval, float thetaval)
{
_x = xval;
_y = yval;
_theta = thetaval;
}
Location(Location* other)
{
_x = other->x();
_y = other->y();
_theta = other->theta();
}
void incAngle(float inc)
{
_theta+=inc;
}
void set(float xval, float yval, float thetaval)
{
_x = xval;
_y = yval;
_theta = thetaval;
}
float x()
{
return _x;
}
float y()
{
return _y;
}
float theta()
{
return _theta;
}
Location* move(float dis,float inputtheta) {
Location* returnlocation;
float returntheta = theta() + inputtheta;
float returnx = x()+dis*sin(returntheta);
float returny = y()+dis*cos(returntheta);
returnlocation = new Location(returnx, returny, returntheta);
return returnlocation;
}
Location* move_forward(float dis) {
Location* return_location;
float return_theta = theta();
float return_x = x() + dis*sin(return_theta);
float return_y = y() + dis*cos(return_theta);
return_location = new Location(return_x,return_y,return_theta);
return return_location;
}
cPoint* point() {
cPoint* pt = new cPoint(x(),y());
return pt;
}
float distance(Location* other)
{
return sqrt(pow((other->x()-x()),2.0)+pow((other->y()-y()),2.0));
}
};