您的位置:首页 > 其它

hdu 2587 Mirror and Light

2016-04-07 16:59 375 查看
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


这道题很简单,坑爹的是最后输出0的时候总是莫名其妙的多出来一个负号,浪费了我一大堆时间;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct point
{
double x, y;
};
double a1, b1, c1, a2, b2, c2;
int main()
{
int t;
point a, b, c, d;
cin >> t;
while (t--)
{
cin >> a.x >> a.y;
cin >> b.x >> b.y;
cin >> c.x >> c.y;
cin >> d.x >> d.y;
a1 = -a.y + b.y, b1 = -b.x + a.x, c1 =-a.x*b.y + b.x*a.y;
point t;
t.x = ((b1*b1 - a1*a1)*c.x - 2 * a1*c1 - 2 * a1*b1*c.y) / (a1*a1 + b1*b1);
t.y = ((a1*a1 - b1*b1)*c.y - 2 * b1*c1 - 2 * a1*b1*c.x) / (a1*a1 + b1*b1);
double x, y;
a2 = d.y - t.y, b2 = t.x - d.x, c2 = d.x*t.y - d.y*t.x;
x = (c2*b1 - c1*b2) / (a1*b2 - a2*b1);
y = (c2*a1 - c1*a2) / (b1*a2 - b2*a1);
if (x == 0) x = 0;
if (y == 0) y = 0;
printf("%.3lf %.3lf\n", x, y);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: