多边形面积
改革春风吹满地
#include<bits/stdc++.h>
using namespace std;
struct point
{
double x,y;
}node[10001];
double cosr(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
int main( )
{
int n;
while(cin>>n&&n)
{
double sum=0;
for(int i=0;i<n;i++)
{
cin>>node[i].x>>node[i].y;
}
for(int i=1;i<n-1;i++)
{
sum+=cosr(node[i],node[i+1],node[0]);
}
printf("%.1lf
",sum/2.0);
}
return 0;
}
多边形重心
#include<bits/stdc++.h>
using namespace std;
struct tree
{
double x,y;
} node[1000010];
double sum(tree a,tree b,tree c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int main( )
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
double res=0;
double ans=0;
tree p;
p.x=0;
p.y=0;
for(int i=0; i<n; i++)
{
cin>>node[i].x>>node[i].y;
}
for(int i=1; i<n-1; i++)
{
ans=sum(node[i],node[i+1],node[0]);
res+=ans;
p.x+=ans*(node[i].x+node[i+1].x+node[0].x)*1.0/3;
p.y+=ans*(node[i].y+node[i+1].y+node[0].y)*1.0/3;
}
p.x/=res*1.0;
p.y/=res*1.0;
printf("%.2lf %.2lf
",p.x,p.y);
}
return 0;
}