-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraham.cpp
More file actions
263 lines (215 loc) · 6.74 KB
/
Graham.cpp
File metadata and controls
263 lines (215 loc) · 6.74 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
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
#include <iostream>
#include "lista_jed.h"
#include "Graham.h"
void import_from_to(const char *string, Graham *graham);
double dist(point * a, point * b)
{
double xLength = a->x - b->x;
double yLength = a->y - b->y;
return xLength * xLength + yLength * yLength;
}
double cmp(point * a, point * b, point * c)
{
return (b->y - a->y) * (c->x - b->x) - (b->x - a->x) * (c->y - b->y);
}
bool cmp_for_sort(point * startpoint, point * b, point * c)
{
double mult = cmp(startpoint, b, c);
if(mult == 0)
{
if (dist(startpoint, c) < dist(startpoint, b))
{
return true;
}
} else if (mult > 0)
{
return true;
}
return false;
}
int main() {
Graham ob_g;
import_from_to("points4.txt", &ob_g);
ob_g.setStartPoint();
auto t1 = std::chrono::high_resolution_clock::now();
ob_g.sort(cmp_for_sort);
auto t2 = std::chrono::high_resolution_clock::now();
auto timeSort = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
t1 = std::chrono::high_resolution_clock::now();
ob_g.graham_scan(cmp);
t2 = std::chrono::high_resolution_clock::now();
auto timeLoop = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
std::cout<< ob_g.print_data(timeSort, timeLoop);
ob_g.print_graph();
ob_g.clear();
return 0;
}
void import_from_to(const char *string, Graham * graham) {
std::ifstream file;
file.open(string);
if(!file)
{
std::cerr<<"Nie mozna takiego pliku otworzyc";
exit(1);
}
int n;
file>>n;
point ** points = new point*[n];
for(int i = 0; i<n; i++)
{
double x,y;
file>>x>>y;
points[i] = new point(x,y);
}
*graham = {points, n};
file.close();
}
#ifndef GRAHAM_H
#define GRAHAM_H
#include "gnuplot-iostream.h"
#include "HeapSort.h"
template<typename T> std::string toStr(const T& t)
{
std::ostringstream os;
os << t;
return os.str();
}
struct Point
{
double x, y;
Point(double d, double d1) : x(d), y(d1) {}
bool operator==(Point &lhs) const
{
return (x == lhs.x && y == lhs.y);
}
};
class Graham{
private:
Point ** points; //points array that will be sort
Point ** pointsOrg; //points array orginal for result
Point * startPoint;
int countPoints;
public:
int getCountPoints() const {
return countPoints;
}
private:
list<int> indexPointsResult;
public:
Graham() {}
Graham(Point ** pPoint, int i) {
pointsOrg = pPoint;
countPoints = i;
points = new Point*[i];
for(int j = 0;j<i;j++)
{
points[j] = new Point(pPoint[j]->x, pPoint[j]->y);
}
}
void print_graph()
{
Gnuplot gp(R"("C:\Program Files\gnuplot\bin\gnuplot.exe" -persist)");
gp << "$edge << EOD\n";
for(int i = 0; i < indexPointsResult.getSize(); i++) {
gp << toStr(points[indexPointsResult[i]]->x) << " "
<< toStr(points[indexPointsResult[i]]->y) << " ";
if(i != indexPointsResult.getSize()-1) {
gp << toStr(points[indexPointsResult[i + 1]]->x) << " "
<< toStr(points[indexPointsResult[i + 1]]->y) << "\n";
}else
{
gp << toStr(points[indexPointsResult[0]]->x) << " "
<< toStr(points[indexPointsResult[0]]->y) << "\n";
}
}gp << "EOD\n";
gp << "plot '$edge' using 1:2:($3-$1):($4-$2) with vectors lw 2 notitle\n";
gp << "$data << EOD\n";
for(int i = 0; i < countPoints; i++)
gp << toStr(pointsOrg[i]->x) << " " << toStr(pointsOrg[i]->y) << " " << toStr(i) << "\n";
gp << "EOD\n";
gp << "replot '$data' using 1:2:3 with labels point pt 7 offset char 0.6,0.6 title 'ID punktow'\n";
gp << "$dataStart << EOD\n";
gp << toStr(startPoint->x) << " " << toStr(startPoint->y) << "\n";
gp << "EOD\n";
gp << "replot '$dataStart' using 1:2 with points pt 6 ps 7 notitle\n";
}
void setStartPoint() {
int indeksStart = 0;
startPoint = points[indeksStart];
for(int i = 1; i<countPoints;i++)
{
if((startPoint->y > points[i]->y) || (startPoint->y == points[i]->y && startPoint->x > points[i]->x))
{
indeksStart = i;
startPoint = points[indeksStart];
}
}
//zamiana pozycji pierwszego elementu tablicy z startpointem
Point * tmp = points[indeksStart];
points[indeksStart] = points[0];
points[0] = tmp;
}
void sort(bool(*cmp)(Point *, Point *, Point *)) {
HeapSort<Point *>::sort(points,countPoints,cmp,startPoint);
}
Point *& operator[](int index) noexcept(false)
{
if(index < 0 || index >= countPoints)
{
throw std::out_of_range("Podano liczbÄ™ spoza zakresu");
}else{
return points[index];
}
}
void graham_scan(double (*cmp)(Point *, Point *, Point *)) {
indexPointsResult.dodaj_koniec(0);
indexPointsResult.dodaj_koniec(1);
int i0, i1, i2;
for(int i = 2; i<countPoints; i++)
{
indexPointsResult.dodaj_koniec(i);
int sizeCurr = indexPointsResult.getSize();
i0 = indexPointsResult[sizeCurr - 3];
i1 = indexPointsResult[sizeCurr-2];
i2 = indexPointsResult[sizeCurr-1];
while(cmp(points[i0], points[i1], points[i2]) >= 0.0)
{
indexPointsResult.usun_przedostatni();
sizeCurr = indexPointsResult.getSize();
if(sizeCurr == 2) break;
i0 = indexPointsResult[sizeCurr-3];
i1 = indexPointsResult[sizeCurr-2];
i2 = indexPointsResult[sizeCurr-1];
}
}
}
std::string print_data(long long int timeSort, long long int timeLoop) {
std::string res = "";
res += "Ilosc punktow w powloce wypuklej: "+ toStr(indexPointsResult.getSize())+"\n";
res += "Indeksy:\n";
for(int i = 0; i<indexPointsResult.getSize(); i++)
{
for(int j = 0; j<countPoints; j++)
{
if(*points[indexPointsResult[i]] == *pointsOrg[j])
{
res += toStr(j) + " ";
break;
}
}
}
res += "\nCzas obliczen:\nSortowanie: "+toStr(timeSort)+"ms\nPetla: "+toStr(timeLoop)+"us\n";
return res;
}
void clear()
{
for(int i = 0; i<countPoints; i++)
{
delete points[i];
delete pointsOrg[i];
}
delete [] points;
delete [] pointsOrg;
}
};
#endif //GRAHAM_H