链接

根据距离可以列得直线方程,附上初始矩形的四个顶点,依次用直线切割。

 #include<iostream>
#include <stdio.h>
#include <math.h>
#include<cstring>
#include<algorithm>
#define eps 1e-8
using namespace std;
const int MAXN=;
int m;
double r,tx,ty,ans;
int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
struct point
{
double x,y;
point(double x=,double y=):x(x),y(y) {}
};
point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
void getline(point x,point y,double &a,double &b,double &c) //两点x、y确定一条直线a、b、c为其系数
{
a = y.y - x.y;
b = x.x - y.x;
c = y.x * x.y - x.x * y.y;
}
void initial()
{
for(int i = ; i <= m; ++i)p[i] = points[i];
p[m+] = p[];
p[] = p[m];
cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数
}
point intersect(point x,point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点
{
//cout<<a<<" "<<b<<" "<<c<<endl;
double u = fabs(a * x.x + b * x.y + c);
double v = fabs(a * y.x + b * y.y + c);
// cout<<u<<" "<<v<<endl;
point pt;
pt.x=(x.x * v + y.x * u) / (u + v);
pt.y=(x.y * v + y.y * u) / (u + v);//cout<<pt.x<<" -"<<pt.y<<" "<<x.x<<" "<<y.x<<endl;
return pt;
}
void cut(double a,double b ,double c)
{
curCnt = ;
for(int i = ; i <= cCnt; ++i)
{
if(a*p[i].x + b*p[i].y + c > -eps)q[++curCnt] = p[i];
else
{
if(a*p[i-].x + b*p[i-].y + c > eps)
{
q[++curCnt] = intersect(p[i-],p[i],a,b,c);
}
if(a*p[i+].x + b*p[i+].y + c > eps) //原理同上
{
q[++curCnt] = intersect(p[i],p[i+],a,b,c);
}
}
//cout<<q[curCnt].x<<" --"<<q[curCnt].y<<" "<<i<<" "<<p[i].x<<" "<<p[i].y<<endl;
}
for(int i = ; i <= curCnt; ++i)p[i] = q[i];
p[curCnt+] = q[];
p[] = p[curCnt];
cCnt = curCnt;
}
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
void solve(double x,double y,int flag)
{
double a,b,c;
if(flag == )
{
a = -*x+*tx,b = -*y+*ty,c = x*x+y*y-tx*tx-ty*ty;
}
else if(flag==)
{
a = *x-*tx,b = *y-*ty,c = tx*tx+ty*ty-x*x-y*y;
}
// if(dcmp(a)==0&&dcmp(b)==0&&dcmp(c)<=0)
// {
// ans = 0;
// return ;
// }
cut(a,b,c);
}
int main()
{
points[] = point(,);
points[] = point(,);
points[] = point(,);
points[] = point(,);
p[] = p[];
m = ;
tx = ,ty=;
char s[];
double x,y;
initial();
while(scanf("%lf%lf%s",&x,&y,s)!=EOF)
{
int flag;
if(strcmp(s,"Colder")==||strcmp(s,"Same")==)solve(x,y,);
if(strcmp(s,"Hotter")==||strcmp(s,"Same")==)solve(x,y,);
tx = x,ty=y;
double area = ;
for(int i = ; i <= cCnt; ++i)
{
//cout<<p[i].x<<" "<<p[i].y<<endl;
area += p[i].x * p[i + ].y - p[i + ].x * p[i].y;
}
area = fabs(area / 2.0);
ans = area;
printf("%.2f\n",ans);
}
}

最新文章

  1. 游走 bzoj 3143
  2. 【原创】.NET平台机器学习组件-Infer.NET连载(一)介绍
  3. About Flash
  4. Linux下一些有用的指令
  5. warning:deprecated conversion from string constant to &#39;char *&#39; 解决方案
  6. Android开源库--SlidingMenu左右侧滑菜单
  7. How to Diagnose Audi Vehicles via Tuirel S777
  8. javascript切换效果
  9. FSG压缩壳和ImportREC的使用 - 脱壳篇05
  10. iOS开发 socket, 全局socket
  11. dedecms的热门标签在那里修改
  12. [Python Web]常见的 POST 提交数据的方式
  13. response.redirect 与location.href 的区别
  14. I/O系统(二)
  15. 简单聊聊Linux学习经历
  16. recovery 根据@/cache/recovery/block.map描述从data分区升级
  17. sql server复制表数据到另外一个表 的存储过程
  18. haproxy [WARNING] 312/111530 (17395) : config : &#39;option forwardfor&#39; ignored for frontend &#39;harbor_login&#39; as it requires HTTP mode.
  19. Linux各个文件夹的作用~~~非常实用!!
  20. 论文笔记——Factorized Convolutional Neural Networks

热门文章

  1. 一个很不错的bash脚本编写教程
  2. SpringMVC 接收复杂对象
  3. 通过spring工厂读取property配置文件
  4. Java中的ClassLoader
  5. Android GridView 第一个Item 点击没反应
  6. Paratroopers
  7. CSUFT 1003 All Your Base
  8. Entity Framework 第七篇 简化排序
  9. 怎样解决:未找到路径“……”的控制器或该控制器未实现 IController?
  10. git总结