poj 1279 Art Gallery

Description

The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily convex). When a big exhibition is organized, watching over all of the pictures is a big security concern. Your task is that for a given gallery to write a program which finds the surface of the area of the floor, from which each point on the walls of the gallery is visible. On the figure 1. a map of a gallery is given in some co-ordinate system. The area wanted is shaded on the figure 2.
poj <wbr>1279 <wbr>Art <wbr>Gallery

Input

The number of tasks T that your program have to solve will be on the first row of the input file. Input data for each task start with an integer N, 5 <= N <= 1500. Each of the next N rows of the input will contain the co-ordinates of a vertex of the polygon ? two integers that fit in 16-bit integer type, separated by a single space. Following the row with the co-ordinates of the last vertex for the task comes the line with the number of vertices for the next test and so on.

Output

For each test you must write on one line the required surface - a number with exactly two digits after the decimal point (the number should be rounded to the second digit after the decimal point).

Sample Input

1
7
0 0
4 4
4 7
9 7
13 -1
8 -6
4 -4

Sample Output

80.00

Source

 
 
求多边形核的面积。
红果果的半平面交。题目保证顺时针读入。
 
特别需要注意的一点就是平行的判断。比较恶心啊、、视情况而定、、
 
AC CODE
 
program pku_1279;
const eps=1e-7;
type dotsty=record
                          x,y:double;
                      end;
        linesty=record
                            x1,y1,x2,y2,k:double;
                            sty:longint;
                        end;
var line:array[1..1500] of linesty;
      dot:array[1..1500] of dotsty;
      q:array[1..1500] of longint;
      n,cases:longint;
//============================================================================
procedure swap(x,y:longint);
var tt:linesty;
begin
  tt:=line[x]; line[x]:=line[y]; line[y]:=tt;
end;
//============================================================================
procedure qsort(l,r:longint);
var k:double;
      i,j:longint;
begin
  k:=line[(l+r) shr 1].k; i:=l; j:=r;
  repeat
      while line[i].k>k do inc(i);
      while line[j].k
      if i<=j then
      begin
          swap(i,j);
          inc(i); dec(j);
      end;
  until i>j;
  if l
  if i
end;
//============================================================================
procedure init;
var i,j:longint;
begin
  readln(n);
  for i:=1 to n do
      readln(line[i].x1,line[i].y1);
  for i:=1 to n-1 do line[i].x2:=line[i+1].x1;
  for i:=1 to n-1 do line[i].y2:=line[i+1].y1;
  line[n].x2:=line[1].x1; line[n].y2:=line[1].y1;
  for i:=1 to n do
  begin
      if (line[i].x1
          ((line[i].x1=line[i].x2) and (line[i].y1
              line[i].sty:=1 else line[i].sty:=2;
      if line[i].x1<>line[i].x2 then
          line[i].k:=(line[i].y1-line[i].y2)/(line[i].x1-line[i].x2) else
              line[i].k:=maxlongint;
  end; i:=1; j:=n;
  repeat
    while line[i].sty=1 do inc(i);
    while line[j].sty=2 do dec(j);
    if i<=j then swap(i,j);
  until i>j; qsort(1,j); qsort(i,n);
end;
//============================================================================
function left(x,y:double; l:linesty):boolean;
begin
  if (l.x2-l.x1)*(y-l.y1)-(x-l.x1)*(l.y2-l.y1)>-eps then
      exit(true) else exit(false);
end;
//============================================================================
function cross(l1,l2:linesty):dotsty;
var s1,s2,ss,k,dx,dy:double;
begin
  s1:=(l2.x2-l1.x1)*(l1.y2-l1.y1)-(l1.x2-l1.x1)*(l2.y2-l1.y1);
  s2:=(l1.x2-l1.x1)*(l2.y1-l1.y1)-(l2.x1-l1.x1)*(l1.y2-l1.y1);
  ss:=s1+s2;
  if abs(ss)
  begin
      cross.x:=maxlongint; exit;
  end;
  k:=s1/ss;
  dx:=l2.x1-l2.x2; dy:=l2.y1-l2.y2;
  cross.x:=l2.x2+dx*k;
  cross.y:=l2.y2+dy*k;
end;
//============================================================================
function half_plane:double;
var tmp:dotsty;
      tt:double;
      i,be,en:longint;
      flag:boolean;
begin
  be:=1; en:=1; q[1]:=1;
  for i:=2 to n do
  begin
      if abs(line[q[en]].k-line[i].k)
          if left(line[q[en]].x1,line[q[en]].y1,line[i]) then
              dec(en) else continue;
      while be
      begin
          tmp:=cross(line[i],line[q[en]]);
          if tmp.x=maxlongint then exit(0);
          if left(tmp.x,tmp.y,line[q[en-1]]) then
              dec(en) else break;
      end;
      while be
      begin
          if line[i].sty=1 then break;
          if line[i].k>line[q[be]].k-eps then break;
          tmp:=cross(line[i],line[q[be]]);
          if tmp.x=maxlongint then exit(0);
          if left(tmp.x,tmp.y,line[q[be+1]]) then
              inc(be) else break;
      end;
      if be
      begin
          tmp:=cross(line[i],line[q[en]]);
          if tmp.x=maxlongint then exit(0);
          if left(tmp.x,tmp.y,line[q[be]]) then continue;
      end; inc(en); q[en]:=i;
  end; tt:=0;
  if en
  for i:=be to en-1 do
      dot[i]:=cross(line[q[i]],line[q[i+1]]);
  dot[en]:=cross(line[q[en]],line[q[be]]);
  for i:=be to en-1 do
      tt:=tt+dot[i].x*dot[i+1].y-dot[i+1].x*dot[i].y;
  tt:=tt+dot[en].x*dot[be].y-dot[be].x*dot[en].y;
  tt:=abs(tt/2); exit(tt);
end;
//============================================================================
procedure work;
var s:double;
begin
  init;
  s:=half_plane;
  writeln(s:0:2);
end;
//============================================================================
begin
  assign(input,'1.in'); reset(input);
  readln(cases);
  for cases:=1 to cases do work;
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值