Section 5.3 window area

/*
ID: niepeng1
PROG: window
LANG: C++
*/

/*矩形切割,要注意不能切割出被观察的矩形*/

#include <iostream>
#include <functional>
#include <algorithm>
#include <string>
#include <stdio.h>

using namespace std;
#define min(x,y) x<y?x:y
#define max(x,y) x>y?x:y
struct Node{
 int lx;a
 int ly;
 int hx;
 int hy;
 int level;
};
int top=1,bottom=0;
bool cond[256]={false};
Node window[256];
void w(int lx,int ly,int hx,int hy,char i)
{
 if(cond[i]==true)
  printf("Bad command /n");
 else
  cond[i]=true;
 window[i].hx=hx;
 window[i].hy=hy;
 window[i].lx=lx;
 window[i].ly=ly;
 window[i].level=top++;
}
inline void d(char i)
{
 if(cond[i]==false)
  printf("Bad command /n");
 cond[i]=false;
}
inline void b(char i)
{
 if(cond[i]==false)
  printf("Bad command /n");
 window[i].level=bottom--;
}
inline void t(char i)
{
 if(cond[i]==false)
  printf("Bad command /n");
 window[i].level=top++;
}
inline int area(int lx,int ly,int hx,int hy,int level)
{
 int j,tem1,tem2,tem3,tem4;
 if(ly>=hy||lx>=hx)
  return 0;
 for(j=0;j<256;j++){
  if(cond[j]==true&&window[j].level>level)
   if(!
    (hy<=window[j].ly || window[j].hy<=ly ||
     hx<=window[j].lx || lx >= window[j].hx) )
  {
   tem1=area(lx,max(ly,window[j].ly),min(window[j].lx,hx),min(hy,window[j].hy),level);   
   tem2=area(max(lx,window[j].hx),max(ly,window[j].ly),hx,min(hy,window[j].hy),level);
   tem3=area(lx,ly,hx,min(hy,window[j].ly),level);
   tem4=area(lx,max(ly,window[j].hy),hx,hy,level);
   return tem1+tem2+tem3+tem4;
  }
 }
 return (hx-lx)*(hy-ly);
}
inline double s(char i)
{
 return area(window[i].lx,window[i].ly,window[i].hx,window[i].hy,window[i].level)*(float)100/((window[i].hx-window[i].lx)*(window[i].hy-window[i].ly));

}
int main()
{
 int lx,ly,hx,hy;
 char blank,cmd,i;
 freopen("window.in","r",stdin);
 freopen("window.out","w",stdout);

 while(scanf("%c",&cmd)!=EOF){
  switch(cmd){
  case 'w':
   scanf("%c%c%c%d%c%d%c%d%c%d%c%c",&blank,&i,&blank,&lx,&blank,&ly,&blank,&hx,&blank,&hy,&blank,&blank);
   w(min(lx,hx),min(ly,hy),max(lx,hx),max(ly,hy),i);
   break;
  case 's':
   scanf("%c%c%c%c",&blank,&i,&blank,&blank);
   printf("%.3lf/n",s(i));
   break;
  case 'd':
   scanf("%c%c%c%c",&blank,&i,&blank,&blank);
   d(i);
   break;
  case 'b':  
   scanf("%c%c%c%c",&blank,&i,&blank,&blank);
   b(i);
   break;
  case 't':
   
   scanf("%c%c%c%c",&blank,&i,&blank,&blank);
   t(i);
   break;

  }
 }
 return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值