-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverlappingRectangles.cpp
More file actions
229 lines (186 loc) · 4.81 KB
/
Copy pathOverlappingRectangles.cpp
File metadata and controls
229 lines (186 loc) · 4.81 KB
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
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <filesystem>
#include <chrono>
using namespace std::chrono;
using namespace std;
class point {
long x;
long y;
public:
point()
{
x = 0;
y = 0;
}
point(long l, long r)
{
x = l;
y = r;
}
long getx()
{
return x;
}
long gety()
{
return y;
}
void setx(long i)
{
x = i;
}
void sety(long i)
{
y = i;
}
friend class rectangle;
};
class rectangle {
point LowerLeft, UpperRight;
public:
rectangle()
{
LowerLeft.setx(0);
LowerLeft.sety(0);
UpperRight.setx(0);
UpperRight.sety(0);
}
rectangle(point x, point y)
{
LowerLeft.setx(x.getx());
LowerLeft.sety(x.gety());
UpperRight.setx(y.getx());
UpperRight.sety(y.gety());
}
point get_LowerLeft(void)
{
return LowerLeft;
}
point get_UpperRight(void)
{
return UpperRight;
}
bool overlap(rectangle x)
{
if (LowerLeft.getx() >= x.get_UpperRight().getx() || x.get_LowerLeft().getx() >= UpperRight.getx())
{
return false;
}
if (LowerLeft.gety() >= x.get_UpperRight().gety() || x.get_LowerLeft().gety() >= UpperRight.gety())
{
return false;
}
return true;
}
};
bool check_set(vector<rectangle> temp, rectangle m)
{
for (int k = 0; k < temp.size(); k++)
{
if (m.overlap(temp[k]))
{
return false;
}
}
return true;
}
int divide_into_sets(vector<pair<rectangle, bool>> rectangles)
{
int count = 1;
int i = 0;
for (i = 0; i < rectangles.size() - 1; i++)
{
ofstream file;
vector <rectangle> temp;
if (rectangles[i].second == false)
{
//create a new set/group
file.open("group_" + to_string(count) + ".txt");
file << rectangles[i].first.get_LowerLeft().getx() << " " << rectangles[i].first.get_LowerLeft().gety() << " " << rectangles[i].first.get_UpperRight().getx() << " " << rectangles[i].first.get_UpperRight().gety() << endl;
count++;
rectangles[i].second = true;
temp.push_back(rectangles[i].first);
for (int j = i + 1; j < rectangles.size(); j++)
{
if (rectangles[j].second == false)
{
//check if it's overlapping with any of the rectangles ALREADY in the set
if (check_set(temp, rectangles[j].first))
{
rectangles[j].second = true;
file << rectangles[j].first.get_LowerLeft().getx() << " " << rectangles[j].first.get_LowerLeft().gety() << " " << rectangles[j].first.get_UpperRight().getx() << " " << rectangles[j].first.get_UpperRight().gety() << endl;
temp.push_back(rectangles[j].first);
}
}
}
}
file.close();
}
//check the last rectangle in the file
if (rectangles[rectangles.size() - 1].second == false)
{
ofstream file;
file.open("group_" + to_string(count) + ".txt");
file << rectangles[i].first.get_LowerLeft().getx() << " " << rectangles[i].first.get_LowerLeft().gety() << " " << rectangles[i].first.get_UpperRight().getx() << " " << rectangles[i].first.get_UpperRight().gety() << endl;
rectangles[i].second = true;
file.close();
}
return count-1;
}
int main()
{
char done = 'C';
do {
//x1,y1,x2,y2 are the points given in one line of the file
//Vector rectangles has a pair, pair.first stores the rectangle, pair.second indicated whether it belongs to a set or not
long x1, y1, x2, y2;
vector <pair<rectangle, bool>> rectangles;
string file_path;
cout << "Enter the correct and full file path (including .txt extension):" << endl;
cin >> file_path;
//if .txt extension isn't present
/*if (file_path.find(".txt") == NULL)
{
cout << "Path not correct, please ensert .txt extension file" << endl;
continue;
}*/
ifstream file(file_path);
//if the file can't be found
if (file.fail())
{
cout << "Can't find the file, please insert the correct path" << endl;
continue;
}
auto start = high_resolution_clock::now();
//Adding the given Rectangles in a Vector
while (file >> x1 >> y1 >> x2 >> y2)
{
point lower_left(x1, y1);
point upper_right(x2, y2);
rectangle rec(lower_left, upper_right);
rectangles.emplace_back(rec, false);
}
if (!file.eof())
{
cout << "Wrong file format!" << endl;
continue;
}
//close the file
file.close();
cout << "Number of Inputs = " << rectangles.size() << endl;
int count = divide_into_sets(rectangles);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << duration.count() << "millisecond" << endl;
cout << "Number of Output groups = " << count << endl;
cout << "To continue press C, to exit press any character:" << endl;
cin >> done;
} while (done == 'C' || done == 'c');
return 0;
}