求多边形面积,选定(0,0)点,用三角形法求,求三角形面积时用叉积,把式子展开并合并同类项可得模板中的公式。
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include
<
iostream
>
#include < cstdio >
#include < cstdlib >
#include < cstring >
using namespace std;
#define maxn 101
struct XPoint
{
double x, y;
}point[maxn];
int n;
void input()
{
for ( int i = 0 ; i < n; i ++ )
{
scanf( " %lf%lf " , & point[i].x, & point[i].y);
}
}
double areaofp( int vcount, XPoint plg[])
{
int i;
double s;
if (vcount < 3 ) return 0 ;
s = plg[ 0 ].y * (plg[vcount - 1 ].x - plg[ 1 ].x);
for (i = 1 ;i < vcount;i ++ )
s += plg[i].y * (plg[(i - 1 )].x - plg[(i + 1 ) % vcount].x);
return s / 2 ;
}
int main()
{
// freopen("D:\\t.txt", "r", stdin);
while (scanf( " %d " , & n) != EOF && n != 0 )
{
input();
printf( " %.1f\n " , areaofp(n, point));
}
return 0 ;
}
#include < cstdio >
#include < cstdlib >
#include < cstring >
using namespace std;
#define maxn 101
struct XPoint
{
double x, y;
}point[maxn];
int n;
void input()
{
for ( int i = 0 ; i < n; i ++ )
{
scanf( " %lf%lf " , & point[i].x, & point[i].y);
}
}
double areaofp( int vcount, XPoint plg[])
{
int i;
double s;
if (vcount < 3 ) return 0 ;
s = plg[ 0 ].y * (plg[vcount - 1 ].x - plg[ 1 ].x);
for (i = 1 ;i < vcount;i ++ )
s += plg[i].y * (plg[(i - 1 )].x - plg[(i + 1 ) % vcount].x);
return s / 2 ;
}
int main()
{
// freopen("D:\\t.txt", "r", stdin);
while (scanf( " %d " , & n) != EOF && n != 0 )
{
input();
printf( " %.1f\n " , areaofp(n, point));
}
return 0 ;
}