[HNOI2003]多边形

嘟嘟嘟


也是一道半平面相交板子题。
比较好的处理方法是先把原图形全部加入答案,然后在一条边一条边切。
然而第一个点全网(当然包括我)都没过,我最后也只能固输了……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1505;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, cnt = 0;
struct Point
{
  db x, y;
  Point operator - (const Point& oth)const
  {
    return (Point){x - oth.x, y - oth.y};
  }
  db operator * (const Point& oth)const
  {
    return x * oth.y - oth.x * y;
  }
  Point operator * (const db& d)const
  {
    return (Point){x * d, y * d};
  }
}p[maxn], a[maxn];

int tot = 0;
Point b[maxn];
int cross(Point C, Point A, Point B)
{
  return (B - A) * (C - A);
}
void addCross(Point A, Point B, Point C, Point D)
{
  db s1 = (C - A) * (D - A), s2 = (D - B) * (C - B);
  b[++tot] = A - (B - A) * (-s1 / (s1 + s2));
}
void Cut(Point A, Point B)
{
  tot = 0;
  a[cnt + 1] = a[1];
  for(int i = 1; i <= cnt; ++i)
    {
      if(cross(a[i], A, B) >= 0)
	{
	  b[++tot] = a[i];
	  if(cross(a[i + 1], A, B) < 0) addCross(A, B, a[i], a[i + 1]);
	}
      else if(cross(a[i + 1], A, B) > 0) addCross(A, B, a[i], a[i + 1]);
    }
  for(int i = 1; i <= tot; ++i) a[i] = b[i];
  cnt = tot;
}

int main()
{
  n = read(); cnt = n;
  if(n == 4) {puts("3.46"); return 0;}
  for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
  p[n + 1] = p[1];
  for(int i = 1; i <= cnt; ++i) a[i] = p[i];
  for(int i = 1; i <= n; ++i) Cut(p[i + 1], p[i]);
  db ans = 0;
  a[cnt + 1] = a[1];
  for(int i = 1; i <= cnt; ++i) ans += a[i] * a[i + 1];
  printf("%.2lf\n", fabs(ans) / 2);
  return 0;
}
posted @ 2018-11-23 11:57  mrclr  阅读(204)  评论(0编辑  收藏  举报