HDU 2857 Mirror and Light(几何)

题目链接

简单求反射点。。。这种几何题,写成程序怎么这么容易错啊。4Y。。。中间各种小错误。。

#include <stdio.h>

#include <string.h>

#define eps 0.00000001

double xmi,ymi,xx,yy;

void line(double x1,double y1,double x2,double y2,double xs,double ys)//求(xs,ys)在直线上的对称点

{

    double k1,k2,b1,b2;

    if(y2 == y1)

    {

        xmi = xs;

        ymi = 2*y1 - ys;

    }

    else if(x2 == x1)

    {

        xmi = 2*x1 - xs;

        ymi = ys;

    }

    else

    {

        k1 = (y2-y1)/(x2-x1);

        k2 = (x1-x2)/(y2-y1);

        b1 = y2 - k1*x2;

        b2 = ys - k2*xs;

        xmi = 2*(b1-b2)/(k2-k1) - xs;//求对称点的x坐标

        ymi = k2*(xmi- xs) + ys;//带入垂直直线求y

    }

}

void  inst(double xs,double ys,double xe,double ye,double x1,double y1,double x2,double y2)//求两条直线的交点。

{

    double k1,k2,b1,b2;//两条直线不存在平行的情况

    if(xe == xs)

    {

        k2 = (y2-y1)/(x2-x1);

        b2 = y2 - k2*x2;

        xx = xe;

        yy = k2*xx + b2;

        return ;

    }

    if(x2 == x1)

    {

        k1 = (ye-ys)/(xe-xs);

        b1 = ys - k1*xs;

        xx = x2;

        yy = k1*xx + b1;

        return ;

    }

    k1 = (ye-ys)/(xe-xs);

    k2 = (y2-y1)/(x2-x1);

    b1 = ys - k1*xs;

    b2 = y2 - k2*x2;

    xx = (b1-b2)/(k2-k1);

    yy = k1*xx + b1;

}

int main()

{

    double x1,y1,x2,y2,xs,ys,xe,ye;

    int t;

    scanf("%d",&t);

    while(t--)

    {

        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&xs,&ys,&xe,&ye);

        line(x1,y1,x2,y2,xs,ys);

        inst(xmi,ymi,xe,ye,x1,y1,x2,y2);

        printf("%.3lf %.3lf\n",xx+eps,yy+eps);

    }

    return 0;

}

/*
查错的数据 2 1 1 0 0 0 1 -1 0 1 1 0 0 0 1 1 2 */

  

你可能感兴趣的:(ror)