计算几何 hihocoder 1183 Integral

转换成求多边形的面积和重心.....

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 2000005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int sgn(double x)
{
	if(fabs(x) < eps) return 0;
	if(x < 0) return -1;
	else return 1;
}

struct Point
{
	double x, y;
	Point() {}
	Point(double _x, double _y) {
		x = _x;
		y = _y;
	}
	bool operator == (Point b) const {
		return sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x;
	}
	bool operator < (Point b) const {
		return sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x;
	}
	Point operator - (const Point& b) const {
		return Point(x - b.x, y - b.y);
	}
	double operator ^ (const Point& b) const{
		return x * b.y - y * b.x;
	}
	double operator * (const Point& b) const {
		return x * b.x + y * b.y;
	}
	Point operator + (const Point& b) const {
		return Point(x + b.x, y + b.y);
	}
	Point operator * (const double& k) const {
		return Point(x * k, y * k);
	}
	Point operator / (const double& k) const {
		return Point(x / k, y / k);
	}
	void input(void)
	{
		scanf("%lf%lf", &x, &y);
	}
};

struct Line
{
	Point s, e;
	Line() {}
	Line(Point _s, Point _e) {
		s = _s;
		e = _e;
	}
};

struct polygon
{
	int n;
	Point p[maxn];
	Line l[maxn];
	void input(int _n)
	{
		n = _n;
		for(int i = 0; i < n; i++)
			p[i].input();
	}
	void getline()
	{
		for(int i = 0; i < n; i++)
			l[i] = Line(p[i], p[(i+1) % n]);
	}
	Point getbarycentre()
	{
		Point ret(0, 0);
		double area = 0;
		for(int i = 1; i < n-1; i++) {
			double tmp = (p[i] - p[0]) ^ (p[i+1] - p[0]);
			area += tmp;
			ret.x += (p[0].x + p[i].x + p[i+1].x) / 3 * tmp;
			ret.y += (p[0].y + p[i].y + p[i+1].y) / 3 * tmp;
		}
		if(sgn(area)) ret = ret / area;
		return ret;
	}
	double getarea()
	{
		double sum = 0;
		for(int i = 0; i < n; i++) {
			sum += (p[i] ^ p[(i+1) % n]);
		}
		return fabs(sum) / 2;
	}
}t;

Point ans;
void work(void)
{
	int n;
	scanf("%d", &n);
	t.input(n);
	t.getline();
	ans = t.getbarycentre();
	double res = (ans.x + ans.y) * t.getarea();
	printf("%.2f\n", res);
}

int main(void)
{
	int _;
	while(scanf("%d", &_)!=EOF) {
		while(_--) {
			work();
		}
	}	

	return 0;
}


你可能感兴趣的:(HDU)