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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
| #include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#ifndef __PROGTEST__
struct CCoord
{
CCoord ( int x = 0, int y = 0 ) { m_X = x; m_Y = y; }
int m_X;
int m_Y;
};
#endif /* __PROGTEST__ */
#define fmin( a, b ) (((a) > (b)) ? (b) : (a))
#define fmax( a, b ) (((a) > (b)) ? (a) : (b))
//для qsort
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
//базовый класс для фигур
class CObject
{
public:
CObject(int _id, int x1=0, int x2=0, int y1=0, int y2=0)
:ID(_id), bx1(x1), bx2(x2), by1(y1), by2(y2) {}
virtual CObject* clone() const =0;//функция для копирования объекта. Нужна чтобы всё в списке хранить
virtual bool belong(int px, int py) const=0;//принадлежит ли фигуре точка
int ID;
//bounding box
int bx1;//левая
int bx2;//правая
int by1;//верхняя
int by2;//нижняя
};
class CRectangle: public CObject
{
public:
CRectangle(int ID,int _x1,int _y1,int _x2,int _y2)
:CObject(ID,min(_x1, _x2),max(_x1,_x2), max(_y2, _y1),min(_y2,_y1)), x1(_x1), y1(_y1), x2(_x2), y2(_y2)
{}
virtual CObject* clone() const
{
return new CRectangle(*this);
}
virtual bool belong(int x, int y) const
{
if((x>=x1 && x<=x2) && (y>=(y1) && y<=y2))
return true;
return false;
}
private:
//верхняя левая координата
int x1;
int y1;
//нижняя правая коордианата
int x2;
int y2;
};
class CCircle: public CObject
{
public:
CCircle(int ID, int _x, int _y, int _r)
:CObject(ID,_x-_r, _x+_r, _y+_r, _y-_r), x(_x), y(_y), r(_r) {}
virtual CObject* clone() const
{
return new CCircle(*this);
}
virtual bool belong(int px, int py) const
{
return ((x-px)*(x-px)+(y-py)*(y-py)<=(r*r));
}
private:
//координата центра
int x;
int y;
//радиус круга
int r;
};
class CTriangle: public CObject
{
public:
CTriangle(int ID, CCoord _a, CCoord _b, CCoord _c)
: CObject(ID,min(_a.m_X,min(_b.m_X,_c.m_X)), max(_a.m_X,max(_b.m_X,_c.m_X)),
max(_a.m_Y,max(_b.m_Y,_c.m_Y)), min(_a.m_Y,min(_b.m_Y,_c.m_Y)) ),
a(_a), b(_b), c(_c) {}
virtual CObject* clone() const
{
return new CTriangle(*this);
}
virtual bool belong(int x, int y) const
{
int k = (a.m_X - x) * (b.m_Y - a.m_Y) - (b.m_X - a.m_X) * (a.m_Y - y);
int l = (b.m_X - x) * (c.m_Y - b.m_Y) - (c.m_X - b.m_X ) * (b.m_Y - y);
int m = (c.m_X - x) * (a.m_Y - c.m_Y) - (a.m_X - c.m_X) * (c.m_Y - y);
if ((k >= 0 && l >= 0 && m >= 0) || (k <= 0 && l <= 0 && m <= 0))//(k==l==m==0)
return true;
return false;
}
private:
//точки
CCoord a;
CCoord b;
CCoord c;
};
class CPolygon: public CObject
{
public:
CPolygon(int ID, int n, const CCoord* v)
: CObject(ID),size(n), coord(new CCoord[size])
{
int xMax=v[0].m_X;
int xMin=v[0].m_X;
int yMax=v[0].m_Y;
int yMin=v[0].m_Y;
for(int i=0;i<size;i++)
coord[i]= v[i];
for(int i=1;i<size;i++)
{
if(coord[i].m_X>xMax)
xMax=coord[i].m_X;
else if(coord[i].m_X<xMin)
xMin=coord[i].m_X;
if(coord[i].m_Y>yMax)
yMax=coord[i].m_Y;
else if(coord[i].m_Y<yMin)
yMin=coord[i].m_Y;
}
bx1=xMin;
bx2=xMax;
by1=yMax;
by2=yMin;
}
CPolygon(const CPolygon& right)
:CObject(right.ID), size(right.size), coord(new CCoord[size])
{
for(int i=0;i<size;i++)
coord[i]=right.coord[i];
bx1= right.bx1;
bx2= right.bx2;
by1= right.by1;
by2= right.by2;
}
virtual CObject* clone() const
{
return new CPolygon(*this);
}
virtual bool belong(int x, int y) const
{
int i, crossings = 0;
for ( i = 0; i < size; i++ ) {
double x1 = coord[i].m_X;
double y1 = coord[i].m_Y;
double x2 = coord[(i + 1) % size].m_X;
double y2 = coord[(i + 1) % size].m_Y;
double d = (y - y1) * (x2 - x1) - (x - x1) * (y2 - y1);
if ( (y1 >= y) != (y2 >= y) ) {
crossings += y2 - y1 >= 0 ? d >= 0 : d <= 0;
}
if ( !d && fmin( x1, x2 ) <= x && x <= fmax( x1, x2 )
&& fmin( y1, y2 ) <= y && y <= fmax( y1, y2 ) ) {
return 1;
}
}
return crossings & 1;
}
private:
int size;//сколько координат
//указатель на массив координат
CCoord* coord;
};
class CScreen
{
public:
CScreen()
: head(NULL), tail(head), size(0) {}
void Add(const CObject& toAdd)//должна хранить как-то координаты фигуры что дают
{
Elem* add= new Elem(toAdd.clone(),0);
Elem* temp(head);
//пока левая коордианата x у следующего объекта меньше той что добавляем
while(temp && temp->next && temp->next->obj->bx1 < add->obj->bx1)
temp=temp->next;
if(temp)
add->next=temp->next, temp->next=add;
else
head=add;
}
void Optimize()
{
}
//должна вернуть сколько(resLen) фигур пересекают точку x,y и их id(массив res)
void Test(int x, int y, int& resLen, int* &res)
{
int size=1;
resLen= 0;
res= new int[size];
Elem* temp(head);
while(temp && temp->obj->bx1<x)
{
if(temp->obj->bx1 <= x && temp->obj->bx2 >= x)
if(temp->obj->by1 >= y && temp->obj->by2 <= y)
if(temp->obj->belong(x,y))
{
res[resLen++]=temp->obj->ID;
if(resLen==size)
{
size*=2;
res=(int*) realloc(res,sizeof(int)*size);
}
}
temp=temp->next;
}
if(resLen)
qsort(res,resLen,sizeof(int),compare);
}
private:
struct Elem
{
Elem(CObject* o, Elem* n)
:obj(o), next(n) {}
CObject* obj;
Elem* next;
};
Elem* head;
Elem* tail;
int size;
};
int main()
{
int * res, resLen;
CScreen S0;
S0 . Add ( CRectangle ( 1, 10, 20, 30, 40 ) );
S0 . Add ( CRectangle ( 2, 20, 10, 40, 30 ) );
S0 . Add ( CTriangle ( 3, CCoord ( 10, 20 ), CCoord ( 20, 10 ), CCoord ( 30, 30 ) ) );
S0 . Optimize();
S0 . Test ( 0, 0, resLen, res );
// resLen = 0, res = [ ]
delete [] res;
S0 . Test ( 21, 21, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 3, res = [ 1 2 3 ]
delete [] res;
S0 . Test ( 16, 17, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 1, res = [ 3 ]
delete [] res;
S0 . Test ( 30, 22, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 2, res = [ 1 2 ]
delete [] res;
S0 . Test ( 35, 25, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 1, res = [ 2 ]
delete [] res;
CScreen S1;
S1 . Add ( CCircle ( 1, 10, 10, 15 ) );
S1 . Add ( CCircle ( 2, 30, 10, 15 ) );
S1 . Add ( CCircle ( 3, 20, 20, 15 ) );
S1 . Optimize();
S1 . Test ( 0, 0, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 1, res = [ 1 ]
delete [] res;
S1 . Test ( 15, 15, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 2, res = [ 1 3 ]
delete [] res;
S1 . Test ( 20, 11, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 3, res = [ 1 2 3 ]
delete [] res;
S1 . Test ( 32, 8, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 1, res = [ 2 ]
delete [] res;
CScreen S2;
CCoord vertex1[4] = { CCoord ( 10, 0 ), CCoord ( 20, 20 ), CCoord ( 30, 20 ), CCoord ( 40, 0 ) };
S2 . Add ( CPolygon ( 1, 4, vertex1 ) );
CCoord vertex2[5] = { CCoord ( 20, 10 ), CCoord ( 10, 20 ), CCoord ( 25, 30 ), CCoord ( 40, 20 ), CCoord ( 30, 10 ) };
S2 . Add ( CPolygon ( 2, 5, vertex2 ) );
S2 . Optimize();
S2 . Test ( 25, 15, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 2, res = [ 1 2 ]
delete [] res;
S2 . Test ( 25, 25, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 1, res = [ 2 ]
delete [] res;
S2 . Test ( 15, 3, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 1, res = [ 1 ]
delete [] res;
S2 . Test ( 11, 10, resLen, res );
for(int i=0;i<resLen;cout<<res[i++]<<'\t');
cout<<endl;
// resLen = 0, res = [ ]
delete [] res;
} |