平面有n个点 求最大边形的面积
It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide.
枚举对角线, 求对角线两边最大三角形面积,最后相加。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
struct Point{
double x,y;
}p[500];
struct edge{
Point x,y;
}e;
double xmulti(Point p1,Point p2,Point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
int side(Point p1,edge e){
return xmulti(e.x,p1,e.y)>1e-6;
}
double area(Point a,Point b,Point c){
return fabs(xmulti(a,b,c)/2);
}
int main(){
int n,i,j,k;
double s1,s2,s;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
s=0.0;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
s1=0.0;s2=0.0;
e.x=p[i];e.y=p[j];
for(k=0;k<n;k++){
if(side(p[k],e)>0)s1=max(s1,area(p[i],p[j],p[k]));
else s2=max(s2,area(p[i],p[j],p[k]));
}
if(s1>1e-6&&s2>1e-6)s=max(s,s1+s2);
}
}
printf("%lf\n",s);
}
return 0;
}