码迷,mamicode.com
首页 > 其他好文 > 详细

Mirror and Light (点与直线)

时间:2014-08-26 09:54:55      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:计算几何

Mirror and Light

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 821    Accepted Submission(s): 387


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

直接套模板;
#include <cstdio>
#include <algorithm>
#include <iostream>

using namespace std;

struct Point
{
    double x,y;
    Point(double x = 0, double y = 0) : x(x),y(y) {};
    void input()
    {
        scanf("%lf %lf",&x,&y);
    }
} point[5];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i = 0; i < 4; i++)
            point[i].input();
        double a1 = point[1].y - point[0].y;
        double b1 = point[0].x - point[1].x;
        double c1 = point[0].y * point[1].x - point[0].x * point[1].y;
        double a2,b2,c2;
        double x,y,k;
        k = -2.0 * (a1 * point[2].x + b1 * point[2].y + c1) / (a1 * a1 + b1 * b1);
        x = point[2].x + k * a1;
        y = point[2].y + k * b1;
        a2 = point[3].y - y;
        b2 = x - point[3].x;
        c2 = y * point[3].x - point[3].y * x;
        double x1 = (c2 * b1 - c1 * b2) / (a1 * b2 - a2 * b1);
        double y1 = (c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1);
        if(b1 == 0) x1 = point[0].x;//其实不用考虑这两种情况也能AC,因为不考虑也不影响结果;
        if(a1 == 0) y1 = point[0].y;
        printf("%.3lf %.3lf\n",x1,y1);
    }
    return 0;
}


Mirror and Light (点与直线)

标签:计算几何

原文地址:http://blog.csdn.net/zsgg_acm/article/details/38834619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!