计算几何初步——面积公式,GCD,PICK公式

这题主要用到PICK公式即:

area = I + E/2 - 1.(I为多边形内的格点数,E为边上的格点数,area为多边形面积)

用GCD可以算出一条边上的格点-1个,那么3×GCD即为3条边上的所有格点数(因为每个条边的顶点多算了一次)。。。

View Code
 1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4
5 struct point {
6 int x, y;
7 };
8
9 int pick(int area, int num)
10 {
11 return (area - num)/2 + 1;
12 }
13
14 int cross(point &p, point &q)
15 {
16 return q.y*p.x - p.y*q.x;
17 }
18
19 int gcd(int x, int y)
20 {
21 if (x == 0 || y == 0)return max(x,y);
22 return gcd(y,x%y);
23 }
24
25 int main()
26 {
27 point one, two, three;
28 int x1, x2, x3, y1, y2, y3;
29 while (scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3) != EOF) {
30 if (x1 == 0 && x2 == 0 && x3 == 0 && y1 == 0 && y2 == 0 && y3 == 0)break;
31 one.x = x1;one.y = y1;
32 two.x = x2;two.y = y2;
33 three.x = x3;three.y = y3;
34 int area = (cross(one,two) + cross(two,three) + cross(three,one));
35 if (area < 0)area = -area;
36 int num = gcd(abs(one.x - two.x),abs(one.y - two.y));
37 num += gcd(abs(two.x - three.x),abs(two.y - three.y));
38 num += gcd(abs(three.x - one.x),abs(three.y - one.y));
39 cout<<pick(area,num)<<endl;
40 }
41 return 0;
42 }



posted on 2012-01-17 16:43  Dev-T  阅读(679)  评论(0编辑  收藏  举报