首页 > 代码库 > poj 2954 Triangle(Pick定理)

poj 2954 Triangle(Pick定理)

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

Triangle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5043 Accepted: 2164

Description

lattice point is an ordered pair (xy) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lattice points), you are to count the number of lattice points which lie completely inside of the triangle (points on the edges or vertices of the triangle do not count).

Input

The input test file will contain multiple test cases. Each input test case consists of six integers x1y1x2y2x3, and y3, where (x1y1), (x2y2), and (x3y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1y1x2y2x3y3 ≤ 15000. The end-of-file is marked by a test case with x1 =  y1 =x2 = y2 = x3 = y3 = 0 and should not be processed.

Output

For each input case, the program should print the number of internal lattice points on a single line.

Sample Input

0 0 1 0 0 10 0 5 0 0 50 0 0 0 0 0

Sample Output

06

 

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

继续用pick定理,area=i + b / 2 -1

注意判结束时不可(a+b+c+d)看是否为零,因为有负数

 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7  8 using namespace std; 9 10 typedef struct11 {12     double x,y;13 }point;14 15 double crossProduct(point a,point b,point c)16 {17     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);18 }19 20 int gcd(int a,int b)21 {22     return b ? gcd(b,a%b) : a;23 }24 25 point p[4];26 27 int onEdge(int n)28 {29     int sum=0;30     p[n]=p[0];31     for(int i=0; i<n; i++)32     {33         sum+=gcd(abs((int)(p[i].x-p[i+1].x)),abs((int)(p[i].y-p[i+1].y)));34     }35     return sum;36 }37 38 int main()39 {40     while(scanf("%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y)!=EOF41           && p[0].x!=0||p[0].y!=0||p[1].x!=0||p[1].y!=0||p[2].x!=0||p[2].y!=0)42     {43         double area=fabs(crossProduct(p[0],p[1],p[2]))/2.0;44         int edge=onEdge(3);45         printf("%d\n",(int)area+1-edge/2);46     }47     return 0;48 }
View Code

 

poj 2954 Triangle(Pick定理)