给你三角形三个顶点的坐标,
让你求三角形内部的点数
pick定理 三角形的面积 = 三条边上的点数/2 + 内部的点数 - 1
一个pick定理模板,边上的点用GCD算。
三角形面积叉积的一半。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct pnode
{
int x,y;
pnode(int xx=0,int yy=0):x(xx),y(yy){}
pnode operator - (const pnode &b)const{
return pnode(x-b.x,y-b.y);
}
int operator ^(const pnode &b)const{
return x*b.y-y*b.x;
}
int pscanf(){
return scanf("%d %d",&x,&y);
}
bool check(){
if( x==0 && y==0)
return true;
return false;
}
}a,b,c;
int Gcd( int a , int b )
{
return b==0 ? a : Gcd( b , a%b );
}
int Area( pnode p0 ,pnode p1, pnode p2 )
{
return abs( (p1-p0)^(p2-p0) ) / 2 ;
}
int Edge( pnode a ,pnode b, pnode c )
{
int E = 0;
E += Gcd( abs(a.x - b.x) , abs( a.y - b.y ) );
E += Gcd( abs(a.x - c.x) , abs( a.y - c.y ) );
E += Gcd( abs(b.x - c.x) , abs( b.y - c.y ) );
return E;
}
int main( )
{
while( a.pscanf() && b.pscanf() && c.pscanf() )
{
if( a.check() && b.check() && c.check() )break;
int area = Area( a , b ,c );
int edge = Edge( a , b ,c );
printf( "%d\n",area + 1 - edge/2);
}
return 0;
}