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. LoadRunner 录制 mobile

    方法一:本地安装安卓模拟器,用LR选择模拟器录制方式录制 方法二:手机真机需要root,可以在电脑上下载一键root工具(如卓大师),然后手机和电脑用数据线连接,然后root. 在手机上运行 Mobi ...

  2. Linux命令-文件搜索命令:whereis

    主要用途:查找linu命令,而不是磁盘上的普通文件,并且能看到命令的目录和帮助文件. whereis useradd 查找命令useradd的所在位置,同时还查出来它的帮助文件所在位置 whereis ...

  3. 转 Android开发学习笔记:浅谈WebView

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/647456 ...

  4. CString中Format函数与格式输入与输出

    CString中Format函数与格式输入与输出 Format是一个非经常常使用.却又似乎非常烦的方法,下面是它的完整概貌.以供大家查询之用:   格式化字符串forma("%d" ...

  5. DataGridView 的单元格的边框、 网格线样式的设定

    1) DataGridView 的边框线样式的设定DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的. BorderStyle 属性设定值是 ...

  6. string.Format字符串格式化说明

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  7. char类型到底是有符号还是无符号

    根据c标准,char类型到底是有符号整数类型还是无符号整数类型,这取决于c实现,也就是c编译器的作者的想法:( 那么,如何快速的编写一个检测程序,查看当前编译器如何对char进行定义? #includ ...

  8. Fastboot和Recovery

    Fastboot是什么意思?从字面意思来讲,Fastboot是『快速启动』的意思.通常大家所讨论的Fastboot,通常都是说安卓手机的Fastboot模式.在安卓手机中fastboot是一种比rec ...

  9. jquery判断元素的子元素是否存在

    jquery判断元素的子元素是否存在的示例代码. jquery判断子元素是否存在 一.判断子元素是否存在 //一级子元素 if($("#specialId>img").len ...

  10. atitit.seo 发帖关键词以及链接的制作.doc

    atitit.seo 发帖关键词以及链接的制作.doc 1. 关键词的获得(by cate) 1 1.1. 删除统计数量     Cartier(144)  格式 1 1.2. \(\d*\)  替换 ...