poj1265 求多边形内部的点和边界上的点

pick公式:给定顶点坐标均是整点的简单多边形,面积=内部点+边上点/2+1

对于一个多边形,由于要左闭右开的线段避免重复计算边上点

每条边上的个点数为gcd(x2-x1,y2-y1)

#include<cstdio>
#include<cmath>
#define maxl 100010
#define eps 1e-8

inline int sgn(double x)
{
	if(x>-eps && x<eps) return 0;
	if(x>0) return 1;
	else	return -1;
}
struct point
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator + (const point &b)const
	{
		return point(x+b.x,y+b.y);
	}
	point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	friend point operator *(const point &b,const double t)
	{
		return point(b.x*t,b.y*t);
	}
	friend point operator *(const double t,const point &b)
	{
		return point(t*b.x,t*b.y);
	}
	friend point operator /(const point &b,const double t)
	{
		return point(b.x/t,b.y/t);
	}
	inline double norm()
	{
		return sqrt(x*x+y*y); 
	} 
};
inline double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}

inline int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
}
inline int abs(int x){return x<0?-x:x;}

struct polygon
{
	int n;
	point a[maxl];
	polygon(){}
	inline double perimeter()
	{
		double sum=0;
		a[n+1]=a[1];
		for(int i=1;i<=n;i++)
			sum+=(a[i+1]-a[i]).norm();
		return sum;
	}
	inline double area()
	{
		double sum=0;
		a[n+1]=a[1];
		for(int i=1;i<=n;i++)
			sum+=det(a[i+1],a[i]);
		return fabs(sum/2);
	}
	inline int border_point_num()
	{
		int num=0;
		a[n+1]=a[1];
		for(int i=1;i<=n;i++)
			num+=gcd(abs(int(a[i+1].x-a[i].x)),abs(int(a[i+1].y-a[i].y)));
		return num;
	} 
	inline int inside_point_num()
	{
		return int(area())+1-border_point_num()/2;
	}
}d;

int n,cas;
int ans1,ans2;

inline void prework()
{
	scanf("%d",&d.n);
	int x,y;d.a[0]=point(0,0);
	for(int i=1;i<=d.n;i++)
	{
		scanf("%d%d",&x,&y);
		d.a[i]=point(d.a[i-1].x+x,d.a[i-1].y+y);
	}
}

inline void mainwork()
{
	ans1=d.inside_point_num();
	ans2=d.border_point_num();
}

inline void print()
{
	printf("Scenario #%d:\n",cas);
	printf("%d %d %.1f\n\n",ans1,ans2,d.area());
}

int main()
{
	int t;
	scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值