专题:计算几何学 线段相交 hdu1086
来源:互联网 发布:四年级奥数最优化问题 编辑:程序博客网 时间:2024/05/16 14:29
- 题目
- 思路
- 代码如下
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
代码链接:
https://github.com/fangtanchen/Learning/tree/master/1_Algorithm/hdu/ACM_STEP/ch7/s1/2_segment_intersection
题目
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
思路
- 简单题目
- 这题的数据严谨,过不了不必担忧
- 利用A线段如果与B线段相交,A线段2端点在B线段两边,B线段2端点在A线段两边的性质求解
- 利用向量之间的叉积
代码如下
#include<iostream>#include<cstdio>#include<cstring>//#include<vector>#include<algorithm>#include<cmath>#ifdef L_JUDGE#pragma warning(disable:4996)#endifusing namespace std;const int MAXN=110;struct Node{ double x,y; Node(double a=0,double b=0){ x=a;y=b; }};Node pt[MAXN][2];int N;double Direction(const Node &p1,const Node &p2,const Node &p3){ return (p3.x-p1.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p3.y-p1.y);}bool OnSegment(const Node &p1,const Node &p2,const Node &p3){ double maxx=max(p1.x,p2.x); double minx=min(p1.x,p2.x); double maxy=max(p1.y,p2.y); double miny=min(p1.y,p2.y); return (p3.x>=minx)&&(p3.x<=maxx) &&(p3.y<=maxy)&&(p3.y>=miny);}/*bool OnSegment(const Node &p1,const Node &p2,const Node &p3){ double maxx=max(p1.x,p2.x); double minx=min(p1.x,p2.x); double maxy=max(p1.y,p2.y); double miny=min(p1.y,p2.y); return (p3.x>minx)&&(p3.x<maxx) &&(p3.y<maxy)&&(p3.y>miny);}*/int SegIntersect(const Node &p1,const Node &p2, const Node &p3,const Node &p4){ double d1=Direction(p1,p2,p3); double d2=Direction(p1,p2,p4); double d3=Direction(p3,p4,p1); double d4=Direction(p3,p4,p2); if((d1*d2<=0)&&(d3*d4<=0))return 1; return 0;}int main(){ #ifdef L_JUDGE freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout); #endif while(scanf("%d",&N),N){ int ans=0; for(int i=0;i<N;i++){ scanf("%lf%lf%lf%lf",&pt[i][0].x,&pt[i][0].y,&pt[i][1].x,&pt[i][1].y); for(int j=0;j<i;j++){ ans+=SegIntersect(pt[i][0],pt[i][1],pt[j][0],pt[j][1]); } } printf("%d\n",ans); } #ifdef L_JUDGE fclose(stdin); fclose(stdout); system("out.txt"); #endif return 0;}
- 专题:计算几何学 线段相交 hdu1086
- HDU1086--线段相交
- hdu1086(线段相交)
- hdu1086【线段相交】
- hdu1086判读线段相交
- HDU1086-线段相交点个数
- HDU1086(判断两线段是否相交)
- 【计算几何】线段相交
- hdu1086计算n条线段的交点个数
- 计算几何学
- 计算几何学
- 计算几何学
- 专题:计算几何学 (多边形的中心) hdu1115Lifting the Stone
- 专题:计算几何学 凸包 hdu2108 shape of hdu
- 专题 计算几何学 凸包 hdu1392Surround the Trees
- CUGB计算几何专题:B - A Round Peg in a Ground Hole判断线段相交
- HDU1086:You can Solve a Geometry Problem too(线段相交模板)
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
- css技巧
- Flex入门基础——Flex布局、容器container的属性
- wamp安装配置常见问题
- EHCache分布式缓存集群环境配置
- 博客搬家
- 专题:计算几何学 线段相交 hdu1086
- HTML入门
- iOS开发Debug之Xcode 8的Swift版本不兼容
- 自建网页项目-蚁族财富网-记录7
- spfa判断负环
- ajax
- Linux系统命令及其详解4
- P1091 合唱队形题解NOIP2004
- Servlet基础