您的位置:首页 > 其它

pku 2954 Triangle && pku 1265 Area Pick定理的应用 + 叉积求多边形面积

2012-05-21 20:31 597 查看
Pick定理证明:http://translate.google.com/translate?u=http://episte.math.ntu.edu.tw/articles/sm/sm_25_10_1/page4.html&hl=zh-CN&ie=UTF8&sl=zh-TW&tl=zh-CN

http://poj.org/problem?id=2954

这个题意就是求网格内给定三角形三点,然后求他的内部点的个数; pick :s = in + on/2 -1 in内部点数 on外部边上的点数 s 多边形面积这里要求的是in,通常是先求出多边形面积,以及on的值,然后求in。这里on的值得求法是 求整数点(x1,y1),(x2,y2)之间的整数个数 = gcd(|x1-x2|,|y1-y2|);

面积的求法:任意多边形面积(凸多边形+凹凸变形,不包括有两边交叉的多边形)=叉积之和的一半

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#define maxn 4
using namespace std;

struct node
{
int x,y;
}p[maxn];
int m,in,on;
double s;
int gcd(int x,int y)
{
while (y != 0)
{
int temp = y;
y = x%y;
x = temp;
}
return x;
}
int main()
{
int i;
while (~scanf("%d%d%d%d%d%d",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y))
{
if (!p[0].x && !p[0].y && !p[1].x && !p[1].y && !p[2].x && !p[2].y) break;
in = on = s = 0;
for (i = 0; i < 3; ++i)
{
on += gcd(abs(p[(i + 1)%3].x - p[i].x),abs(p[(i + 1)%3].y- p[i].y));//任意两点之间的dx,dy
s += p[(i + 1)%3].y*p[i].x - p[i].y*p[(i + 1)%3].x;//任意两点的叉积
}
s = s*0.5;
if (s < 0) s = -s;//叉积求出来的可能为负,注意处理
in = s + 1 - on/2.0;
printf("%d\n",in);
}
return 0;
}


  http://poj.org/problem?id=1265

同上题,只不过这里给定的直接就是dx,dy了。其默认的起点是(0,0)终止的节点也是(0,0)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#define maxn 107
using namespace std;

struct node
{
int x,y;
}p[maxn];
int m,in,on;
double s;
int gcd(int x,int y)
{
while (y != 0)
{
int temp = y;
y = x%y;
x = temp;
}
return x;
}
int main()
{
int t,i;
int dx,dy;
int cas = 1;
scanf("%d",&t);
while (t--)
{
scanf("%d",&m);
p[0].x = p[0].y = 0;
s = in = on = 0;
for (i = 1; i <= m; ++i)
{
scanf("%d%d",&dx,&dy);
on += gcd(abs(dx),abs(dy));
p[i].x = p[i - 1].x + dx;
p[i].y = p[i - 1].y + dy;
s += p[i].y*p[i - 1].x - p[i - 1].y*p[i].x;
}
s = s*0.5;
if (s < 0) s = -s;
in = s + 1 - on/2.0;
printf("Scenario #%d:\n",cas++);
printf("%d %d %.1lf\n\n",in,on,s);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: