判断两条直线(线段)的交点问题
2014-11-19 11:49
477 查看
下面给出杭电上的一道题hdu1086.借题来说明一下。
You can Solve a Geometry Problem too
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7770 Accepted Submission(s): 3793
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
首先说明一下这题的数据真的好水。。
#include <iostream> #include <math.h> #include <stdio.h> using namespace std; double f(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x) { double x0,y0; x0 = ((x2-x1)*(x4-x3)*(y3-y1)+(y2-y1)*(x4-x3)*x1-(y4-y3)*(x2-x1)*x3)/(-x); y0 = ((y2-y1)*(y4-y3)*(x3-x1)+(x2-x1)*(y4-y3)*y1-(x4-x3)*(y2-y1)*y3)/(x); //printf(">>%lf%lf<<\n",x0,y0); if((x0-x1)*(x0-x2)<=0 && (x0-x3)*(x0-x4) <=0 && (y0-y1)*(y0-y2) <=0 && (y0-y3)*(y0-y4) <=0) return 1; else return 0; } int main() { int n; int i,j; double x1[102],x2[102],y1[102],y2[102]; while (~scanf("%d",&n)) { if(n == 0) break; int ans = 0; for(i = 0 ; i < n; i++) { scanf("%lf%lf%lf%lf",&x1[i],&y1[i],&x2[i],&y2[i]); } for(i = 0; i < n ;i++) { for(j = i+1; j < n ;j++) { int d = (y2[i]-y1[i])*(x1[j]-x2[j]) - (x2[i]-x1[i])*(y1[j]-y2[j]); // printf(">>%d<<",d); if(d == 0) continue; else { if(f(x1[i],y1[i],x2[i],y2[i],x1[j],y1[j],x2[j],y2[j],d)&&f(x1[j],y1[j],x2[j],y2[j],x1[i],y1[i],x2[i],y2[i],d)) ans++; } } } printf("%d\n",ans); } // cout << "Hello world!" << endl; return 0; }
这题的问题是没有解决交点重合问题。。我还没想好,暂且放在这里,等想出办法来再写。。
给代码加一下备注啊
//首先判断d = (y2-y1)(x4-x3)-(y4-y3)(x2-x1), // 若d=0,则直线AB与CD平行或重合, // 若d!=0,则直线AB与CD有交点,设交点为E(x0,y0): // x0 = [(x2-x1)*(x4-x3)*(y3-y1)+(y2-y1)*(x4-x3)*x1-(y4-y3)*(x2-x1)*x3]/d //y0 = [(y2-y1)*(y4-y3)*(x3-x1)+(x2-x1)*(y4-y3)*y1-(x4-x3)*(y2-y1)*y3]/(-d) /* 求出交点后在判断交点是否在线段上,即判断以下的式子: (x0-x1)*(x0-x2) <=0 (x0-x3)*(x0-x4) <=0 (y0-y1)*(y0-y2) <=0 (y0-y3)*(y0-y4) <=0 只有上面的四个式子都成立才可判定(x0,y0)是线段AB与CD的交点,否则两线段无交点*/
相关文章推荐
- 判断线段与直线交点——noyj 管道问题
- 判断两条线段/直线相交,并求交点
- 判断两条线段/直线相交,并求交点
- poj 2074 Line of Sight(视线问题,求直线与线段的交点及判断相交)
- 求两条直线(线段)的交点
- 判断两条直线 共线或 平行 或相交 求交点 叉乘的应用 poj 1269 Intersecting Lines
- POJ 1039 Pipe(直线和线段相交判断,求交点)
- hdoj4741 求空间中不平行的两条直线的最短距离及最短线段与两直线的交点
- 求两条直线(线段)的交点
- Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)
- 判断线段是否相交的函数和求直线交点的函数
- 求两条直线(线段)的交点
- 判断直线与线段是否相交,相交则输出交点x轴坐标
- 已知两条线段端点,判断是否相交及交点
- POJ 1039 Pipe(直线和线段相交判断,求交点)
- 叉积--求两条直线(线段)的交点
- 两条线段知道端点line1(x1,y1)(x2,y2)line2(x3,y3)(x4,y4),判断两条线段是否相交,交点坐标(x,y)
- hunnu10522(判断两条线段的交点个数)
- poj 1269判断两条直线的位置关系 以及求交点
- poj 1039 Pipe (判断 直线和 线段 是否相交 并 求交点)