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


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

  • 相关阅读:
    Vue项目和微信小程序项目的区别与比较
    在Vue中应该如何封装Axios 管理API接口
    Vue2.x 项目踩坑笔记
    微信小程序日常踩坑笔记
    面试题之JavaScript 请写一个深度克隆的函数
    面试题之JavaScript 请编写实现一个对js类型检测函数(支持检测基本类型,对象,函数,正则,时间等)
    面试题之 HTML && CSS && JavaScript 总结
    面试题之JavaScript 有一个url 'http://www.youdao.com/newcard.html?sid=50&a=5&add=9&type=all',请写一个获取url中'?'后制定参数值的函数
    面试题之JavaScript 请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组。
    面试题之JavaScript 正则相关题
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697785.html
Copyright © 2011-2022 走看看