Mirror and Light
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 650 Accepted Submission(s): 316
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.
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.
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
Recommend
计算几何:求点关于直线的对称点 + 两线段交点。
直接copy了别人的模板,写的有些混乱,见谅!有时间再研究。
参考链接:
代码:
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 using namespace std;
5 struct Point {double x,y;}; //点
6 struct Ldir{double dx,dy;}; //方向向量
7 struct Lline{Point p; Ldir dir;}; //直线
8 // 计算直线的一般式 Ax+By+C=0
9 void format(Lline ln,double& A,double& B,double& C)
10 {
11 A=ln.dir.dy;
12 B=-ln.dir.dx;
13 C=ln.p.y*ln.dir.dx-ln.p.x*ln.dir.dy;
14 }
15 // 求点p1关于直线ln的对称点p2
16 Point mirror(Point P,Lline ln)
17 {
18 Point Q;
19 double A,B,C;
20 format(ln,A,B,C);
21 Q.x=((B*B-A*A)*P.x-2*A*B*P.y-2*A*C)/(A*A+B*B);
22 Q.y=((A*A-B*B)*P.y-2*A*B*P.x-2*B*C)/(A*A+B*B);
23 return Q;
24 }
25 //求线段交点
26 struct TLine
27 {
28 //直线标准式中的系数
29 double a, b, c;
30 };
31 TLine lineFromSegment(Point p1, Point p2)
32 {
33 TLine tmp;
34 tmp.a = p2.y - p1.y;
35 tmp.b = p1.x - p2.x;
36 tmp.c = p2.x * p1.y - p1.x * p2.y;
37 return tmp;
38 }
39 /*求直线的交点,注意平形的情况无解,避免RE*/
40 const double eps = 1e-6; //注意一定要加,否则错误
41 Point LineInter(TLine l1, TLine l2)
42 {
43 //求两直线得交点坐标
44 Point tmp;
45 double a1 = l1.a;
46 double b1 = l1.b;
47 double c1 = l1.c;
48 double a2 = l2.a;
49 double b2 = l2.b;
50 double c2 = l2.c;
51 //注意这里b1 = 0
52 if(fabs(b1) < eps){
53 tmp.x = -c1 / a1;
54 tmp.y = (-c2 - a2 * tmp.x) / b2;
55 }
56 else{
57 tmp.x = (c1 * b2 - b1 * c2) / (b1 * a2 - b2 * a1);
58 tmp.y = (-c1 - a1 * tmp.x) / b1;
59 }
60 return tmp;
61 }
62 int main()
63 {
64 int T;
65 cin>>T;
66 cout<<setiosflags(ios::fixed)<<setprecision(3);
67 while(T--){
68 Point p1,p2,ps,pe;
69 Lline l;
70 cin>>p1.x>>p1.y;
71 cin>>p2.x>>p2.y;
72 cin>>ps.x>>ps.y;
73 cin>>pe.x>>pe.y;
74 l.p = p1;
75 l.dir.dx = p2.x - p1.x;
76 l.dir.dy = p2.y - p1.y;
77 Point duichen = mirror(ps,l); //求对称点
78 //cout<<duichen.x<<' '<<duichen.y<<endl;
79 TLine l1,l2;
80 l1 = lineFromSegment(p1,p2);
81 l2 = lineFromSegment(duichen,pe);
82 Point inter = LineInter(l1,l2); //求交点
83 cout<<inter.x<<' '<<inter.y<<endl;
84 }
85 return 0;
86 }
Freecode : www.cnblogs.com/yym2013