You can Solve a Geometry Problem too

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

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
给出N个线段,求线段相交的个数
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = ;
const double eps = 0.000001;
struct Point
{
double x, y;
Point(double x = , double y = ) : x(x), y(y) {}
};
struct Line
{
Point start, End;
};
Line line[Max];
typedef Point Vector;
Vector operator- (Vector A, Vector B)
{
return Vector(A.x - B.x, A.y - B.y);
}
double Cross(Vector A, Vector B)
{
return A.x * B.y - A.y * B.x;
}
bool OnSegment(Point A, Point B, Point C)
{
double MinX, MaxX, MinY, MaxY;
if (A.x - B.x > eps)
{
MinX = B.x;
MaxX = A.x;
}
else
{
MinX = A.x;
MaxX = B.x;
}
if (A.y - B.y > eps)
{
MinY = B.y;
MaxY = A.y;
}
else
{
MinY = A.y;
MaxY = B.y;
}
// 大于等于 >= -eps
if (C.x - MinX >= -eps && MaxX - C.x >= -eps && C.y - MinY >= -eps && MaxY - C.y >= -eps)
return true;
return false;
}
bool solve(Line A, Line B)
{
double c1 = Cross(A.End - A.start, B.start - A.start);
double c2 = Cross(A.End - A.start, B.End - A.start);
double c3 = Cross(B.End - B.start, A.start - B.start);
double c4 = Cross(B.End - B.start, A.End - B.start);
if (c1 * c2 < && c3 * c4 < ) // && 手残写成了 || wa了好几次
return true;
if (c1 == && OnSegment(A.start, A.End, B.start))
return true;
if (c2 == && OnSegment(A.start, A.End, B.End))
return true;
if (c3 == && OnSegment(B.start, B.End, A.start))
return true;
if (c4 == && OnSegment(B.start, B.End, A.End))
return true;
return false;
} int main()
{
int n;
while (scanf("%d", &n) != EOF && n)
{
int res = ;
for (int i = ; i <= n; i++)
{
scanf("%lf%lf%lf%lf", &line[i].start.x, &line[i].start.y, &line[i].End.x, &line[i].End.y);
}
for (int i = ; i <= n; i++)
{
for (int j = i + ; j <= n; j++)
{
if (solve(line[i], line[j]))
res++;
}
}
printf("%d\n", res);
}
return ;
}
 
 

HDU1086You can Solve a Geometry Problem too(判断线段相交)的更多相关文章

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

  2. HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 判断两条线段是否有交点,我用的是跨立实验法: 两条线段分别是A1到B1,A2到B2,很显然,如果 ...

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

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

  7. HDUOJ1086You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

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

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

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

随机推荐

  1. ORACLE告警日志文件

    告警日志介绍 告警日志文件是一类特殊的跟踪文件(trace file).告警日志文件命名一般为alert_<SID>.log,其中SID为ORACLE数据库实例名称.数据库告警日志是按时间 ...

  2. Expdp 导数错误 ORA-00832

    问题实验环境 操作系统:Red Hat Enterprise Linux Server release 5.7 (Tikanga) 数据库  :Oracle Database 10g Release ...

  3. SQL SERVER 2000 迁移后SQL SERVER代理服务启动错误分析

    公司有一个老系统,这个系统所用的数据库是SQL SERVER 2000,它所在的Dell服务器已经运行超过10年了,早已经过了保修服务期,最近几乎每周会出现一次故障,加之5月份另外一台服务器坏了两个硬 ...

  4. [20141121]无法通过powershell读取sql server性能计数器问题

    背景: 全新服务器,需要增加性能监控,发现无法通过powershell读取性能指标 解决方法: Open the Registry Editor by going to the Start Menu ...

  5. NGUI 指定视口大小

    由于只是给Uinty开发插件,对Unity没有系统的学习,对Unity的很多功能都不是非常了解,幸得其他Unity同事的耐心教导,才不至于想崩头.记录一下,避免重复犯错. NGUI可以建立指定视口大小 ...

  6. 关于 Java 数组的 12 个最佳方法

    1.  声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...

  7. js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)

    js中遍历数组的有两种方式 var array=['a'] //标准的for循环 for(var i=1;i<array.length;i++){ alert(array[i]) } //for ...

  8. stm32 u8 u16 u32

    u8 是 unsigned char u16 是 unsigned short u32 是 unsigned int

  9. JSP连接MySQL数据库问题

    之前写了一大段,还说了一下具体JDBC连接数据库API的具体包的基本知识,哪知道自己手残不小心按了删除按钮.结果去看自动保存记录时,就只剩下下面这段了.好吧,其实最主要最核心的也就是下面这点了.具体如 ...

  10. EF高级应用

    UpdateSourceTrigger     NumberTextbox         参考 Finally! Entity Framework working in fully disconne ...