poj2954

本文介绍了一个基于Pick公式的简单多边形面积计算方法,通过确定边界上的格点数量(on)和多边形内部的格点数量(in),可以计算出多边形的面积(area)。文章还提供了一段C++代码实现,该实现利用了最大公约数(gcd)来辅助计算边界上的点数。

Pick公式:平面上以格子点为顶点的简单多边形,如果边上的点数为on,内部的点数为in,则它的面积为area=on/2+in-1
利用gcd求每个边上的点数。

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

struct Point
{
int x, y;
}point[3];

void input()
{
for (int i = 0; i < 3; i++)
scanf("%d%d", &point[i].x, &point[i].y);
int ret = 0;
for (int i = 0; i < 3; i++)
ret |= point[i].x | point[i].y;
if (!ret)
exit(0);
}

int xmult(Point a, Point b, Point c)
{
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}

int gcd(int x, int y)
{
if (!x || !y)
return x > y ? x : y;
for (int t; t = x % y; x = y, y = t);
return y;
}

void work()
{
int area = abs(xmult(point[0], point[1], point[2]));
int on = 0;
for (int i = 0; i < 3; i++)
on += gcd(abs(point[i % 3].x - point[(i + 1)%3].x), abs(point[i % 3].y - point[(i + 1)%3].y));
int in = (area - on) / 2 + 1;
printf("%d\n", in);
}

int main()
{
//freopen("t.txt", "r", stdin);
while (1)
{
input();
work();
}
return 0;
}

转载于:https://www.cnblogs.com/rainydays/archive/2011/10/08/2202118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值