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
题目翻译:
晶格点是一个有序的对 (x, y) ,其中x和y都是整数.给定三角形顶点的坐标(恰好是晶格点),您需要计算完全位于三角形内的晶格点数(三角形边缘或顶点的点不计数)。
输入
输入测试文件将包含多个测试用例。每个输入测试用例由六个整数x1、y 1、x 2、y 2、x 3和y3组成,其中(x1),y 1), (x2, y2) 和 (x3, y3) 是三角形顶点的坐标。输入中的所有三角形都将是非退化的(将具有正面积),并且 #15000 = x1, y1, x2, y2, x 3 , x3,y3 = 15000。文件结尾用x1 = y1 = x2 = y2 = x3 = y3 = 0 的测试用例进行标记,不应被处理。
输出
对于每个输入情况,程序应在一行上打印内部晶格点的数量。
题意理解:
给你三角形三个点的坐标,三角形内部点的个数,需要用到pick定理。
详解可以参考我的这篇博文(凸多边形):https://blog.csdn.net/qq_43472263/article/details/100881808
这个题是上面那个题的特殊情况(三角形)。
#include<iostream>
#include<cmath>
using namespace std;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int Cross(int x1,int y1,int x2,int y2){
return x1*y2-x2*y1;
}
int main(){
int x1,y1,x2,y2,x3,y3;
while(scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF){
if(!x1&&!y1&&!x2&&!y2&&!x3&&!y3) break;
int cnt=gcd(abs(x1-x2),abs(y1-y2));
cnt+=gcd(abs(x1-x3),abs(y1-y3));
cnt+=gcd(abs(x2-x3),abs(y2-y3));
int S=abs(Cross(x1-x2,y1-y2,x1-x3,y1-y3))/2;
printf("%d\n",S-cnt/2+1);
}
return 0;
}