Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
//计算几何
/*
题意:求三角形中晶格点的个数
分析:
用pick定理和GCD()可以求得
关于pick定理:http://translate.google.com/translate?u=http%3A%2F%2Fepiste.math.ntu.edu.tw%2Farticles%2Fsm%2Fsm_25_10_1%2Fpage4.html&hl=zh-CN&ie=UTF8&sl=zh-TW&tl=zh-CN
关键: area=b/2+i-1;
*/
//程序如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct Point
{
int x,y;
}a,b,c;
int Gcd(int a,int b)
{
return b?Gcd(b,a%b):a;
}
int abs(int a)
{
return a>0?a:-a;
}
int area2;
int E;
int main()
{
while(scanf("%d%d%d%d%d%d",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y)!=EOF)
{
if(!a.x&&!a.y&&!b.x&&!b.y&&!c.x&&!c.y)break;
area2=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
area2=abs(area2);
E=Gcd(abs(a.x-b.x),abs(a.y-b.y))+Gcd(abs(b.x-c.x),abs(b.y-c.y))+Gcd(abs(c.x-a.x),abs(c.y-a.y));
printf("%d\n",(area2-E+2)/2);
}
}