poj 1269 Intersecting Lines

这个题是一个几何题,主要考虑精度问题,为了减少精度的损失,尽量少用除法,该题如果先算出斜率k,那么后面就会精度损失;

该题最好用向量解比较好;该题要注意两直线垂直的情况;poj上面的测试数据太弱了;

#include<stdio.h>
#include<stdlib.h>
int main( )
{
    double A1,B1,A2,B2,X1,Y1,X2,Y2;
    double PX,PY,QX,QY;
    int n;
    scanf("%d",&n);
    printf("INTERSECTING LINES OUTPUT\n");
    for(int i=0;i<n; i++)
    {
          scanf("%lf%lf%lf%lf",&A1,&B1,&A2,&B2);
          scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
          PX=A1-A2;PY=B1-B2;
          QX=X1-X2;QY=Y1-Y2;
          if( (PX*QY-PY*QX)==0 ) // 平行
          {
              if( PX==0 )  //垂直情况

                 if( A1==X1 )   //共线的情况
                   printf( "LINE\n" );
                  else printf("NONE\n");
               else
               {
                   if( PY==0 )  //垂直情况
                    if( B1==Y1 )//共线的情况
                    printf( "LINE\n" );
                    else  printf("NONE\n");
                    else
                       {
                           if( ( B1-PY/PX*A1 )==( Y1-QY/QX*X1 ) )
                           printf( "LINE\n" );
                           else printf("NONE\n");
                       }   
               }   
          }
          else
          {
              if( PX==0 ) //垂直情况
              {
                  printf( "POINT %.2lf %.2lf\n",A1,(A1-X1)*QY/QX + Y1 );
              }
              else
              {
              if( QX==0 ) //垂直情况
              {
                  printf( "POINT %.2lf %.2lf\n",X1,(X1-A1)*PY/PX + B1 );
              }
              else
              {
                   double t1=( QX*(PX*Y1+PY*A1)-PX*(QX*B1+QY*X1))/(QX*PY-PX*QY);  //交点X的值
                   double t2=(( PY*(A1*QY+QX*Y1-QY*X1)-PX*B1*QY)/(QX*PY-PX*QY) ); //交点Y的值
                   printf("POINT %.2lf %.2lf\n",t1,t2 );
              }
              }   
          } 
    }
    printf("END OF OUTPUT\n");
    return 0;
}

你可能感兴趣的:(intersect)