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
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include "ChainedList.h"
#include "Pont.h"
#include "Node.h"
using namespace std;
//template <class T>
//void GrahamScan(Pont array[], ChainedList<T> &L, int n);
// 0 : a pontok egy vonalon vannak ; 0 < : balra fordul ; 0 > : jobbra fordul
double Whatdirection(Pont p1, Pont p2, Pont p3)
{
return (p2.x-p1.x) * (p3.y-p1.y) - (p2.y-p1.y) * (p3.x-p1.x);
}
void GetTangents(Pont p1 , Pont p2 , Pont Pivot, double &p1tangent, double &p2tangent)
{
if (p1.y - Pivot.y < 0 )
p1tangent = -1 * (p1.y - Pivot.y);
else
p1tangent = (p1.y - Pivot.y);
if (p1.x - Pivot.x < 0 )
p1tangent /= -1 * (p1.x - Pivot.x);
else
p1tangent /= (p1.x - Pivot.x);
if (p2.y - Pivot.y < 0 )
p2tangent = -1 * (p2.y - Pivot.y);
else
p2tangent = (p2.y - Pivot.y);
if (p2.x - Pivot.x < 0 )
p2tangent /= -1 * (p2.x - Pivot.x);
else
p2tangent /= (p2.x - Pivot.x);
}
bool Smaller (Pont p1 , Pont p2 , Pont Pivot)
{
cout << Pivot.x << '\n';
double p1tangent,p2tangent;
//Speciális eset kategória #1: amikor az x koordináta megegyezik (90 fokot zár be az x tengellyel)
if (Pivot.x == p2.x) // A p2 és a Pivot vonala 90 fokot zár be az x tengellyel
{
if (Pivot.x == p1.x) //mindkét ponttal 90 fokot zár be, tehát egyik sem "kisebb"
return 0;
else
return 1; //a p1 valóban "kisebb" p2-nél
}
if (Pivot.x == p1.x)
return 0;
// Normális eset(ek):
GetTangents(p1 , p2 , Pivot, p1tangent, p2tangent);
return p1tangent < p2tangent;
}
bool Greater (Pont p1 , Pont p2 , Pont Pivot)
{
double p1tangent,p2tangent;
//Speciális eset kategória #1: amikor az x koordináta megegyezik (90 fokot zár be az x tengellyel)
if (Pivot.x == p1.x) // A p1 és a Pivot vonala 90 fokot zár be az x tengellyel
{
if (Pivot.x == p2.x) //mindkét ponttal 90 fokot zár be, tehát egyik sem "kisebb"
return 0;
else
return 1; //a p2 valóban "kisebb" p2-nél
}
if (Pivot.x == p2.x)
return 0;
// Normális eset(ek):
GetTangents(p1 , p2 , Pivot, p1tangent, p2tangent);
return p1tangent > p2tangent;
}
void QuickSortPoints(Pont array[],int lower_border,int upper_border, char c) //az algoritmus növekvő, vagy csökkenő rendbe rendez a megkapott karaktertől függően (i = increase ; d = decrease)
{
int i = lower_border,j = upper_border,n = (lower_border + upper_border) / 2;
Pont x,helper;
if (c == 'i')
{
if (upper_border <= 1) //nincs mit rendezni
return;
}
else
if (c == 'd')
{
if (lower_border-1 == upper_border) //nincs mit rendezni
return;
}
x = array[n];
do{
if (c == 'i')
{
//Ebben keletkezik a szegmens hiba
while (Smaller (array[i] , x , array[0]) && i < j) i++;
//Ebben keletkezik a szegmens hiba
while (Greater (array[j] , x , array[0]) && i < j) j--;
}
else
if (c == 'd')
{
while (Greater (array[i] , x , array[0]) && i < j) i++;
while (Smaller (array[j] , x , array[0]) && i < j) j--;
}
if (i <= j)
{
helper = array[i];
array[i] = array[j];
array[j] = helper;
i++;
j--;
}
}while (i <= j);
if (lower_border < j)
QuickSortPoints(array,lower_border,j, c);
if (i < upper_border)
QuickSortPoints(array,i,upper_border, c);
}
void MaxSearch(Pont array[], int n)
{
Pont csere;
for (int i=1 ; i < n ; i++)
{
if ( (array[i].y > array[0].y) || (array[i].y == array[0].y && array[i].x < array[0].x) )
{
csere = array[i];
array[i] = array[0];
array[0] = csere;
}
}
}
template <class T>
void GrahamScan(Pont array[], ChainedList<T> &L, int n)
{
if (n <= 1) //ha egy vagy nulla pont van...
{
L.insertAfter(array[0]);
return;
}
// I. A leg alacsonyabban lévő pont kerül előre
MaxSearch(array, n);
int j,k;
Pont csere;
//II. Előre rendezzük a P-től jobbra lévő pontokat (és így persze a tőle balra levők hátra kerülnek) Ez buborékrendezéshez hasonló algoritmus
k = 0; //A k vált. mindig a legutóbbi olyan pont indexe, amit nem kell cserélni
for ( int i = 1; i < n ;i++ )
{
j = 0;
if ( array[i].x < array[0].x )
{
j = i+1;
while (j < n)
{
if ( array[j].x >= array[0].x )
{
csere = array[j];
array[j] = array[i];
array[i] = csere;
break;
}
j++;
}
}
else
k = i;
if (j == n)
{
k = i-1;
break;
}
else
k = i;
}
// III. és IV. rész: rendezés a P és adott pont által meghatározott vonal X tengellyel bezárt szöge szerint
QuickSortPoints(array,1,k, 'i');
QuickSortPoints(array,k+1,n-1, 'd');
//Itt kezdődik az igazi Graham scan
//double direction;
Pont p1 = array[0],p2 = array[1],p3;
L.insertAfter(p1);
L.insertAfter(p2);
for (int i = 2 ; i < n ; i++ )
{
p3 = array[i];
L.insertAfter(p3);
while (Whatdirection(p1, p2, p3) <= 0)
{
L.removeAfter();
L.insertAfter(p3);
i++;
p3 = array[i];
}
p1 = p2;
p2 = p3;
}
}
int main(int argc, char *argv[])
{
int n;
cin >> n;
Pont array[n];
for (int i = 0;i < n;i++)
{
cin >> array[i].x;
cin >> array[i].y;
}
ChainedList<Pont> L;
GrahamScan(array,L,n);
while (L.notEmpty)
{
cout << "( " << L.Next().x << " ; " << L.Next().y << " )" << '\n';
L.removeAfter();
std::cout << L.notEmpty << '\n';
}
}
|