Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
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,(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
10.000 0.0004.000 0.0001.000 1.0003.000 1.000
Sample Output
2.000 0.000
公式:
·已知直线上的两点(x1, y1)(x2, y2),求直线ax+by+c=0
a = y2-y1;b = x1-x2; c = 1*x2-x1*y2;
·已知(x0, y0),寻求直线ax+by+c=0的对称点(x, y)
k = -2 * (a*x0+b*y0+c)/(a*a+b*b);
x = x0 + k*a;
y = y0 + k*b;
·已知直线
与直线,求交点(x, y)假如b1==0
否则
#include<cstdio>#include<string>#include<cmath>#define eps 1e-4using namespace std;struct point { double x,y;};point symmetric_point(point p1, point l1, point l2)// 要求P1的对称点{point ret;if (l1.x > l2.x - eps && l1.x < l2.x + eps)///不存在斜率{ret.x = (2 * l1.x - p1.x);ret.y = p1.y;}else{double k = (l1.y - l2.y ) / (l1.x - l2.x);if(k + eps > 0 && k - eps < 0)///斜率为零{ret.x = p1.x;ret.y = l1.y - (p1.y - l1.y);}else{ret.x = (2*k*k*l1.x + 2*k*p1.y - 2*k*l1.y - k*k*p1.x + p1.x) / (1 + k*k);ret.y = p1.y - (ret.x - p1.x ) / k;}}return ret;}point intersection(point u1,point u2,point v1,point v2)///寻找两条直线的交点{point ret=u1;double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));ret.x+=(u2.x-u1.x)*t;ret.y+=(u2.y-u1.y)*t;return ret;}int main(){int t;scanf("%d",&t);while(t--){point m1,m2,l1,l2;scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&m1.x,&m1.y,&m2.x,&m2.y,&l1.x,&l1.y,&l2.x,&l2.y);point tmp=symmetric_point(l1,m1,m2);point ans=intersection(tmp,l2,m1,m2);printf("%.3lf %.3lf\n",ans.x,ans.y);}return 0;}