Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12960 | Accepted: 5755 |
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
题意:给定两条直线,判断这两条直线的关系,若相交则输出交点。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (50+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Point
{
double x, y;
Point(){}
Point(double X, double Y){
x = X; y = Y;
}
};
Point operator - (Point A, Point B){
return Point(A.x-B.x, A.y-B.y);
}
Point operator + (Point A, Point B){
return Point(A.x+B.x, A.y+B.y);
}
Point operator * (Point A, double p){
return Point(A.x*p, A.y*p);
}
double Cross(Point A, Point B){
return A.x*B.y - A.y*B.x;
}
double Dot(Point A, Point B){
return A.x*B.x + A.y*B.y;
}
int dcmp(double x)
{
if(fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point &a, const Point &b){
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
struct Line
{
Point s, e;
Line(){}
Line(Point S, Point E){
s = S; e = E;
}
};
Line L[MAXN];
int judge(Line A, Line B)
{
if(dcmp(Cross(A.s-A.e, B.s-B.e)) == 0)
{
if(dcmp(Cross(A.s-A.e, A.s-B.s)) == 0)//共线
return 1;
else//平行
return 2;
}
else//相交
return 3;
}
Point Get_Line_Inter(Line A, Line B)
{
Point u = A.s - B.s;
Point va = A.e - A.s, vb = B.e - B.s;
double t = Cross(vb, u) / Cross(va, vb);
return A.s + va * t;
}
int main()
{
int n; Ri(n);
printf("INTERSECTING LINES OUTPUT\n");
while(n--)
{
Rf(L[0].s.x); Rf(L[0].s.y); Rf(L[0].e.x); Rf(L[0].e.y);
Rf(L[1].s.x); Rf(L[1].s.y); Rf(L[1].e.x); Rf(L[1].e.y);
switch(judge(L[0], L[1]))
{
case 1: printf("LINE\n"); break;
case 2: printf("NONE\n"); break;
case 3: Point ans = Get_Line_Inter(L[0], L[1]);
printf("POINT");
printf(" %.2lf %.2lf\n", ans.x, ans.y);
}
}
printf("END OF OUTPUT\n");
return 0;
}