ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 2954 Triangle [pick定理,叉积,计算几何]

2021-09-27 14:35:51  阅读:177  来源: 互联网

标签:&& Triangle gcd int abs POJ 叉积 include define


题目

Triangle

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7044Accepted: 2966

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定理,这个定理不只用于三角形,多边形都可以

简单解释,即S=a+b/2-1,S是三角形面积,a是内部的格点,b是边上的格点

S可以通过叉积直接求,边上的格点数量可以通过gcd(|x|,|y|)求出来

为什么不用模板呢,因为模板上的double类型的,改来改去就是不对

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define trav(a, x)  for(auto& a : x)
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
//#define _           0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;


using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pi    = acos(-1.0);
const double eps   = 1e-8;
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;
const int    maxn  = 200009;
const ll     N     = 5;

const double inf=1e20;
const int maxp=1010;

//判断正负
int sgn(double x) {
	if (fabs(x)<eps) return 0;
	if (x<0) return -1;
	else return 1;
}
//平方
inline double sqr(double x) {
	return x*x;
}




inline int gcd(int a,int b) {
    return b>0 ? gcd(b,a%b):a;
}



void solve(){
	int a, b, c, d, e, f;
	while(cin>>a>>b>>c>>d>>e>>f) {
		if (a==b&&b==c&&c==d&&d==e&&e==f&&f==0) break;
		int ea = abs((c-a)*(f-b) - (d-b)*(e-a));
		int q = gcd(abs(c-a),abs(b-d)) + gcd(abs(c-e),abs(d-f)) + gcd(abs(e-a), abs(f-b));
		cout<<(ea-q+2)/2<<endl;
	}
}


int main()
{
//	TIE;
    #ifndef ONLINE_JUDGE
//    freopen ("in.txt" , "r", stdin );
//    freopen ("out.txt", "w", stdout);
    #else
    #endif
	solve();
//    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
//	return ~~(0^_^0);
}

参考:https://blog.csdn.net/clove_unique/article/details/53982212

标签:&&,Triangle,gcd,int,abs,POJ,叉积,include,define
来源: https://blog.csdn.net/Dueser/article/details/120507269

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有