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. 【原创】Centos配置turn服务器

    使用ssh工具,进入命令行,安装下面的就是可以配置turn-server(coturn) 转请注明出处. 1.安装centos必须的库文件      yum install -y make gcc c ...

  2. spark实验(二)--scala实验(3)

    实验1,计算级数: 首先打开安装完scala ide的eclipse,在eclipse 中新建一个scala project. 然后新建一个scala的object对象 导入scala.io.StdI ...

  3. 开源沙箱CuckooSandbox 介绍与部署

    1. 介绍 1.1应用  在工作中很多时候需要自己对一些可以程序,可执行文件进行检测,当然我们可以通过VT,微步,等一些开源的平台进行检测.现在我们通过自己搭建的开源的沙箱进行检测.所谓沙箱,是分离运 ...

  4. 【代码学习】PYTHON 异常处理

    一.什么是异常 在程序执行过程中可能会影响程序的正常执行,一般情况下,在python无法正常处理程序时就会发生一个异常 当python脚本发生异常时我们需要捕获处理他,否则程序会终止执行 二.异常处理 ...

  5. node vue 项目git 管理

    push 上传到云的时候,依赖包及相关文件是不上传上去的, 所以每次克隆到本地后,node 项目运行前须要 npm install 安装对应依赖 vue 项目编译前也须要  npm install,安 ...

  6. c++ 读取、输出txt文件

    下面这段话转自:https://blog.csdn.net/lightlater/article/details/6326338 关于文本文件的文件头 第一 ANSI文件的文件头为空,不需要处理: 第 ...

  7. 零基础入门深度学习(6) - 长短时记忆网络(LSTM)

    代码: def forward(self, x): ''' 根据式1-式6进行前向计算 ''' self.times += 1 # 遗忘门 fg = self.calc_gate(x, self.Wf ...

  8. VUE 鼠标右键自定义

    需要在区域内右击自定义菜单的DIV绑定contextmenu右击事件 <div   style="width:100% ; z-index: inherit;position: rel ...

  9. java 第三次课后作业

    1.java字段初始化的规律 public class gouzao { public static void main(String[] args) { test te=new test(); Sy ...

  10. Codeforces Round #589 (Div. 2)D(思维,构造)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;vector<int>adj[10 ...