hdu 2857 点在直线上的投影+直线的交点

Mirror and Light

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


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

题目大意:给一面镜子(一直线),给一入射光经过的点跟反射光经过的点,求入射点。

思路:求一个点关于镜子的对称点,与另一点与镜子的交点就是入射点。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 const double eps=1e-10;
 8 const double Pi=acos(-1.0);
 9 struct Point
10 {
11     double x,y;
12     Point(double x=0,double y=0):x(x),y(y) {}
13 };
14 typedef Point Vector;
15 Vector operator +(Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
16 Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
17 Vector operator *(Vector A,double p){return Vector(A.x*p,A.y*p);}
18 int dcmp(double x)
19 {
20     if(fabs(x)<eps) return 0;
21     else return x<0?-1:1;
22 }
23 
24 double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//点积
25 double Length(Vector A){return sqrt(Dot(A,A));}//向量的长度
26 double Angle(Vector A,Vector B){return acos(Dot(A,B)/Length(A)/Length(B));}//两向量的夹角
27 double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积
28 Point GetLineProjection(Point P,Point A,Point B)//P在直线AB上的投影点
29 {
30     Vector v=B-A;
31     return A+v*(Dot(v,P-A)/Dot(v,v));
32 }
33 Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)//两直线的交点
34 {
35     Vector u=P-Q;
36     double t=Cross(w,u)/Cross(v,w);
37     return P+v*t;
38 }
39 
40 Point read_point()
41 {
42     Point p;
43     scanf("%lf%lf",&p.x,&p.y);
44     return p;
45 }
46 int main()
47 {
48     int t;
49     Point p1,p2,p3,p4,p5;
50     scanf("%d",&t);
51     while(t--)
52     {
53         p1=read_point();p2=read_point();p3=read_point();p4=read_point();
54         p5= GetLineProjection(p3,p1,p2);
55         p5=p3+(p5-p3)*2;
56         p5=GetLineIntersection(p5,p5-p4,p1,p2-p1);
57         printf("%.3lf %.3lf
",p5.x,p5.y);
58     }
59     return 0;
60 }
原文地址:https://www.cnblogs.com/xiong-/p/3930489.html