首页 > 代码库 > HDU-2857-Mirror and Light(计算几何)

HDU-2857-Mirror and Light(计算几何)

Problem Description
The light travels in a straight line and always goes in the minimal path between two points, are the basic laws of optics.

Now, our problem is that, if a branch of light goes into a large and infinite mirror, of course,it will reflect, and leave away the mirror in another direction. Giving you the position of mirror and the two points the light goes in before and after the reflection, calculate the reflection point of the light on the mirror.
  
You can assume the mirror is a straight line and the given two points can’t be on the different sizes of the mirror.
 

Input
The first line is the number of test case t(t<=100).
  
The following every four lines are as follow:
  X1 Y1
  X2 Y2
  Xs Ys
  Xe Ye

  (X1,Y1),(X2,Y2) mean the different points on the mirror, and (Xs,Ys) means the point the light travel in before the reflection, and (Xe,Ye) is the point the light go after the reflection.

  The eight real number all are rounded to three digits after the decimal point, and the absolute values are no larger than 10000.0.
 

Output
  Each lines have two real number, rounded to three digits after the decimal point, representing the position of the reflection point.
 

Sample Input
1 0.000 0.000 4.000 0.000 1.000 1.000 3.000 1.000
 

Sample Output
2.000 0.000
 

思路:先求一个点关于镜子的对称点,再求该点与令一点确定的直线与镜子的交点。

#include <stdio.h>

void jd(double a1,double b1,double c1,double a2,double b2,double c2,double &x,double &y)//两直线交点
{
    x=(b2*c1-b1*c2)/(a1*b2-a2*b1);
    y=(a2*c1-a1*c2)/(a2*b1-a1*b2);
}

void line(double x1,double y1,double x2,double y2,double &a,double &b,double &c)//两点确定的直线
{
    a=y1-y2;
    b=x2-x1;
    c=x2*y1-x1*y2;
}

int main()
{
    int T;
    double x1,x2,y1,y2,x0,y0,x3,y3,a1,a2,b1,b2,c1,c2,x,y;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x0,&y0,&x3,&y3);

        a1=x1-x2;//过(x0,y0)垂直与镜子的直线
        b1=y1-y2;
        c1=x0*x1-x0*x2+y0*y1-y0*y2;

        line(x1,y1,x2,y2,a2,b2,c2);//镜子所在直线

        jd(a1,b1,c1,a2,b2,c2,x,y);//(x,y)上面两条直线的交点

        x+=x-x0;
        y+=y-y0;

        line(x,y,x3,y3,a1,b1,c1);

        jd(a1,b1,c1,a2,b2,c2,x,y);

        printf("%.3f %.3f\n",x,y);
    }
}

HDU-2857-Mirror and Light(计算几何)