Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10749 | Accepted: 4797 |
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.
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
<pre name="code" class="cpp">#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
typedef unsigned long long ull;
typedef double PointType;
struct point{
PointType x,y;
point(double x=0,double y=0):x(x),y(y) {}
};
typedef point Vector ;
int dcmp(double x){
if(fabs(x) < eps) return 0;else return x<0 ?-1:1;
}
Vector operator + (Vector A , Vector B){
return Vector(A.x + B.x , A.y + B.y);
}
Vector operator - (point A , point B){
return Vector(A.x - B.x , A.y - B.y);
}
Vector operator * (Vector A , double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A , double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const point& a, const point& b){
return a.x<b.x||(a.x==b.x && a.y<b.y);
}
bool operator == (const point& a, const point& b){
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double Dot(Vector A , Vector B){
return A.x*B.x+A.y*B.y;
}
double Length(Vector A){
return sqrt(Dot(A,A));
}
double Cross(Vector A , Vector B){
return A.x*B.y - A.y*B.x;
}
double Angle(Vector A , Vector B){
return acos(Dot(A, B) / Length(A) / Length(B));
}
point jd;
point GetLineIntersection(point P, Vector v, point Q, Vector w){
Vector u = P-Q;
double t = Cross(w,u)/Cross(v,w);
return P+v*t;
}
int Jiaodian(point a,point b,point c,point d)//平行返回0 重合返回1 相交返回2
{
double A1=b.y-a.y,A2=d.y-c.y,B1=a.x-b.x,B2=c.x-d.x;
double C1=b.y*(b.x-a.x)-b.x*(b.y-a.y),C2=d.y*(d.x-c.x)-d.x*(d.y-c.y);
if(A1*B2==B1*A2)//平行或重合
{
if(A2*C1==A1*C2&&B1*C2==B2*C1)
return 1;
return 0;
}
jd=GetLineIntersection(a,a-b,c,c-d);
return 2;
}
int main()
{
point a,b,c,d;
int n;
printf("INTERSECTING LINES OUTPUT\n");
scanf("%d",&n);
while(n--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
int ans=Jiaodian(a,b,c,d);
if(ans==0)
puts("NONE");
else if(ans==1)
puts("LINE");
else
printf("POINT %.2f %.2f\n",jd.x,jd.y);
}
printf("END OF OUTPUT\n");
return 0;
}