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. 使用js制作 下拉选择日期列表 (即日期选择器)

    上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  2. CSS中 - display: inline-block

    参考 https://stackoverflow.com/questions/9189810/css-display-inline-vs-inline-block An inline-block el ...

  3. QLabel设置伙伴关系和快捷键(Alt+首字母)

    Qlabe中设置伙伴关系是使用Setbuddy函数: Qlabel.Setbuddy(QLineEdit) #将Qlabel和QLineEdit之间设置伙伴关系 另外,需要配合热键(快捷键)进行使用, ...

  4. bat获取当前路径

    @echo off setlocal EnableDelayedExpansion echo 当前正在运行的批处理文件所在路径:!cd! pause @echo off echo 当前目录是:%cd% ...

  5. 同一条sql insert 有时快有时慢 引发的血案

    同一条sql语句,为什么有时插入块,有时插入慢原因剖析 背景:同一条sql ,有时插入时间几毫秒,有时插入时间几十毫秒,为什么? Sql角度:简单insert 表角度: 一个主键 系统参数角度: 开启 ...

  6. base64,base32bit加密解密

    import base64 str='admin' str=str.encode('utf-8') #加密 bs64=base64.b64encode(str) #解密 debs64=base64.b ...

  7. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  8. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  9. FreeSWITCH 加载模块过程解读

    今天来学习FreeSWITCH 加载模块过程. 哪些模块需要编译,是由源码下的 modules.conf 文件决定的. 哪些模块在程序启动时自动加载,是由 freeswitch/conf/autolo ...

  10. ava StringTokenizer 类使用方法

    菜鸟教程 -- 学的不仅是技术,更是梦想! 首页 笔记首页 Android 互联网 程序员人生 程序员笑话 编程技术 红包 知识店铺 --> 首页 Android 鸡汤 逗乐 Search Ja ...