HDU 2857 Mirror and Light(几何,求对称点)

时间:2022-08-01 04:13:37

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2857

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
 

题意:题目很简单了,先求对称点 ,在求两条直线交点 ,

代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <malloc.h>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define pi acos(-1.0)
#define INF 0xffffff
int main()
{
double x1,x2,x3,x4,y1,y2,y3,y4,xs,xe,ys,ye;
double k1,k2,k3,k4,xx,yy,m,n;
int t;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%lf%lf",&x1,&y1);
scanf("%lf%lf",&x2,&y2);
scanf("%lf%lf",&xs,&ys);
scanf("%lf%lf",&xe,&ye);
k1=(y2-y1)/(x2-x1);
if(x1==x2)//斜率不存在
{
xx=x1;
yy=ys;
printf("%.3lf %.3lf\n",xx,yy);
continue;
}
m = k1*k1*ys-ys+2*xs*k1;
n = 2*y2-2*k1*x2;
y3 = (m+n)/(k1*k1+1);
x3 = k1*ys-k1*y3+xs;
k2=(ye-y3)/(xe-x3);
xx=(ye-y2 + k1*x2-k2*xe)/(k1-k2);
yy=k1*xx - k1*x2+y2;
printf("%.3lf %.3lf\n",xx,yy);
}
}
return 0;
}