1. 程式人生 > >CGL_2_C:Cross Point (幾何簡單基礎模板)

CGL_2_C:Cross Point (幾何簡單基礎模板)

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 s

1 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;
}