题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086

判断两条线段是否有交点,我用的是跨立实验法:

两条线段分别是A1到B1,A2到B2,很显然,如果这两条线段有交点,那么可以肯定的是:

A1-B1,A2-B1这两个向量分别在B2-B1的两边,判断是不是在两边可以用向量的叉积来判断,这里就不说了,同理B1-A1,B2-A1在A2-A1的两边,当同时满足这两个条件时,说明这两条线段是有交点的。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
const double eps = 1e-;
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y) {}
inline friend point operator + (point p1,point p2)
{
return point(p1.x+p2.x,p1.y+p2.y);
}
inline friend point operator - (point p1,point p2)
{
return point(p1.x-p2.x,p1.y-p2.y);
}
}A[maxn],B[maxn]; inline double dot(point p1,point p2)
{
return p1.x*p2.y - p2.x*p1.y;
}
inline double dis(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
int judge(point p1,point p2,point p3,point p4)
{
double temp = dot(p3-p1,p2-p1) * dot(p4-p1,p2-p1);
if(temp < || fabs(temp) < eps) return ;
return ;
} int main()
{
//freopen("in","r",stdin);
int n;
while(scanf("%d",&n),n)
{
for(int i = ;i < n;++i)
scanf("%lf%lf%lf%lf",&A[i].x,&A[i].y,&B[i].x,&B[i].y);
int ans = ;
for(int i = ;i < n;++i)
for(int j = i+;j < n;++j)
{
if(judge(A[i],B[i],A[j],B[j]) && judge(A[j],B[j],A[i],B[i])) ans++;
}
printf("%d\n",ans);
}
return ;
}

HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)的更多相关文章

  1. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)

    称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...

  2. hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  3. HDU1086You can Solve a Geometry Problem too(判断线段相交)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  4. hdu 1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  5. HDU 1086:You can Solve a Geometry Problem too

    pid=1086">You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  6. hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  7. hdu 1086 You can Solve a Geometry Problem too (几何)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  8. You can Solve a Geometry Problem too(线段求交)

    http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000 ...

  9. You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...

随机推荐

  1. UVA11136Hoax or what( multiset的应用)

    题目链接 题意:n天,每天往一个箱子里放m个数,放完之后取最大的Max和最小的min做差,并把这两个数去掉,求n天之后的和 multiset 和 set的原理是相似的,multiset可以存多个相同的 ...

  2. ELKstack搭建

    开源实时日志分析ELK平台部署 官网地址:https://www.elastic.co/products 介绍: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现 ...

  3. Apache Nutch v2.3 发布,Java实现的网络爬虫

    http://www.oschina.net/news/59287/apache-nutch-2-3 Apache Nutch v2.3已经发布了,建议所有使用2.X系列的用户和开发人员升级到这个版本 ...

  4. css001 Css需要的html

    Css需要的html 可以忘却的html属性和标签 1.不要用<font>来控制字体的大小和类型.(那要用什么?见第六章) 2.不要用<b>和<i>(b和i只是把字 ...

  5. MySQL数据库常用函数

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 不区分大小写 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等 ...

  6. Java简单的系统登陆

    class Check{ public boolean validate(String name,String password){ if(name.equals("lixinghua&qu ...

  7. Win7环境下Eclipse连接Hadoop2.2.0

    准备: 确保hadoop2.2.0集群正常运行 1.eclipse中建立java工程,导入hadoop2.2.0相关jar包 2.在src根目录下拷入log4j.properties,通过log4j查 ...

  8. Understanding Convolutions

    http://colah.github.io/posts/2014-07-Understanding-Convolutions/ Posted on July 13, 2014 neural netw ...

  9. XtraFinder在OSX10.11的使用

    重启系统,按住command键加上R键 进入恢复模式还是什么模式里,然后启动terminal 然后键入 csrutil enable --without debug 重启电脑,可正常使用 居然上传不了 ...

  10. Elmah 日志记录组件

    http://www.cnblogs.com/jys509/p/4571298.html 简介 ELMAH(Error Logging Modules and Handlers)错误日志记录模块和处理 ...