POJ 3304 Segments [枚举+叉乘判断线段相交]【计算几何】

题目链接 http://poj.org/problem?id=3304

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11920 Accepted: 3757

Description

Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output “Yes!”, if a line with desired property exists and must output “No!” otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

Sample Input

3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0
Sample Output

Yes!
Yes!
No!
Source

Amirkabir University of Technology Local Contest 2006


题目大意 :
就是给你一堆线段 问你是否存在这样一条直线 使得所有线段在直线上的投影均有公共部分 有输出Yes 没有输出No

解题方法 :
投影直线上有公共点的话就是说这条直线存在一个垂线经过所有线段 就是与所有线段均相交

只要枚举这些线段端点所确定的直线 是否存在一条与所有线段均相交即可

判断线段相交用的是叉乘 首先用两点p1,p2确定了一条直线 在用p1,p2分别与计算线段两个端点计算叉乘即可
叉乘之积>0就说明线段两端点在直线的同侧 也就是直线不经过此线段

注意 :
本题是double类型
判断是否有重点 重点定义为坐标非常接近的两点 <1e-8即可
判断叉乘之积时 只要<1e-8 即可

附本题代码


这是引用网上与我代码非常接近的大神的代码 能Ac 但我的不知为什么就是不行 ~>_<~ 但也在下面贴出来了 如果有人找到BUG 了 一定要回复我 不胜感激

#include <iostream>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值