POJ2954 Triangle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5869 | Accepted: 2524 |
Description
A lattice point is an ordered pair (x, y) 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 x1, y1, x2, y2, x3, and y3, where (x1, y1), (x2, y2), and (x3, y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1, y1, x2, y2, x3, y3 ≤ 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 1 0 0 5 0 0 5 0 0 0 0 0 0
Sample Output
0 6
Source
给定三个点的坐标,问三个点连成的三角形中,包含了多少整坐标点
数学问题 计算几何? pick定理
pick定理:设平面直角坐标系中,一个三角形内部有a个整坐标点,它的三条边共经过了b个整坐标点,那么三角形面积S=a + b/2 +1
把公式变形一下,用海伦公式或者叉积求出S,就能算出a了
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 int read(){ 9 int x=0,f=1;char ch=getchar(); 10 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 11 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();} 12 return x*f; 13 } 14 struct point{ 15 int x,y; 16 point operator - (point rhs)const{ 17 return (point){x-rhs.x,y-rhs.y}; 18 } 19 int cross(point rhs){return x*rhs.y-y*rhs.x;} 20 }p[4]; 21 inline int abs(int x){return x<0?-x:x;} 22 int gcd(int a,int b){return (!b)?a:gcd(b,a%b);} 23 int area(point a,point b,point c){ 24 return abs((c-a).cross(b-a)); 25 } 26 int side(){ 27 p[0]=p[3];int res=0; 28 for(int i=1;i<=3;i++){ 29 res+=gcd(abs(p[i].x-p[i-1].x),abs(p[i].y-p[i-1].y)); 30 } 31 return res; 32 } 33 int main(){ 34 int i,j; 35 while(scanf("%d%d%d%d%d%d",&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y)!=EOF){ 36 if(!p[1].x && !p[2].x && !p[3].x && !p[1].y && !p[2].y && !p[3].y)break; 37 int ans=area(p[1],p[2],p[3])+2-side(); 38 printf("%d\n",ans/2); 39 } 40 return 0; 41 }