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. Android SDK 更新和下载慢怎么办?

    博客搬家:因为各种原因,我如今的博客将首发于blog.mojijs.com, 能够百度搜索 "姜哥的墨迹技术博客" , 或者 点击这里 本文地址 http://blog.mojij ...

  2. 最大似然估计(MLE)与最大后验概率(MAP)

    何为:最大似然估计(MLE): 最大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”.可以通过采样,获取部分数据,然后通过最大似然估计来获取已知模型的参数. 最大似然估计 ...

  3. js表单验证控制代码大全

    http://www.cnblogs.com/SAL2928/archive/2008/10/24/1319020.html目录: 1:js 字符串长度限制.判断字符长度.js限制输入.限制不能输入. ...

  4. 摘:C++日期时间与字符串间的转换

    VC6中 CString sTime = _T("2007-10-26 13:20:30"); char *charTime = (LPSTR)(LPCTSTR)sTime; CS ...

  5. C#中Out和Ref參数修饰符

    在编程过程中对于函数之间的參数的传递一般分为两种:传值和传地址. 以下为大家分析一下. 传值 比方你又一份文档,假设採用传值的话.相当于我复制了一份,因此我对我这份文档的改动都不会影响到你的那份.假设 ...

  6. ajax提交数据处理总结

    一:Ajax中Get请求与Post请求的区别  http://www.cnblogs.com/oneword/archive/2011/06/06/2073533.html 二:ajax分页: 360 ...

  7. uploadify onSelect

    uploadify onSelect [return false]停止选择 $("#fileEleId").uploadify({ 'width': _option.width, ...

  8. Ubuntu14.04单机版kubernetes安装指导

    转:http://dockone.io/article/950 概述本文主要讲述如何在Ubuntu系统上安装kubernetes,网络上也有许多相关的文章,感觉都不是很清晰,这里我将自己的安装实践给大 ...

  9. poj1936

    非连续子串匹配题,直接模拟 /** \brief poj 1936 * * \param date 2014/8/5 * \param state AC * \return memory 804k t ...

  10. 基于redis的分布式缓存disgear开源到github上了

    disgear是笔者参考solrcloud架构基于redis实现的分布式的缓存,支持数据切分到多台机器上,支持HA,支持读写分离和主节点失效自动选举,目前把它开放到github上,开放给大家 gith ...