You can Solve a Geometry Problem too

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

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
 
题意:求n条直线交点的个数
 
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
using namespace std;
const int N = ;
int flag;
double ans1,ans2,yy;
struct Point//定义点的结构体
{
double x, y;
};
struct stline//定义边的结构体
{
Point a, b;
} line[]; bool cmp(Point a, Point b)
{
return a.y < b.y;
}
int dblcmp(double a, double b)
{
if (fabs(a - b) <= 1E-) return ;
if (a > b) return ;
else return -;
}
//***************点积判点是否在线段上***************
double dot(double x1, double y1, double x2, double y2) //点积
{
return x1 * x2 + y1 * y2;
} int point_on_line(Point a, Point b, Point c) //求a点是不是在线段bc上,>0不在,=0与端点重合,<0在。
{
return dblcmp(dot(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y), );
}
//**************************************************
double cross(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
double ab_cross_ac(Point a, Point b, Point c) //ab与ac的叉积
{
return cross(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y);
}
int ab_cross_cd (Point a,Point b,Point c,Point d) //求ab是否与cd相交,交点为p。1规范相交,0交点是一线段的端点,-1不相交。
{
double s1,s2,s3,s4;
int d1,d2,d3,d4;
Point p;
d1=dblcmp(s1=ab_cross_ac(a,b,c),);
d2=dblcmp(s2=ab_cross_ac(a,b,d),);
d3=dblcmp(s3=ab_cross_ac(c,d,a),);
d4=dblcmp(s4=ab_cross_ac(c,d,b),); //如果规范相交则求交点
if ((d1^d2)==- && (d3^d4)==-)
{
p.x=(c.x*s2-d.x*s1)/(s2-s1);
p.y=(c.y*s2-d.y*s1)/(s2-s1);
return ;
} //如果不规范相交
if (d1== && point_on_line(c,a,b)<=)
{
p=c;
return ;
}
if (d2== && point_on_line(d,a,b)<=)
{
p=d;
return ;
}
if (d3== && point_on_line(a,c,d)<=)
{
p=a;
return ;
}
if (d4== && point_on_line(b,c,d)<=)
{
p=b;
return ;
}
//如果不相交
return -;
}
int main()
{
int t;
while(scanf("%d", &t)&&t)
{
int cnt=;
for(int i=;i<=t;i++)
scanf("%lf%lf%lf%lf", &line[i].a, &line[i].a.y, &line[i].b.x, &line[i].b.y);
for(int i=;i<=t;i++)
{
for(int j=i+;j<=t;j++)
{
if(ab_cross_cd(line[i].a, line[i].b, line[j].a, line[j].b)!=-)
cnt++;
}
}
printf("%d\n",cnt);
}
return ;
}
 

hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数的更多相关文章

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

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

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

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

  5. HDU 1086 You can Solve a Geometry Problem too( 判断线段是否相交 水题 )

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

  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. Hdoj 1086.You can Solve a Geometry Problem too 题解

    Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...

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

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

  9. 【HDOJ】1086 You can Solve a Geometry Problem too

    数学题,证明AB和CD.只需证明C.D在AB直线两侧,并且A.B在CD直线两侧.公式为:(ABxAC)*(ABxAD)<= 0 and(CDxCA)*(CDxCB)<= 0 #includ ...

随机推荐

  1. 移除微信昵称中的emoji字符

    移除微信昵称中的emoji字符: /** * 移除微信昵称中的emoji字符 * @param type $nickname * @return type */ function removeEmoj ...

  2. python opencv:图像的一些属性与操作

    img = cv.imread(xxx) # 常用的有以下属性 type(img) # img的数据类型 img.shape # img的结构 img.size # img的大小 img.dtype ...

  3. ETCD授权认证

    export ETCDCTL_API=3 ENDPOINTS=localhost:2379 etcdctl --endpoints=${ENDPOINTS} role add root etcdctl ...

  4. 吴裕雄--天生自然Numpy库学习笔记:NumPy 创建数组

    import numpy as np x = np.empty([3,2], dtype = int) print (x) import numpy as np # 默认为浮点数 x = np.zer ...

  5. k sum 问题系列

    转自:http://tech-wonderland.net/blog/summary-of-ksum-problems.html (中文旧版)前言: 做过leetcode的人都知道, 里面有2sum, ...

  6. rtt学习之线程间同步与通信

    一 线程间的同步与互斥:信号量.互斥量.实践集 线程互斥是指对于临界区资源访问的排它性,如多个线程对共享内存资源的访问,生产消费型对产品的操作.临界区操作操作方法有: rt_hw_interrupt_ ...

  7. Update(stage3):第1节 redis组件:4、安装(略);5、数据类型(略);6、javaAPI操作;

    第三步:redis的javaAPI操作 操作string类型数据 操作hash列表类型数据 操作list类型数据 操作set类型的数据 详见代码

  8. 【译】高级T-SQL进阶系列 (二)【下篇】:使用 APPLY操作符

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 使用OUTER APPLY 操作符 OUTER APPLY操作符工作起来和CROSS APPLY比较类似.唯一的 ...

  9. C — 小知识

    老是记错int与void*之间的转换,所以记录一个,顺便用一下一些宏.预处理... int与void*的转换.打印变量名: #include <stdio.h> // 打印变量名 #def ...

  10. 第二节: Vuejs常用特性1

    一. 常用特性 1. 表单元素 通过 v-model指令绑定 输入框.单选/多选框.下拉框.文本框 2. 表单域修饰符 (1) .number:转换成数值,如果输入的是非数字字符串时,无法进行转换 ( ...