Pagini recente » Cod sursa (job #2918856) | Cod sursa (job #777190) | Cod sursa (job #163047) | Cod sursa (job #484736) | Cod sursa (job #1879658)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Point {
double x, y;
};
vector<Point> v, h;
double det(Point a, Point b, Point c) {
return a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y);
}
bool comparePoints(Point a, Point b) {
return (a.x<b.x)||(a.x==b.x)&&(a.y<b.y);
}
bool compareAngles(Point a, Point b) {
return det(v[0], a, b) < 0;
}
void convexHull() {
swap(v[0], *min_element(v.begin(), v.end(), comparePoints));
sort(v.begin()+1, v.end(), compareAngles);
h.push_back(v[0]);
h.push_back(v[1]);
for (int i = 2; i < v.size(); i++) {
while (h.size()>1 && det(*(h.end()-2), *(h.end()-1), v[i])>0) {
h.erase(h.end()-1);
}
h.push_back(v[i]);
}
reverse(h.begin(), h.end());
}
void read() {
ifstream cin("infasuratoare.in");
int n;
cin>>n;
v.resize(n);
for (int i = 0; i < n; i++) {
cin>>v[i].x>>v[i].y;
}
}
void write() {
ofstream cout("infasuratoare.out");
cout<<fixed;
cout<<h.size()<<"\n";
for (int i = 0; i<h.size(); i++) {
cout<<setprecision(9)<<h[i].x<<" "<<h[i].y<<"\n";
}
}
int main() {
read();
convexHull();
write();
return 0;
}