https://code.csdn.net/snippets/575284.git
// 凸包.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100
struct Point
{
double x, y;
}P[N], S[N];
int n, top;
double Dis(const Point& p1, const Point& p2)
{
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)*1.0);
}
double Cross(const Point& p1, const Point& p2, const Point& p3, const Point& p4)
{
return (p2.x - p1.x)*(p4.y - p3.y) - (p2.y - p1.y)*(p4.x - p3.x);
}
bool cmp(const Point& p1, const Point& p2)
{
double C = Cross(P[0], p1, P[0], p2);
return C ? C>0 : Dis(P[0], p1)<Dis(P[0], p2);
}
void Pole_Point()
{
int id;
Point pp = P[id = 0];
for (int i = 1; i<n; i++)
if (P[i].y < pp.y || P[i].y == pp.y && P[i].x < pp.x)
pp = P[i], id = i;
P[id] = P[0], P[0] = pp;
}
void Convex_Hull()
{
Pole_Point();
sort(P + 1, P + n, cmp);
S[0] = P[0], S[top = 1] = P[1];
for (int i = 2; i<n; i++)
{
while (top && Cross(S[top - 1], S[top], S[top], P[i]) <= 0)
--top;
S[++top] = P[i];
}
}
int main()
{
while (~scanf_s("%d", &n))
{
for (int i = 0; i<n; i++)
scanf_s("%lf %lf", &P[i].x, &P[i].y);
Convex_Hull();
printf("输出为:\n");
for (int i = 0; i <= top; i++)
printf("%.6f %.6f\n", S[i].x, S[i].y);
}
return 0;
}