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 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课( BFS )
BFS... 我连水题都不会写了QAQ ------------------------------------------------------------------------- #inclu ...
- c# 流程控制
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 调用 sphinx-build生成HTML文件
安装 Sphinx $ easy_install sphinx Searching for sphinx Reading http://pypi.python.org/simple/sphinx/ R ...
- LNMP一键安装包sh脚本
Xshell 5 (Build 0719) Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `he ...
- Judge loop in directed graph
1 深度优先方法 首先需要更改矩阵初始化函数init_graph() 然后我们需要初始化vist标记数组 深度优先访问图,然后根据是否存在back edge判断是否存在环路 算法如下: #includ ...
- 分支-15. 日K蜡烛图(15)
#include<iostream> using namespace std; int main(){ float o,h,l,c; while(cin>>o>>h ...
- A + B Problem II 大数加法
题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...
- python list comprehension twos for loop 嵌套for循环
list comprehension 后面可以有多个for loops,每个for后面可以有if [(x, y, x * y)for x in(0,1,2,3)for y in(0,1,2,3)if ...
- python:利用asyncio进行快速抓取
web数据抓取是一个经常在python的讨论中出现的主题.有很多方法可以用来进行web数据抓取,然而其中好像并没有一个最好的办法.有一些如scrapy这样十分成熟的框架,更多的则是像mechanize ...
- 第二种:NSObject
- (void)viewDidLoad { [super viewDidLoad]; /** * 开启子线程的方式之一:NSObject */ // 第一个参数:selector // 第二个参数:方 ...