Intersecting LinesPOJ - 1269
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
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
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.
The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).
Output
There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF 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 5Sample Output
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT
分析:
判断两条直线的关系:
1.不求方程的话,必须知道直线上的两个点,比如line1(p1,p2),line2(p3,p4)
那么,用叉乘判断p1,p2相对p3p4的关系,如果两次叉乘的结果都是0,那么重合,如果两次的结果都不为0,但是相等,由于叉乘可以用平行四边形的有向面积来定义,所以这个时候两条直线平行,如果以上条件都不满足,那么就是相交的,但是求交点的话,就必须使用方程法
2.先说已知两点求直线方程
若有p1(x1,y1),p2(x2,y2),那么可以找到A,B,C的一组解
A=y1-y2,B=x2-x1,C=cross(p1,p2)(虽然p1,p2不是向量,但这么写更方便,就把cross当行列式求值好了)
已经求出了A,B,C,再说如何求点
方程组:
A1*x+B1*y=-C1;
A2*x+B2*y=-C2;
用克拉默就可以了,怕写错的话,就设几个point,然后用cross。。。还是觉得很麻烦。。。
代码:
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
using namespace std;
const double eps=1e-8;
struct Vector{
double x,y;
Vector(){}
Vector(double x,double y):x(x),y(y){}
};
typedef Vector point;
Vector operator + (Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);};
Vector operator - (Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);};
Vector operator * (Vector a,Vector b){return Vector(a.x*b.x,a.y*b.y);};
Vector operator / (Vector a,Vector b){return Vector(a.x/b.x,a.y/b.y);};
double dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
struct line{
double a,b,c;
line(){}
line(double a,double b,double c):a(a),b(b),c(c){}
};
line get_l(point p1,point p2){
return line(p1.y-p2.y,p2.x-p1.x,cross(p1,p2));
}
int main(){
int t;cin>>t;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
while(t--){
point p1,p2,p3,p4;
cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y;
Vector v=p3-p4,v1=p1-p4,v2=p2-p4;
if(abs(cross(v,v1))<eps&&abs(cross(v,v2))<eps){cout<<"LINE"<<endl;continue;}
if(cross(v,v1)==cross(v,v2)){cout<<"NONE"<<endl;continue;}
line l1,l2;
l1=get_l(p1,p2);
l2=get_l(p3,p4);
point t1(l1.a,l1.b),t2(l2.a,l2.b),t3(-l1.c,l1.b),t4(-l2.c,l2.b),t5(l1.a,-l1.c),t6(l2.a,-l2.c);
double px=cross(t3,t4)/cross(t1,t2),py=cross(t5,t6)/cross(t1,t2);
cout<<"POINT ";
cout<<fixed<<setprecision(2)<<px<<' '<<py<<endl;
}
cout<<"END OF OUTPUT"<<endl;
return 0;
}