POJ 1269 Intersecting Lines

点击打开链接

题目大意:

给N组数据

每组两条直线

输出交点POINT

平行NONE

重合LINE

 

 

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const double eps=1e-7;//精度
const int INF=1<<29;
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-(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);}
int doublecmp(double x){//判断double等于0或。。。
	if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
bool operator==(const Point&a,const Point&b){
	return doublecmp(a.x-b.x)==0&&doublecmp(a.y-b.y)==0;
}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}//|a|*|b|*cosθ 点积
double Length(Vector a){return sqrt(Dot(a,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){//向量旋转rad弧度
	return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
Vector Normal(Vector a){//计算单位法线
	double L=Length(a);
	return Vector(-a.y/L,a.x/L);
}
Point GetLineProjection(Point p,Point a,Point b){
	Vector v=b-a;
	return a+v*(Dot(v,p-a)/Dot(v,v));
}
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;
}
//直线P+tV
//--------------------------------------

int main(){
	puts("INTERSECTING LINES OUTPUT");
	int T;
	scanf("%d",&T);
	while(T--){
		Point x,y,p,q;
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x.x,&x.y,&y.x,&y.y,&p.x,&p.y,&q.x,&q.y);
		Vector v,w,w2;
		v=y-x;
		w=q-p;
		w2=p-q;
		if(Normal(v)==Normal(w)||Normal(v)==Normal(w2)){
			if(x==GetLineProjection(x,p,q)) puts("LINE");
			else puts("NONE");
		}
		else{
			Point ans=GetLineIntersection(x,v,p,w);
			printf("POINT %.2lf %.2lf\n",ans.x,ans.y);
		}
	}
	puts("END OF OUTPUT");
	return 0;
}


 

你可能感兴趣的:(POJ 1269 Intersecting Lines)