poj3819 Coverage (求直线与圆的交占直线的百分比 )
题意:给你一条直线和若干个圆,求圆与直线相交的长度占整条直线的比例
解题思路:通过定比分点的方法求出圆与直线的交占圆的比例。
第一步:(确定投影的方向是x轴还是y轴)
(1)当直线的line.s(x, y), line.e(x, y)的line.s.x与line.e.x不同一时候,这条直线能够等同于起点为line.s.x, line.e.x;
(2)不满足(1)时(即line.s.x==line.e.x时),当直线的line.s(x, y), line.e(x, y)的line.s.y与line.e.y不同一时候。这条直线能够等同于起点为line.s.x, line.e.x;
(3)当不满足(1)以及(2)时(即line.s==line.e),这时候直线为一个点,不论什么的圆都与它没有交。圆占整条直线的比例为0;
第二步:(将圆投影到第一步得到的直线上)
求出圆在直线上的投影的范围;
第三步:
求出全部圆的并。将圆的并除以线段的长度。求圆与线段的交占线段的百分比;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAX = 300;
struct Node
{
double x, y;
};
struct Line
{
Node s, e;
};
Line line;
Node p[MAX];
double A, B, C, delta;
double x, y, r;
double x11, y11, dx, dy;
Node tmp, cir;
double sqr(double x)
{
return x * x;
}
int circle_cross_line(Node s, Node e, Node O, double r)//推断圆与直线是否有交点
{
double x0 = O.x, y0=O.y;
x11 = s.x, y11 = s.y;
double x2 = e.x, y2 = e.y;
dx = x2 - x11, dy = y2 - y11;
A = dx * dx + dy*dy;
B = 2 * dx * (x11 - x0) + 2 * dy * (y11 - y0);
C = sqr(x11-x0) + sqr(y11-y0) - sqr(r);
delta = sqr(B) - 4 * A * C;
return delta > 0;
}
int cmp(Node a, Node b)
{
if (a.x < b.x)
return 1;
return 0;
}
int main()
{
int n, i, cnt;
int flag, flagnum;
double leng;
while (scanf("%d", &n) && n)
{
flagnum = 0;
scanf("%lf%lf%lf%lf", &line.s.x, &line.s.y, &line.e.x, &line.e.y);
if (line.s.x!=line.e.x)
{
if (line.s.x < line.e.x)
{
tmp.x = line.s.x;
tmp.y = line.e.x;
}
else
{
tmp.x = line.e.x;
tmp.y = line.s.x;
}
flag = 0;
leng = fabs(line.e.x - line.s.x);
}
else if (line.s.x==line.e.x && line.s.y!=line.e.y)
{
if (line.s.y < line.e.y)
{
tmp.x = line.s.y;
tmp.y = line.e.y;
}
else
{
tmp.x = line.e.y;
tmp.y = line.s.y;
}
flag = 1;
leng = fabs(line.e.y - line.s.y);
}
else
flagnum = 1;
cnt = 0;
for (i=0; i<n; i++)
{
scanf("%lf%lf%lf", &cir.x, &cir.y, &r);
if (flagnum)
continue;
if (circle_cross_line(line.s, line.e, cir, r))
{
p[cnt].x = (-B-sqrt(delta))/(2.0*A);
p[cnt].y = (-B+sqrt(delta))/(2.0*A);
if (flag==0)
{
p[cnt].x = p[cnt].x * dx + x11;
p[cnt].y = p[cnt].y * dx + x11;
}
else
{
p[cnt].x = p[cnt].x * dy + y11;
p[cnt].y = p[cnt].y * dy + y11;
}
if (p[cnt].x>p[cnt].y)
{
double t = p[cnt].x;
p[cnt].x = p[cnt].y;
p[cnt].y = t;
}
if (p[cnt].x>tmp.y || p[cnt].y<tmp.x)
continue;
if (p[cnt].x<tmp.x)
p[cnt].x = tmp.x;
if (p[cnt].y>tmp.y)
p[cnt].y = tmp.y;
cnt++;
}
}
if (flagnum || cnt==0)
printf("0.00\n");
else
{
sort(p, p+cnt, cmp);
double sum = 0;
tmp = p[0];
for (i=1; i<cnt; i++)
{
if (p[i].x < tmp.y)
{
if (p[i].y > tmp.y)
tmp.y = p[i].y;
}
else
{
sum += tmp.y - tmp.x;
tmp = p[i];
}
}
sum += tmp.y - tmp.x;
printf("%.2f\n", sum/leng*100.0);
}
}
return 0;
}
poj3819 Coverage (求直线与圆的交占直线的百分比 )的更多相关文章
- poj3675 求多边形与圆的面积交
题意:给出多边形的顶点坐标.圆的圆心坐标和半径,求面积交 sol:又是模板题啦= = 注意poj的C++好像认不出hypot函数,要稍微改写一下. hypot(double x,double y):即 ...
- 简单几何(直线与圆的交点) ZOJ Collision 3728
题目传送门 题意:有两个一大一小的同心圆,圆心在原点,大圆外有一小圆,其圆心有一个速度(vx, vy),如果碰到了小圆会反弹,问该圆在大圆内运动的时间 分析:将圆外的小圆看成一个点,判断该直线与同心圆 ...
- HDU 3467 (求五个圆相交面积) Song of the Siren
还没开始写题解我就已经内牛满面了,从晚饭搞到现在,WA得我都快哭了呢 题意: 在DotA中,你现在1V5,但是你的英雄有一个半径为r的眩晕技能,已知敌方五个英雄的坐标,问能否将该技能投放到一个合适的位 ...
- Agent J(求三个圆围成的区域面积)
A - A Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status P ...
- hough变换是如何检测出直线和圆的?
(I)直线篇 1 直线是如何表示的?对于平面中的一条直线,在笛卡尔坐标系中,常见的有点斜式,两点式两种表示方法.然而在hough变换中,考虑的是另外一种表示方式:使用(r,theta)来表示一条直线. ...
- hdu 4404 Worms(多边形与圆的交)
求出爆炸点的坐标,就成了多边形与圆相交面积的模板题了... #include<algorithm> #include<iostream> #include<cstring ...
- zznuoj 1540 : 直线与圆
题目描述 给出一个圆的圆心坐标与圆的半径,和一条直线上的两点坐标,求这条直线与圆有多少个交点. 输入 输入3个实数x,y,r值分别表示圆心坐标与圆的半径,输入4个实数x1,y1,x2,y2表示直线上的 ...
- 【python+opencv】直线检测+圆检测
Python+OpenCV图像处理—— 直线检测 直线检测理论知识: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进 ...
- hdu 3264 圆的交+二分
Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
随机推荐
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐( LIS )
求LIS , 然后用 n 减去即为answer ---------------------------------------------------------------------------- ...
- sicily9162. RAZLIKA
9162. RAZLIKA 限制条件 时间限制: 2 秒, 内存限制: 256 兆 题目描述 Mirko's newest math homework assignment is a very dif ...
- 我的Python成长之路---第七天---Python基础(22)---2016年2月27日(晴)
socket网络编程 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. ...
- 转: seajs手册与文档之--模块定义
模块定义 define id dependencies factory exports require require.async require.resolve require.load requi ...
- html = data.decode('gbk').encode('utf-8')
html = data.decode('gbk').encode('utf-8')此处encode编码要与html文件内charset=utf-8的格式一致,如果不一致,浏览器打开乱码,文本编辑器正常 ...
- boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等
一.使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待. //makefile #-------------------------------------------------- ...
- pragma comment
pragma指令简介 在编写程序的时候,我们经常要用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. 下面介绍了一下该指令的一些常用参数,希望对大家有所帮助! 一. mess ...
- poj 1503 Integer Inquiry (高精度运算)
题目链接:http://poj.org/problem?id=1503 思路分析: 基本的高精度问题,使用字符数组存储然后处理即可. 代码如下: #include <iostream> # ...
- Minimum Window Substring @LeetCode
不好做的一道题,发现String Algorithm可以出很多很难的题,特别是多指针,DP,数学推导的题.参考了许多资料: http://leetcode.com/2010/11/finding-mi ...
- XML IList<T> TO DataSet TO DataTable 相互转换
//遍历XML 获得 DataSet //XmlTextReader static void Main(string[] args) { string xmlData = @"D:\stud ...