You can Solve a Geometry Problem too

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

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.000 0.00 0.00 1.00 0.00 0
 
Sample Output
1 3
 
Author
lcy
 
Recommend
We have carefully selected several similar problems for you:  1392 2108 2150 1348 1147 

 
  计算几何:判断两线段是否相交
  很简单的一道题,套上模板之后直接遍历判断每对线段是否相交,相交就计数,最后输出计数就是交点数。这种题的思路就是做两个验证,这两个验证学名叫快速排斥实验和跨立实验,分别有4个判断和2个判断,只有这两个实验都通过才能说这两条线段相交。详见:
  
 判断两线段是否相交模板:

 struct Point{
double x,y;
};
struct Line{
Point p1,p2;
};
double Max(double a,double b)
{
return a>b?a:b;
}
double Min(double a,double b)
{
return a<b?a:b;
}
double xmulti(Point p1,Point p2,Point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool inter(Line l1,Line l2)
{
if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<= &&
xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<= )
return true;
else
return false;
}
 本题代码:
 #include <iostream>
using namespace std;
struct Point{
double x,y;
};
struct Line{
Point p1,p2;
};
double Max(double a,double b)
{
return a>b?a:b;
}
double Min(double a,double b)
{
return a<b?a:b;
}
double xmulti(Point p1,Point p2,Point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool inter(Line l1,Line l2)
{
if( Min(l2.p1.x,l2.p2.x)<=Max(l1.p1.x,l1.p2.x) &&
Min(l2.p1.y,l2.p2.y)<=Max(l1.p1.y,l1.p2.y) &&
Min(l1.p1.x,l1.p2.x)<=Max(l2.p1.x,l2.p2.x) &&
Min(l1.p1.y,l1.p2.y)<=Max(l2.p1.y,l2.p2.y) &&
xmulti(l1.p1,l2.p2,l2.p1)*xmulti(l1.p2,l2.p2,l2.p1)<= &&
xmulti(l2.p1,l1.p2,l1.p1)*xmulti(l2.p2,l1.p2,l1.p1)<= )
return true;
else
return false;
}
int main()
{
int N;
Line l[];
while(cin>>N){
if(N==) break;
for(int i=;i<=N;i++)
cin>>l[i].p1.x>>l[i].p1.y>>l[i].p2.x>>l[i].p2.y;
int c = ;
for(int i=;i<=N-;i++)
for(int j=i+;j<=N;j++)
if(inter(l[i],l[j]))
c++;
cout<<c<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)的更多相关文章

  1. 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. HDU 1086 You can Solve a Geometry Problem too( 判断线段是否相交 水题 )

    链接:传送门 题意:给出 n 个线段找到交点个数 思路:数据量小,直接暴力判断所有线段是否相交 /*************************************************** ...

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

  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 求n条直线交点的个数

    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 [线段相交]

    题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...

  8. hdu 1147:Pick-up sticks(基本题,判断两线段相交)

    Pick-up sticks Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  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. Tomcat 关闭时报错

    最近tomcat走普通的关闭方式无法正常关闭,会报一些Error,用的是Tomcat7,据说是Tomcat7在关闭的时候加了一些检查线程泄漏内存泄露的东西 总结起来,在我项目中有这么几个原因会导致关闭 ...

  2. 数据库选型之内存数据库eXtremeDB

    刘勇    Email:lyssym@sina.com 简介 鉴于内存数据库访问速率快的特点,本文分别从单线程.多线程(并发访问)和多线程读/写混合访问角度对eXtremeDB数据库读写速率展开测试. ...

  3. 流媒体协议RTMP,RTSP与HLS有什么不同

    转载自:http://www.cuplayer.com/player/PlayerCode/Wowza/2015/0204/1774.html HLS (HTTP Live Streaming) Ap ...

  4. FFmpeg + SDL2 实现的视频播放器「视音频同步」

    文章转自:http://blog.csdn.net/i_scream_/article/details/52760033 日期:2016.10.8 作者:isshe github:github.com ...

  5. C#日期格式化英文月份 VS改大小写的快捷键

    DateTime.Now.ToString("MMM yyyyy",CultureInfo.CreateSpecificCulture("en-GB")) ;  ...

  6. 分别用C和C++来实现一个链栈

    下面通过分别用C和C++来实现一个链栈(链表实现),从中体会数据封装抽象的思想: C语言实现:  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  7. ItelliJ基于Gradle创建及发布Web项目(一)

    背景:安装IntelliJ,去官网下载. 创建WEB项目 1. File->New Project,在弹出的选项框中勾选Web,如下图. IntelliJ默认使用Gradle,感谢Gradle. ...

  8. SQL server 2008定期的备份数据库及删除job

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员 每天守到晚上1点去备份数据库.要实现数据库的 ...

  9. 温故而知新 js 的错误处理机制

    // 在函数块的try中return,会直接成为函数的return值 function test() { try { alrt(123) return 'success' } catch(err) { ...

  10. MySQL中多表删除方法

    如果您是才接触MySQL数据库的新人,那么MySQL中多表删除是您一定需要掌握的,下面就将为详细介绍MySQL中多表删除的方法,供您参考,希望对你学习掌握MySQL中多表删除能有所帮助. 1.从MyS ...