zoukankan      html  css  js  c++  java
  • [ACM] 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 

    Source

    2009 Multi-University Training Contest 5 - Host by NUDT

    模板:

    const double eps=1e-6;
    struct point
    {
        double x,y;
    };
    struct line//直线ax+by+c=0
    {
        double a,b,c;
    };
    line LineFromSegment(point p1,point p2)//两个点求直线
    {
        line temp;
        temp.a=p2.y-p1.y;
        temp.b=p1.x-p2.x;
        temp.c=p2.x*p1.y-p1.x*p2.y;
        return temp;
    }
    point LineInter(line l1,line l2)//两条直线求交点
    {
        point temp;
        double a1=l1.a;
        double b1=l1.b;
        double c1=l1.c;
        double a2=l2.a;
        double b2=l2.b;
        double c2=l2.c;
        if(fabs(b1)<eps)
        {
            temp.x=-c1/a1;
            temp.y=(-c2-a2*temp.x)/b2;
        }
        else
        {
            temp.x=(c1*b2-b1*c2)/(b1*a2-b2*a1);
            temp.y=(-c1-a1*temp.x)/b1;
        }
    
        return temp;
    }
    point symmetrical(point p, line L)//求一个点关于一条直线的对称点
    {
        point p2;
        double d;
        d = L.a * L.a + L.b * L.b;
        p2.x = (L.b * L.b * p.x - L.a * L.a * p.x -
                2 * L.a * L.b * p.y - 2 * L.a * L.c) / d;
        p2.y = (L.a * L.a * p.y - L.b * L.b * p.y -
                2 * L.a * L.b * p.x - 2 * L.b * L.c) / d;
        return p2;
    }


    本题代码:

    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    const double eps=1e-6;
    struct point
    {
        double x,y;
    };
    struct line//直线ax+by+c=0
    {
        double a,b,c;
    };
    line LineFromSegment(point p1,point p2)//两个点求直线
    {
        line temp;
        temp.a=p2.y-p1.y;
        temp.b=p1.x-p2.x;
        temp.c=p2.x*p1.y-p1.x*p2.y;
        return temp;
    }
    point LineInter(line l1,line l2)//两条直线求交点
    {
        point temp;
        double a1=l1.a;
        double b1=l1.b;
        double c1=l1.c;
        double a2=l2.a;
        double b2=l2.b;
        double c2=l2.c;
        if(fabs(b1)<eps)
        {
            temp.x=-c1/a1;
            temp.y=(-c2-a2*temp.x)/b2;
        }
        else
        {
            temp.x=(c1*b2-b1*c2)/(b1*a2-b2*a1);
            temp.y=(-c1-a1*temp.x)/b1;
        }
    
        return temp;
    }
    point symmetrical(point p, line L)//求一个点关于一条直线的对称点
    {
        point p2;
        double d;
        d = L.a * L.a + L.b * L.b;
        p2.x = (L.b * L.b * p.x - L.a * L.a * p.x -
                2 * L.a * L.b * p.y - 2 * L.a * L.c) / d;
        p2.y = (L.a * L.a * p.y - L.b * L.b * p.y -
                2 * L.a * L.b * p.x - 2 * L.b * L.c) / d;
        return p2;
    }
    int main()
    {
        int t;
        cin>>t;
        for(int i=1;i<=t;i++)
        {
            point p[4];
            for(int j=0;j<4;j++)
                cin>>p[j].x>>p[j].y;
            point tmp;
            line l1,l2;
            l1=LineFromSegment(p[0],p[1]);//两点求直线
            tmp=symmetrical(p[3],l1);//直线外一点与一条直线,求该点关于直线的对称点,这里是求的反射点的对称点
            l2=LineFromSegment(tmp,p[2]);//对称点与入射点求直线
            tmp=LineInter(l1,l2);//求反射点(两条直线相交的点)
            cout<<setiosflags(ios::fixed)<<setprecision(3)<<tmp.x<<" "<<tmp.y<<endl;
        }
        return 0;
    }
    


    辅助图(根据所写代码画的)

  • 相关阅读:
    ubuntu server 14.04和18.04挂载vmware共享文件夹
    Ubuntu 无法进行SSH连接,开启22端口
    ubuntu切换到root用户
    VMware Workstation 15 Pro 永久激活密钥
    idea静态资源的访问问题,如HTML,css,js的加载
    idea在Tomcat服务器加载html文件出现乱码的解决方案
    html,js 打开时出现 Uncaught TypeError: Cannot read property 'addEventListener' of null at register.js:24错误的解决方法
    js判断input输入是不是含有中文,或者判断输入是不是全是中文
    PHP连接前端from数据的错误,如源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。
    PHP与Tomcat运行前的配置。
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697785.html
Copyright © 2011-2022 走看看