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. 使用强大的可视化工具redislive来监控我们的redis,别让自己死的太惨~~~

    作为玩windows的码农,在centos上面装点东西,真的会崩溃的要死,,,我想大家也知道,在centos上面,你下载的是各种源代码,需要自己编译...而 使用yum的话,这个吊软件包有点想nuge ...

  2. 大数据系列(2)——Hadoop集群坏境CentOS安装

    前言 前面我们主要分析了搭建Hadoop集群所需要准备的内容和一些提前规划好的项,本篇我们主要来分析如何安装CentOS操作系统,以及一些基础的设置,闲言少叙,我们进入本篇的正题. 技术准备 VMwa ...

  3. 安装Visual Studio的插件AnkhSvn

    安装Visual Studio的插件AnkhSvn 安装AnkhSvn(VS插件).下载地址:http://download.csdn.net/detail/luqingfei/4538807 Ank ...

  4. MySQL 更新语句技巧

    一. 多表更新 1. 数据准备 mysql> mysql> select goods_id, goods_name,goods_cate from tdb_goods; +-------- ...

  5. c#中对txt文件的读取与写入,针对二维数组

    class Program { ; ; static string[,] str = new string[ROW, COL]; static void Main(string[] args) { R ...

  6. Shell基础学习小结

    0 shell基础概念 Shell是解释性语言,使用脚本编程语言的好处是,它们多半运行在比编译型语言还高的层级,能够轻易处理文件与目录之类的对象:缺点是它们的效率通常不如编译型语言.Shell命令有本 ...

  7. UVALive 4998 Simple Encryption --DFS

    题意: 给出K1,求一个12位数(不含前导0)K2,使得K1^K2 mod (10^12) = K2. 解法: 求不动点问题. 有一个性质: 如果12位数K2满足如上式子的话,那么K2%1,K2%10 ...

  8. HDU 1524 A Chess Game【SG函数】

    题意:一个N个点的拓扑图,有M个棋子,两个人轮流操作,每次操作可以把一个点的棋子移动到它的一个后继点上(每个点可以放多个棋子),直到不能操作,问先手是否赢. 思路:DFS求每个点的SG值,没有后继的点 ...

  9. oracle 错误代码大全

    oracle错误代码大全(超详细)   ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最 ...

  10. CH Round #72 奇数码问题[逆序对 观察]

    描述 你一定玩过八数码游戏,它实际上是在一个3*3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3*3的网格中. 例如:5 2 81 3 _4 6 7 在游戏过程中,可以把空格与其上 ...