POJ2954-Triangle

Triangle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4963 Accepted: 2127

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 x1y1 = 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
//AC代码
/*
题意:在一个平面直角坐标系中,给你一个三角形的三个顶点,问你这个三角形内包含多少个点(除了边上的点和端点)
此题用GCD求出三角形边上多有点,再求出这个三角形的面积最后运用皮克定理(Pick)Pick定理 面积 = 内部点数 + 边上点数 / 2 - 1
即可求出三角形内的所有点
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
const double eps=1e-8;
const double PI=3.1415926;
const int Max=1001;
#define zero(x) (((x)>0?(x):-(x))<eps)
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
typedef struct Node
{
    double x;
    double y;
}point;
point list[Max],stack[Max];
int n;
int top;
double xmult(point p0,point p1,point p2)
{
	return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double Distance(point p1,point p2)// 返回两点之间欧氏距离
{
	return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
}
bool cmp(point p1,point p2)
{
    double temp;
    temp=xmult(list[0],p1,p2);
    if(temp>0)
        return true;
    if(temp==0&&(Distance(list[0],p1)<Distance(list[0],p2)))
        return true;
    return false;
}
void convex_hull()//凸包模板
{
    int i;
    for(i=1;i<n;i++)
    {
        point temp;
        if((list[i].y<list[0].y)||(list[i].y==list[0].y&&list[i].x<list[0].x))
            swap(list[0],list[i]);
    }
    sort(list+1,list+n,cmp);
    top=1;
    stack[0]=list[0];
    stack[1]=list[1];
    for(i=2;i<n;i++)
    {
        while(top>=1&&xmult(stack[top-1],stack[top],list[i])<=0)
            top--;
        top++;
        stack[top]=list[i];
    }
}
int GCD(int x,int y)
{
    return y?GCD(y,x%y):x;
}
int main()
{
    int m,i,j;
    int x1,y1,x2,y2,x3,y3;
    int ax,ay,bx,by,cx,cy;
    int node_in,node_on,A;
    while(cin>>x1>>y1>>x2>>y2>>x3>>y3&&(x1||x2||x3||y1||y2||y3))
    {
        node_in=0;
        node_on=0;
        A=0;
        ax=x2-x1;
        ay=y2-y1;
        bx=x3-x2;
        by=y3-y2;
        cx=x1-x3;
        cy=y1-y3;
        node_on+=GCD(abs(ax),abs(ay));//求出三角形边上的点
        node_on+=GCD(abs(bx),abs(by));
        node_on+=GCD(abs(cx),abs(cy));
        A+=(x2-x1)*(y3-y1)-(y2-y1)*(x3-x1);//求出三角形的面积
        if(A<0)//有可能面积是顺时针求得是负值
            A=-A;
        A/=2;
        //cout<<A<<" "<<node_on<<endl;
        cout<<(A+1)-node_on/2<<endl;//皮克定理
    }
    return 0;
}
/*
0 0 5 0 0 5
1 1 -2 3 4 -7
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值