Pagini recente » Cod sursa (job #2123169) | Cod sursa (job #1745226) | Cod sursa (job #1566911) | Rating Stefan Tiberiu (StefanAndrei12) | Cod sursa (job #2287848)
///CONVEX HULL
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <iomanip>
struct Coords{
double x;
double y;
};
std::vector<Coords> Points;
inline double cross_product(const Coords& A, const Coords& B, const Coords& C) {
return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}
inline bool cmp(const Coords& p1, const Coords& p2) {
return cross_product(Points[0], p1, p2) < 0;
}
Coords NEXT_TO_TOP(std::stack<Coords> Stack){
Stack.pop();
return Stack.top();
}
int main()
{
std::ifstream in("infasuratoare.in");
std::ofstream out("infasuratoare.out");
//read nummber of points
int N;
in >> N;
//read in a vector of structs all the points
double x, y;
in >> x >> y;
Points.push_back({x, y});
int bottom_most = 0;
for(int i = 1; i < N; ++i){
in >> x >> y;
Points.push_back({x, y});
if(x < Points[bottom_most].x || (x == Points[bottom_most].x && y < Points[bottom_most].y)){
bottom_most = i;
}
}
//move the bottom most element on position 0
std::swap(Points[0], Points[bottom_most]);
//sort Points by the polar angles reported to Points[0]
std::sort(Points.begin() + 1, Points.end(), cmp);
std::stack<Coords> Stack;
Stack.push(Points[0]);
Stack.push(Points[1]);
Stack.push(Points[2]);
for(int i = 3; i < N; ++i){
while(Stack.size() >= 2 && cross_product(NEXT_TO_TOP(Stack), Stack.top(), Points[i]) > 0){
Stack.pop();
}
Stack.push(Points[i]);
}
out << Stack.size() << '\n';
while(!Stack.empty()){
Coords tmp = Stack.top();
out << std::fixed << std::setprecision(9) << tmp.x << ' ' << tmp.y << '\n';
Stack.pop();
}
return 0;
}