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. 最常用的CountDownLatch, CyclicBarrier你知道多少? (Java工程师必会)

    CountdownLatch,CyclicBarrier是非常常用并发工具类,可以说是Java工程师必会技能了.不但在项目实战中经常涉及,而且在编写压测程序,多线程demo也是必不可少,所以掌握它们的 ...

  2. Node.js 服务端处理图片

    Node 服务端处理图片 服务端进行图片处理是很常见的需求,但是Node在这一块相对来说比较薄弱.找了几个比较常见的模块来解决问题. gm GraphicsMagick for node 使用Open ...

  3. DVWA实验之Brute Force(暴力破解)- Low

    DVWA实验之Brute Force-暴力破解- Low     这里开始DVWA的相关实验~   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2 ...

  4. JS bind()方法、JS原生实现bind()

    一.arguments的含义 // arguments 是一个对应于传递给函数的参数的类数组对象 function a(){ console.log(arguments); } a(); // Arg ...

  5. 3_08_MSSQL课程_Ado.Net_子查询

    子查询 1.把一个查询结果作为一个表来使用,就是子查询. 2.把一个查询结果作为一个 表达式进行使用就是子查询. (分页Sql)

  6. webpack原理类型问题

    1.webpack底层原理 (实现一个webpack) 步骤:1.拿到入口文件的代码并读出来转化为js对象(抽象语法术parser)2.拿到所有模块的依赖 ‘./message.js’,放进数组中 引 ...

  7. SMBUS与I2C

    SMBUS(系统管理总线)基于I2C总线,主要用于电池管理系统中.它工作在主/从模式:主器件提供时钟,在其发起一次传输时提供一个起始位,在其终止一次传输时提供一个停止位:从器件拥有一个唯一的7或10位 ...

  8. vue项目中vant tab改变标签颜色

    找了几种方法,只有下面这个方法是生效的: <van-tabs v-model="active" sticky title-active-color="#144a9e ...

  9. JAVA 常用包

    JAVA是以包的形式进行语言结构组织的. 引入这些包的关键词就是 import 下面说说 JAVA常用包有下面的几个 1. java.lang 这个是默认引入的,也是一个最基础的包.其中lang不是中 ...

  10. PAT T1011 Cut Rectangles

    大模拟题,按要求建立多边形,先定位斜边的位置,再分类讨论~ #include<bits/stdc++.h> using namespace std; ; struct node { dou ...