poj2954——pick定理+点在三角形上的个数

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

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‎‎都是整数.给定三角形顶点的坐标(恰好是晶格点),您需要计算完全位于三角形内的晶格点数(三角形边缘或顶点的点不计数)。‎

‎输入‎

‎输入测试文件将包含多个测试用例。每个输入测试用例由六个整数‎‎x‎‎1、y‎‎ ‎‎1、x‎‎ ‎‎2、y‎‎ ‎‎2、x‎‎ ‎‎3‎‎和‎‎y‎‎3‎‎组成,其中‎‎(x‎‎1)‎‎,y ‎‎1‎‎), (‎‎x‎‎2,‎‎ ‎‎y‎‎2‎‎) 和 (‎‎x‎‎3,‎‎ ‎‎y‎‎3‎‎) 是三角形顶点的坐标。输入中的所有三角形都将是非退化的(将具有正面积),并且 #15000 = ‎‎x‎‎1‎‎, ‎‎y‎‎1‎‎, ‎‎x‎‎2‎‎, ‎‎y‎‎2‎‎, x 3 , ‎‎x‎‎3,‎‎y‎‎3‎‎ = 15000。文件结尾用‎‎x‎‎1‎‎ = ‎‎y‎‎1‎‎ = ‎‎x‎‎2‎‎ = ‎‎y‎‎2‎‎ = ‎‎x‎‎3‎‎ = ‎‎y‎‎3‎‎ = 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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值