poj 1265 Area

本文介绍了一种计算由网格点构成的多边形内部网格点数量、边界上的网格点数量及多边形面积的方法。通过使用叉积计算面积、最大公约数计算边界上的网格点数,并结合Pick定理来确定内部网格点数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        网格点围成一个多边形,顶点都在格点上,求多边形内部格点数,边上格点数和面积。

        运用叉积求有向面积加起来得到面积,最大公约数得到边上格点数,再根据pick定理(S=a+b/2-1)计算内部格点数。注意输出最好用"%f"不要用"%lf"

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stack>

using namespace std;

const int mod=1e9+7;

#define type double
#define Vector Point
const double PI = 3.14159265358979323;

int Abs(int x){
	if(x<0)return -x;
	return x;
}

int gcd(int a,int b){
	if(b==0)return a;
	return gcd(b,a%b);
}

struct Point{
	type x,y;
	Point(){
	}
	Point(int x,int y):x(x),y(y){
	}
	Point operator-(Point other){
		return Point(x-other.x,y-other.y);
	}
};

//向量点积 
type Dot(Vector a,Vector b){
	return a.x*b.x + a.y*b.y;
}

//向量叉积 
type Cross(Vector a,Vector b){
	return a.x*b.y - a.y*b.x;
}

//向量长度 
double Length(Vector a){
	return sqrt(a.x*a.x+a.y*a.y+0.0);
}

//两向量夹角 
type Angle(Vector a,Vector b){
	return acos(Dot(a,b) / (Length(a)*Length(b))+0.0);
}

type Area(Point* pts,int sz){
	type re=0;
	for(int i=0;i<sz;i++){
		re+=Cross(pts[i+1],pts[i]);
		//cout<<"re="<<re<<endl;
	}
	re/=2.0;
	if(re<0)re=-re;
	return re;
}

Point pts[1010];

bool cmp(const Point& a,const Point& b){
	if(a.x==b.x)return a.y<b.y;
	return a.x<b.x;
}


int main(){
	int t;
	cin>>t;
	int cas=0;
	while(t--){
		cas++;
		int n;	cin>>n;
		pts[0].x=pts[0].y=0;
		int onEdge=0;
		for(int i=1;i<=n;i++){
			int dx,dy;
			cin>>dx>>dy;
			onEdge+=gcd(Abs(dx),Abs(dy));
			pts[i].x=pts[i-1].x+dx;
			pts[i].y=pts[i-1].y+dy;
		}
		pts[n+1].x=pts[n+1].y=0;
		
		double ans = Area(pts,n);
		int inside=ans+1-onEdge/2;
		
		printf("Scenario #%d:\n",cas);
		printf("%d %d %.1f\n\n",inside,onEdge,ans);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值