您的位置:首页 > 其它

poj 1269 Intersecting Lines 判断直线的位置关系

2016-08-07 15:30 260 查看
Intersecting Lines

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14447 Accepted: 6373
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
[Submit]   [Go Back]   [Status]  
[Discuss]

两点式求交点:

Point GetLineIntersection2(Point A,Point B,Point C,Point D)
{
double a1=A.y-B.y;
double b1=B.x-A.x;
double c1=A.x*B.y-B.x*A.y;

double a2=C.y-D.y;
double b2=D.x-C.x;
double c2=C.x*D.y-D.x*C.y;

double x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
double y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
return Point(x,y);
}

判断是否平行:

int Parallel(Line c,Point &a)//判断两线关系
{
if(!dcmp(Cross(v,c.v)) )
{
if(!dcmp (Cross(v,c.p-p ) ) ) return 2;//平行
return 1;//重合
}
a=GetLineIntersection2(p, p2, c.p, c.p2);//可以改为点线式
return 0;//交于一点
}

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const double PI=cos(-1.0);
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {};
};
typedef Point Vector;
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,double p) {return Vector(A.x*p,A.y*p); }
Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); }
Vector operator -(Vector A) {return Vector(-A.x,-A.y);}

double torad(double deg)
{
return deg/180*acos(-1.0);
}

const double eps=1e-10;
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.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 Angle(Vector A,Vector B)
{
return acos(Dot(A,B)/Length(A)/Length(B));
}

double Cross(Vector A,Vector B)//叉乘
{
return A.x*B.y-A.y*B.x;
}

double Area2(Point A,Point B,Point C)//平行四边形面积
{
return Cross(B-A,C-A);
}

Vector Rotate(Vector A,double rad)//向量A 逆时针旋转rad度
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad) );
}

Vector Normal(Vector A)//计算A的单位向量
{
double L=Length(A);
return Vector( -A.y/L,A.x/L );
}

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;

}
Point GetLineIntersection2(Point A,Point B,Point C,Point D)
{
double a1=A.y-B.y;
double b1=B.x-A.x;
double c1=A.x*B.y-B.x*A.y;

double a2=C.y-D.y;
double b2=D.x-C.x;
double c2=C.x*D.y-D.x*C.y;

double x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
double y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
return Point(x,y);
}

struct Line
{
Point p,p2;
Vector v;
Line(){}
Line(Point p,Vector v):p(p),v(v){}//点线式
void twoPointIntial(Point p,Point p2)//两点式
{
this->p=p;
this->p2=p2;
v= p2-p;
}
Point point(double t)//参数方程求参数t对应点
{
return p+v*t;
}
int fx(double x,double &y)//求f(x)
{
if(dcmp(v.x) ) {y=p.y+(x-p.x)/v.x*v.y;return 1;}//存在一个值
if(dcmp(x-p.x)) return 0;//不存在值
return 2;//无穷点
}
int Parallel(Line c,Point &a)//判断两线关系
{
if(!dcmp(Cross(v,c.v)) )
{
if(!dcmp (Cross(v,c.p-p ) ) ) return 2;//平行
return 1;//重合
}
a=GetLineIntersection2(p, p2, c.p, c.p2);//可以改为点线式
return 0;//交于一点
}
}li[2];
int main()
{
std::ios::sync_with_stdio(false);
int T;cin>>T;
puts("INTERSECTING LINES OUTPUT");
while(T--)
{
Point p[5];
for0(i,4)
{
cin>>p[i].x>>p[i].y;
}
li[0]. twoPointIntial(p[0],p[1]);
li[1]. twoPointIntial(p[2],p[3]);

int ans=li[0].Parallel(li[1],p[4]);
if(ans==1) puts("NONE");
else if(ans==2) puts("LINE");
else printf("POINT %.2f %.2f\n",p[4].x,p[4].y);

}
puts("END OF OUTPUT");
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: