You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6837 Accepted Submission(s):
3303

Problem Description
Many geometry(几何)problems were designed in the
ACM/ICPC. And now, I also prepare a geometry problem for this final exam.
According to the experience of many ACMers, geometry problems are always much
trouble, but this problem is very easy, after all we are now attending an exam,
not a contest :)
Give you N (1<=N<=100) segments(线段), please output the
number of all intersections(交点). You should count repeatedly if M (M>2)
segments intersect at the same point.

Note:
You can assume that two
segments would not intersect at more than one point.

 
Input
Input contains multiple test cases. Each test case
contains a integer N (1=N<=100) in a line first, and then N lines follow.
Each line describes one segment with four float values x1, y1, x2, y2 which are
coordinates of the segment’s ending.
A test case starting with 0 terminates
the input and this test case is not to be processed.
 
Output
For each case, print the number of intersections, and
one line one case.
 
Sample Input
2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
0.00 0.00 1.00 0.00
0
 
Sample Output

若是判断直线和线段是否有交点,把on_segment去掉就可以了

  判断两线段是否相交:

  我们分两步确定两条线段是否相交:

  (1)快速排斥试验

    设以线段 P1P2 为对角线的矩形为R, 设以线段 Q1Q2 为对角线的矩形为T,如果R和T不相交,显然两线段不会相交。

  (2)跨立试验

    如果两线段相交,则两线段必然相互跨立对方。若P1P2跨立Q1Q2 ,则矢量 ( P1 - Q1 ) 和( P2 - Q1 )位于矢量( Q2 - Q1 ) 的两侧,即( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0。上式可改写成( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) > 0。当 ( P1 - Q1 ) × ( Q2 - Q1 ) = 0 时,说明 ( P1 - Q1 ) 和 ( Q2 - Q1 )共线,但是因为已经通过快速排斥试验,所以 P1 一定在线段 Q1Q2上;同理,( Q2 - Q1 ) ×(P2 - Q1 ) = 0 说明 P2 一定在线段 Q1Q2上。所以判断P1P2跨立Q1Q2的依据是:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0。同理判断Q1Q2跨立P1P2的依据是:( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) >= 0。具体情况如下图所示:

  在相同的原理下,对此算法的具体的实现细节可能会与此有所不同,除了这种过程外,大家也可以参考《算法导论》上的实现。

关于计算几何算法概述网站 http://dev.gameres.com/Program/Abstract/Geometry.htm 一个挺好的网站

#include<stdio.h>//判断线段相交模版

struct point
{
double x,y;
}; double direction( point p1,point p2,point p )
{
//叉乘符号,向量a(x1,y1)×向量b(x2,y2)=x1*y2-x2*y1;
return ( p1.x -p.x )*( p2.y-p.y) - ( p2.x -p.x )*( p1.y-p.y) ;
} int on_segment( point p1,point p2 ,point p )
{
double max=p1.x > p2.x ? p1.x : p2.x ;
double min =p1.x < p2.x ? p1.x : p2.x ; if( p.x >=min && p.x <=max )
return ;
else
return ;
} int segments_intersert( point p1,point p2,point p3,point p4 )
{
double d1,d2,d3,d4;
//判断p3和p4是否在p1和p2两侧
d1 = direction ( p1,p2,p3 );
d2 = direction ( p1,p2,p4 );
//判断p1和p2是否在p3和p4两侧
d3 = direction ( p3,p4,p1 );
d4 = direction ( p3,p4,p2 );
if( d1*d2< && d3*d4< )//在两侧,说明p1p2和p3p4相交
return ;
else if( d1== && on_segment( p1,p2,p3 ) )
return ;
else if( d2== && on_segment( p1,p2,p4 ) )
return ;
else if( d3== && on_segment( p3,p4,p1 ) )
return ;
else if( d4== && on_segment( p3,p4,p2 ) )
return ; return ;
} int main()
{
int n,i,j,num;
point begin[],end[]; //结构体数组 while(scanf("%d",&n)&&n!= )
{
num=;
for(i=;i<n;i++)
{
scanf("%lf%lf%lf%lf",&begin[i].x ,&begin[i].y ,&end[i].x ,&end[i].y );
}
for(i=;i<n;i++)//线段两两比较
{
for(j=i+;j<n;j++)
{
if( segments_intersert( begin[i],end[i],begin[j],end[j] ) )
num++;
}
}
printf("%d\n",num);
}
return ;
}

几何,哥哥要征服你,fighting!

You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交的更多相关文章

  1. 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 ...

  2. (叉积,线段判交)HDU1086 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 ...

  3. HDU1086 You can Solve a Geometry Problem too(计算几何)

    You can Solve a Geometry Problem too                                         Time Limit: 2000/1000 M ...

  4. 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 ...

  5. 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 ...

  6. (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/ ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. CODE FIRST之空数据模型

    1.首先添加空Code Firtst模型 2.新建两个实体类,关系一对多 public class UserInfo { public UserInfo() { OrderInfo = new Has ...

  2. 【转】PowerDesigner code、name显示设置 及 同时显示办法

    原文地址:http://blog.csdn.net/fy_hanxu/article/details/52468927 菜单->Tool->Model Options->Name C ...

  3. 使用Charles对Android App的https请求进行抓包

    本文背景 公司新项目要求抓取目前市面上一些热门App的数据,经过研究发现很多App的网络请求都使用https进行数据传输,这样问题就来了,http使用明文传输所有请求都能拦截到,而https请求无法拦 ...

  4. Ubuntu18.04 - 安装深度桌面(Deepin Linux Desktop)

    深度Linux官网:https://www.deepin.org 深度桌面感觉确实不错,所以打算安装上来,以后体验一下,下面是安装的命令: 1, sudo add-apt-repository ppa ...

  5. maven build的常用生命周期

    常用的maven build goals: validate - validate the project is correct and all necessary information is av ...

  6. Vue2.5开发去哪儿网App 第四章笔记 上

    一 .  组件细节知识点 1.  解决组件在h5中编码规范 例如 : table , ul , ol  等等 <table> <tbody> <row></r ...

  7. [Umbraco] Data Type的扩展编程

    继续从上面的Data Types的自定义控件说起.前面用到了自定义控件的数据绑定,虽然这使得我们可以调用外部数据了,但这似乎还比较死板,如果再调用其他数据,还得再创建一个控件,那样的话就会出现类似的功 ...

  8. 写一个MySql存储过程实现房贷等额本息还款计算(另外附javascript代码)

    写一个MySql存储过程实现房贷等额本息还款计算 MySql存储过程代码如下: DROP procedure IF EXISTS `calc_equal_interest_proc`; DELIMIT ...

  9. Chapter 3 Phenomenon——19

    His unfriendliness intimidated me. 他的不友好恐吓到了我. My words came out with less severity than I'd intende ...

  10. dig命令详解

    dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常 语法 dig(选项)(参数) 选项 @<服务器地址>:指定进行域名解析的域名服务器: -b<ip地址>:当主机 ...