| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 6233 | Accepted: 2939 |
Description
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
Input
Output
Sample Input
5 0 0 4 4 0 4 4 0 5 0 7 6 1 0 2 3 5 0 7 6 3 -6 4 -3 2 0 2 27 1 5 18 5 0 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT
Source
求俩直线关系的,相交,平行,或重合。
基础的计算几何~一点点建立起计算几何的自信~
推了下求交点的公式
首先看图

两直线AB,CD相交于O点
设q = 向量AC与向量DC的叉积 = AG * CD
p = 向量AB与向量DC的叉积 = (AG + BH) * CD
得AG / (AG + BH) = q / p
因为BHO和AGO是相似三角形,所以
得 AO / AB = AO / (AO + OB) = AG / (AG + BH) = q / p
于是AO = AB * q / p
即O.x - A.x = (B.x - A.x) * q / p
O.y - A.y = (B.y - A.y) * q / p
剩下的
两直线重合:
C,D点一定在AB上,所以,(X表示叉积)
CB X AB = DB X AB = 0
两直线平行:
AB X CD = 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const double eps = 0.000001;
struct LPoint{
double x, y;
};
struct LLine{
LPoint bg, ed;
};
int dblcmp(double a, double b){
if (a - b > eps) return 1;
if (b - a > eps) return -1;
return 0;
}
double VecPro(double a, double b, double p, double q){
return (a * q) - (b * p);
}
int main(){
int n;
double p, q, t;
LLine a, b;
LPoint o;
scanf("%d", &n);
printf("INTERSECTING LINES OUTPUT\n");
while(n--){
scanf("%lf%lf%lf%lf", &a.bg.x, &a.bg.y, &a.ed.x, &a.ed.y);
scanf("%lf%lf%lf%lf", &b.bg.x, &b.bg.y, &b.ed.x, &b.ed.y);
p = VecPro(a.bg.x - a.ed.x, a.bg.y - a.ed.y, b.bg.x - a.ed.x, b.bg.y - a.ed.y);
q = VecPro(a.bg.x - a.ed.x, a.bg.y - a.ed.y, b.ed.x - a.ed.x, b.ed.y - a.ed.y);
t = VecPro(a.bg.x - a.ed.x, a.bg.y - a.ed.y, b.bg.x - b.ed.x, b.bg.y - b.ed.y);
if (dblcmp(p, 0) == 0 && dblcmp(q, 0) == 0){
printf("LINE\n");
}else if (dblcmp(t, 0) == 0){
printf("NONE\n");
}else{
p = VecPro(b.ed.x - b.bg.x, b.ed.y - b.bg.y, a.bg.x - b.bg.x, a.bg.y - b.bg.y);
p = p / t;
o.x = a.bg.x + p * (a.ed.x - a.bg.x);
o.y = a.bg.y + p * (a.ed.y - a.bg.y);
printf("POINT %0.2f %0.2f\n", o.x, o.y);
}
}
printf("END OF OUTPUT\n");
return 0;
}

本文介绍了一种通过计算几何方法来确定二维平面上两条直线相交情况的算法。包括判断两条直线是否相交于一点、平行或者完全重合的情况,并给出具体的计算公式和C语言实现代码。
241

被折叠的 条评论
为什么被折叠?



