Cross Point
For given two segments s1 and s2, print the coordinate of the cross point of them.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print the coordinate of the cross point. The output values should be in a decimal fraction with an error less than 0.00000001.
Constraints
- 1 ≤ q ≤ 1000
- -10000 ≤ xpi, ypi ≤ 10000
- p0≠p1 and p2≠p3.
- The given segments have a cross point and are not in parallel.
Sample Input
3
0 0 2 0 1 1 1 -1
0 0 1 1 0 1 1 0
0 0 1 1 1 0 0 1
Sample Output
1.0000000000 0.0000000000
0.5000000000 0.5000000000
0.5000000000 0.5000000000
我们可以根据点到直线的距离计算出d1,和d2可以根据三角的相似性我们可以知道
d1:d2=|ck|:|dK|
因此我们可以求出|cK| 这里我们设 t=|ck| / |cd| 我们就可以求出K点的值了
K=a+cd*t
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define clr(a) memset(a,b,sizeof(a))
#define il inline
#define reg register
typedef double db;
typedef long long ll;
const int maxn=10000;
const int minn=100;
const db eps=1e-10;
il db add(db a,db b)
{
if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
return a+b;
}
struct Point
{
db x,y;
Point (){}
Point (db x,db y):x(x),y(y){}
il Point operator + (Point a)
{
return Point(add(x,a.x),add(y,a.y));
}
il Point operator - (Point a)
{
return Point(add(x,-a.x),add(y,-a.y));
}
il Point operator *(double d)
{
return Point(x*d,y*d);
}
il db dot(Point a)//内积
{
return add(x*a.x,y*a.y);
}
il db det(Point a)//外积
{
return add(x*a.y,-y*a.x);
}
il db dis(Point b)//点到点的距离平方
{
return (*this-b).dot(*this-b);
}
};
class Seg
{
private:
Point a,b;
public:
Seg(){}
Seg(Point a,Point b):a(a),b(b){}
il int on_seg(Seg* ot,Point p)//判断点p是否在线段p1,p2上
{
return (ot->a-p).det(ot->b-p)==0&&(ot->a-p).dot(ot->b-p)<=0;
}
il int isOrthcross(Seg ot)//判断两直线是否相交
{
db d1=(b-a).det(ot.a-a),d2=(b-a).det(ot.b-a);
db d3=(ot.b-ot.a).det(a-ot.a),d4=(ot.b-ot.a).det(b-ot.a);
if(d1*d2<0&&d3*d4<0){return 1;}
if(d1==0&&on_seg(this,ot.a)){return 1;}
if(d2==0&&on_seg(this,ot.b)){return 1;}
if(d3==0&&on_seg(&ot,this->a)){return 1;}
if(d4==0&&on_seg(&ot,this->b)){return 1;}
return 0;
}
il int isOrthogonal(Seg ot)//判断两向量关系
{
if((ot.a-ot.b).dot(this->a-this->b)==0.0){return 1;}//垂直
if((ot.a-ot.b).det(this->a-this->b)==0.0){return 2;}//平行
return 0;
}
il Point inter(Seg ot)//两直线的交点
{
return this->a + (this->b - this->a) *
((ot.b -ot.a).det(ot.a - this->a) / (ot.b - ot.a).det(this->b - this->a));
}
il Point footPoint(Point ot)//点到向量的垂足
{
db r=(ot-this->a).dot(this->b-this->a)/this->a.dis(b);
return this->a+(this->b-this->a)*r;
}
il db disPoint(Point ot)//点到直线的距离
{
return sqrt((ot-this->footPoint(ot)).dot(ot-this->footPoint(ot)));
}
il db disP_S(Point ot)//点到线段的距离
{
db r1=(ot-this->a).dot(this->b-this->a)/this->a.dis(b);
if(r1<0.0){return sqrt(ot.dis(this->a));}
if(r1>1.0){return sqrt(ot.dis(this->b));}
return this->disPoint(ot);
}
il db disSeg(Seg ot)
{
if(isOrthcross(ot)){return 0;}
return min(min(this->disP_S(ot.a),this->disP_S(ot.b)),
min(ot.disP_S(this->a),ot.disP_S(this->b)));
}
} ;
int main()
{
Point a,b;
int q;
scanf("%d",&q);
while(q--)
{
scanf("%lf%lf",&a.x,&a.y);
scanf("%lf%lf",&b.x,&b.y);
Seg A(a,b);
scanf("%lf%lf",&a.x,&a.y);
scanf("%lf%lf",&b.x,&b.y);
Seg B(a,b);
printf("%.10f %.10f\n",A.inter(B).x,A.inter(B).y);
}
return 0;
}