POJ 1269 直线位置关系的判断
题目意思:给定四个点,前两个点确定一条直线,后两个点确定一条直线,判断两条直线的位置关系;
思路:
两条直线的位置关系分为三种情况:
1.重合:判断方法:((p1-q1)^(p2-q1))==0&&((p1-q2)^(p2-q2))==0
2.平行:判断方法:(p1-p2).x*(q1-q2).y==(p1-p2).y*(q1-q2).x;
注:不要用斜率判断,会造成不必要的麻烦
3.相交:交点的求解方法:
a.通过变量t将直线p1-p2表示为p1+t(p2-p1),交点又在直线q1-q2上,所以有:
(q2-q1)^(p1+t(p2-p1)-q1)=0;
t=p1+(q2-q1)^(q1-p1)/((q2-q1)^(p2-p1))(p2-p1)
<span style="color:#6600cc;background-color: rgb(255, 255, 255);"></span>
Intersecting Lines
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11006 Accepted: 4952
Description
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
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 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
Mid-Atlantic 1996
<span style="color:#6600cc;">/***************************************** author : Grant Yuan time : 2014/8/19 23:36 algorithm: 判断两条直线的位置关系(重合、平行、相交),若相交,求交点; source : POJ 1269 *******************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #define esp 1e-8 using namespace std; int t; struct Point { double x,y; Point(){} Point(double _x,double _y) { x=_x; y=_y; } Point operator -(const Point&b) { return Point(x-b.x,y-b.y); } Point operator +(const Point&b) { return Point(x+b.x,y+b.y); } double operator *(const Point&b) { return x*b.x+y*b.y; } Point operator *(double b) { return Point(x*b,y*b); } double operator ^(const Point&b) { return x*b.y-y*b.x; } bool operator ==(const Point&b) { return(x==b.x&&y==b.y); } }; struct Line { Point p1,p2; Line(){} Line(Point a,Point b){p1=a;p2=b;} }; Point slove(Point p1,Point p2,Point q1,Point q2) { double tem; tem=((q2-q1)^(q1-p1))/((q2-q1)^(p2-p1)); Point p3; p3=Point(tem*(p2-p1).x,tem*(p2-p1).y); return p1+p3; } Point p1,p2,q1,q2,ans; int main() { scanf("%d",&t); printf("INTERSECTING LINES OUTPUT\n"); while(t--){ scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&q1.x,&q1.y,&q2.x,&q2.y); bool flag; if(((p1-q1)^(p2-q1))==0&&((p1-q2)^(p2-q2))==0) printf("LINE\n"); else if((p1.x-p2.x)*(q1.y-q2.y)==(p1.y-p2.y)*(q1.x-q2.x)) printf("NONE\n"); else {ans=slove(p1,p2,q1,q2); printf("POINT %.2lf %.2lf\n",ans.x,ans.y);}} printf("END OF OUTPUT\n"); return 0; } </span>