Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16680 | Accepted: 7191 |
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
给两条直线判断一些位置关系。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include <iostream>
#include <string>
#include <set>
#include <map>
using namespace std;
const int MAXN = 5000+10;
const int INF=1e9+7;
const double eps=1e-8;
const double pi=acos(-1.0);
//计算几何误差修正
//输入为一个double类型的数,返回-1表示负数,1表示正数,0表示x为0
int cmp(double x){
if(fabs(x)<eps)
return 0;
if(x>0) return 1;
return -1;
}
//计算几何点类
inline double sqr(double x){
return x*x;
}
struct point{
double x,y;
double xx;
point(){}
point(double a,double b):x(a),y(b){}
void input(){
scanf("%lf%lf",&x,&y);
}
//加法
friend point operator + (const point &a,const point &b){
return point(a.x+b.x,a.y+b.y);
}
//减法
friend point operator - (const point &a,const point &b){
return point(a.x-b.x,a.y-b.y);
}
//判断相等
friend bool operator == (const point &a,const point &b){
return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
}
//倍增
friend point operator * (const point &a,const double &b){
return point(a.x*b,a.y*b);
}
//倍增
friend point operator * (const double &b,const point &a){
return point(a.x*b,a.y*b);
}
//除法
friend point operator / (const point &a,const double b){
return point(a.x/b,a.y/b);
}
//模长
double norm(){
return sqrt(sqrt(x)+sqr(y));
}
};
//叉积,a×b>0代表a在b的顺时针方向,<0代表a在b的逆时针方向,等于0代表a和b向量共线,但不确定方向是否相同
double det(const point &a,const point &b){
return a.x*b.y-a.y*b.x;
}
//点积
double dot(const point &a,const point &b){
return a.x*b.x+a.y*b.y;
}
//距离
double dist(const point &a,const point &b){
return (a-b).norm();
}
//op向量绕原点逆时针旋转A(弧度)
point rotate_point(const point &p,double A){
double tx=p.x,ty=p.y;
return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
}
//计算几何线段类
struct line{
point a,b;
line(){}
line(point x,point y):a(x),b(y){}
};
//用两个点a,b生成的一个线段或者直线
line point_make_line(point a,point b){
return line(a,b);
}
//求点p到线段st的距离
double dis_point_segment(point p,point s,point t){
if(cmp(dot(p-s,t-s))<0) return (p-s).norm();
if(cmp(dot(p-s,s-t))<0) return (p-t).norm();
return fabs(det(s-p,t-p)/dist(s,t));
}
//求点p到线段st的垂足,保存在cp中
void PointProjLine(point p,point s,point t,point &cp){
double r=dot((t-s),(p-s))/dot(t-s,t-s);
cp=s+(t-s)*r;
}
//判断p点是否在线段st上
bool PointOnSegment(point p,point s,point t){
return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
}
//判断a和b是否平行
bool parallel(line a,line b){
return !cmp(det(a.a-a.b,b.a-b.b));
}
//判断a和b是否相交,若相交则返回true且交点保存在res中
bool line_make_point(line a,line b,point &res){
if(parallel(a, b))
return false;
double s1=det(a.a-b.a,b.b-b.a);
double s2=det(a.b-b.a,b.b-b.a);
res=(s1*a.b-s2*a.a)/(s1-s2);
return true;
}
//将直线a沿法向量方向平移距离len得到的直线
line move_d(line a,const double &len){
point d=a.b-a.a;
d=d/d.norm();
d=rotate_point(d, pi/2);
return line(a.a+d*len,a.b+d*len);
}
int main(){
int t;
puts("INTERSECTING LINES OUTPUT");
scanf("%d",&t);
while(t--){
line p,q;
p.a.input();
p.b.input();
q.a.input();
q.b.input();
if(parallel(p, q)){
if(!cmp(det(p.b-p.a,q.a-p.b))){
puts("LINE");
}else{
puts("NONE");
}
}else{
point temp;
line_make_point(p, q, temp);
printf("POINT %.2f %.2f\n",temp.x,temp.y);
}
}
puts("END OF OUTPUT");
}