凸包面积一般用极角法。
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 10010;
const double eps = 1e-6;
struct point{ double x,y;};
point p[MAX];
point stk[MAX];
int top;
bool dy(double x,double y) { return x > y + eps;} // x > y
bool xy(double x,double y) { return x < y - eps;} // x < y
bool dyd(double x,double y) { return x > y - eps;} // x >= y
bool xyd(double x,double y) { return x < y + eps;} // x <= y
bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
double disp2p(point a,point b)
{
return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
bool cmp(point a,point b) // 第一次排序
{
if( dd(a.y ,b.y ) )
return xy(a.x, b.x);
return xy(a.y,b.y);
}
bool cmp1(point a,point b) // 第二次排序
{
double len = crossProduct(p[0],a,b);
if( dd(len,0.0) )
return xy(disp2p(p[0],a),disp2p(p[0],b));
return xy(len,0.0);
}
double area_polygon(point p[],int n)
{
double s1 = 0.0,s2 = 0.0;
for(int i=0; i<n; i++)
{
s1 += p[(i+1)%n].y * p[i].x;
s2 += p[(i+1)%n].y * p[(i+2)%n].x;
}
return fabs(s1 - s2)/2.0;
}
double Graham(point p[],int n)
{
sort(p,p+n,cmp);
sort(p+1,p+n,cmp1);
top = 0;
stk[top++] = p[0];
stk[top++] = p[1];
stk[top++] = p[2];
top--;
for(int i=3; i<n; i++)
{
while(1)
{
point a,b;
a = stk[top];
b = stk[top-1];
if( xyd( crossProduct(a,b,p[i]), 0.0 ) )
top--;
else
break;
}
stk[++top] = p[i];
}
return area_polygon(stk,top+1);
}
int main()
{
int n;
while( ~scanf("%d",&n) )
{
for(int i=0; i<n; i++)
scanf("%lf %lf",&p[i].x,&p[i].y);
double s = Graham(p,n);
printf("%.1lf\n",s);
}
return 0;
}