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/32768 K (Java/Others)
Total Submission(s): 6837 Accepted Submission(s):
3303
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.
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.
one line one case.
若是判断直线和线段是否有交点,把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)几何,判断两线段相交的更多相关文章
- 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 ...
- (叉积,线段判交)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 ...
- HDU1086 You can Solve a Geometry Problem too(计算几何)
You can Solve a Geometry Problem too Time Limit: 2000/1000 M ...
- 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 ...
- 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 ...
- (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/ ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Python isinstance 与 instance 的用法
instance: instance 属于python2的关键字,python2中如果一个类没有继承自object, 那么实例化出来的对象就是instance类型,否则就是class类型. isins ...
- 百度地图sdk---pc端
<div class="map" style="width: 1196px;height: 500px;margin: 50px auto;"> & ...
- Mac 下查看端口是否被占用
1. lsof -i :8080 2. netstat -anp tcp | grep 8080 3. nc -w 10 -n -z 127.0.0.1 8070-8090
- nsurlsessiond - taking up all bandwidth!! Help ?
https://discussions.apple.com/thread/6605949?start=0&tstart=0 #!/bin/sh launchctl unload /System ...
- javascript之快速排序
快速排序思想其实还是挺简单的,分三步走: 1.在数组中找到基准点,其他数与之比较. 2.建立两个数组,小于基准点的数存储在左边数组,大于基准点的数存储在右边数组. 3.拼接数组,然后左边数组与右边数组 ...
- Spring集成Solr搜索引擎
1.导入jar包<dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj ...
- (转)CentOS7安装Nginx1.14.2
原文:https://blog.csdn.net/zhyfyz/article/details/84957381 https://blog.csdn.net/q85795362/article/det ...
- spring jpa : 多条件查询
https://www.cnblogs.com/Donnnnnn/p/6277872.html 方式一: 第一步:EmpAccNumService package com.payease.scford ...
- C# 字符串操作详解
MSDN关于String的所有Method 1.字符串转字符数组 (1).ToCharArray()方法,源码如下: 调用代码: var str = "Hello World"; ...
- 关于配置 TeamCity 清理历史 artifacts 问题
使用 CI 一段时间后,artifacts 占用的磁盘会很大,可以配置保留多少天的 artifacts,具体如下: Administration Click the Edit link for any ...