题目链接: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. zabbix 安装

    php+nginx+mysql+zabbix 官方https://www.zabbix.com/documentation/3.0/manual/installation/install 1.安装依赖 ...

  2. Linux安全之——Ubuntu的iptable命令使用

    设置开机自动启动iptables # sysv-rc-conf --level 2345 iptables on 列出当前iptables的策略和规则 # iptables -L -n 允许已经建立的 ...

  3. myeclipse+tomcat内存溢出

  4. Collections操作实例

    <1>实例操作一:返回不可变的集合 Collections类中可以返回空的List.Set.Map集合,但是通过这种方式返回的对象是无法进行增加数据的,因为在这些操作中并没有实现add() ...

  5. js取float型小数点后两位数的方法

    四舍五入以下处理结果会四舍五入:' var num =2.446242342; num = num.toFixed(2); // 输出结果为 2.45   不四舍五入以下处理结果不会四舍五入:第一种, ...

  6. strace命令(收集整理,常看常新)

    starce的用途和参数:http://man.linuxde.net/strace(linux命令大全) strace命令是一个集诊断.调试.统计与一体 的工具,我们可以使用strace对应用的系统 ...

  7. 居于Web的进度条实现思路(下载百分比)

    http://www.cnblogs.com/wfyfngu/p/4866434.html 在传统桌面项目中,进度条随处可见,但作为一个很好的用户体验,却没有在如今主流的B/S程序中得到传承,不能不说 ...

  8. Yii2 – 如何写一个插件 , 如何做一个扩展

    原文地址: http://www.fancyecommerce.com/2016/05/10/yii2-%E5%A6%82%E4%BD%95%E5%86%99%E4%B8%80%E4%B8%AA%E6 ...

  9. webuploader横向按钮样式

    #picker{display: inline-block;line-height: 1.428571429;vertical-align: middle;margin: 0 12px 0 0;wid ...

  10. 仿微信底部自定义菜单 移动web

    最近在做微信开发,要实现微信公众号改版—-改成微官网形式,即移动web页面中实现公众号的主页面,包括了公众号的菜单在底部显示 本文针对仿公众号底部菜单这个功能实现进行总结.实现采用html和css.J ...