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. ax绘图相关的知识点

    1.去边框 # 去掉上.下.左.右边框 ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.sp ...

  2. 【代码审计】appcms 文件包含漏洞

    index.php的开头系统都做了过滤 一个是 htmlspecialchars($v), 另一个是/^[\x{4e00}-\x{9fa5}\w {0}]+$/u. 前一个过滤是把预定义的字符 &qu ...

  3. node express 应用笔记关键点

    1.处理客户端 POST数据(标签 <form>), req.body 对象 app.js var bodyParser = require('body-parser'); ... ... ...

  4. linux和windows系统的区别

    在21世纪的今天,互联网可以说是当代发展最为迅速的行业,举个很简单的例子,现在的我们不论什么年龄阶层,几乎人手都有一部手机,上面的某博,某音,末手等软件,更是受到多数人的热爱,并且人们不仅仅用其来消遣 ...

  5. [ DLPytorch ] 文本预处理&语言模型&循环神经网络基础

    文本预处理 实现步骤(处理语言模型数据集距离) 文本预处理的实现步骤 读入文本:读入zip / txt 等数据集 with zipfile.ZipFile('./jaychou_lyrics.txt. ...

  6. RestTemplate post请求使用map传参 Controller 接收不到值的解决方案 postForObject方法源码解析.md

    结论 post方法中如果使用map传参,需要使用MultiValueMap来传递 RestTemplate 的 postForObject 方法有四个参数 String url => 顾名思义 ...

  7. vue 之 axios Vue路由与element-UI

    一. 在组件中使用axios获取数据 1. 安装和配置axios 默认情况下,我们的项目中并没有对axios包的支持,所以我们需要下载安装. 在项目根目录中使用 npm安装包 npm install ...

  8. Python 基础之正则之二 匹配分组,正则相关函数及表达式修饰符

    四.匹配分组   [元字符] 分组符号 a|b   匹配字符a 或 字符b  (如果两个当中有重合部分,把更长的那个放前面) (ab)   匹配括号内的表达式 ,将()作为一个分组 num  引用分组 ...

  9. vue element 时间选择器设置禁用日期

    在 el-date-picker 组件中有一个 picker-options 属性 disabledDate 可以设置日期的可选范围 <el-date-picker v-model=" ...

  10. [网络转载 ]LoadRunner技巧之THML与URL两种录制模式分析

    loadrunner自带网站的访问 Html_based script模式 Action() { web_url("WebTours", "URL=http://127. ...