题目链接 Mirror and Light
题意
一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点。(都在一个二维平面内)
题解
找出入射光线关于镜子直线的对称点,然后和反射光线连边,与镜子的交点即是所求点。 用这题来测测板子。然后wa了一下,因为没注意要保留3位小数。这种输出错误要多注意啦,类似最近忘记加Case #x的错误- -||。
代码
代码语言:javascript复制#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-8;
int sgn(double x){
if(fabs(x)<eps)return 0;
return (x<0)?-1:1;
}
struct Point{
double x,y;
Point(double _x=0,double _y=0):x(_x),y(_y){}
Point operator -(const Point &b)const{
return Point(x-b.x,y-b.y);
}
double operator ^(const Point &b)const{
return x*b.y-y*b.x;
}
double operator *(const Point &b)const{
return x*b.x y*b.y;
}
};
struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e):s(_s),e(_e){}
pair<int,Point> operator &(const Line& b)const{
Point res=s;
if(sgn((s-e)^(b.s-b.e))==0){
if(sgn((s-b.e)^(b.s-b.e))==0)
return make_pair(0,res);
return make_pair(1,res);
}
double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x =(e.x-s.x)*t;
res.y =(e.y-s.y)*t;
return make_pair(2,res);
}
};
double dist(Point a,Point b){
return sqrt((a-b)*(a-b));
}
void format(Line L,double& A,double& B,double& C){
A=(L.e.y-L.s.y)/dist(L.s,L.e);
B=(L.s.x-L.e.x)/dist(L.s,L.e);
C=-L.s.y*B-L.s.x*A;
}
Point mirror(Point P,Line L){
double A,B,C;
format(L,A,B,C);
double A2=A*A,B2=B*B,AB=A*B;
return Point(((B2-A2)*P.x-AB*P.y*2-A*C*2)/(A2 B2),
((A2-B2)*P.y-AB*P.x*2-B*C*2/(A2 B2)));
}
int t;
double d[8];
int main(){
scanf("%d",&t);
while(t--){
for(int i=0;i<8; i)scanf("%lf",d i);
Line L(Point(d[0],d[1]),Point(d[2],d[3]));
Point s(d[4],d[5]);
Point e=mirror(s,L);
Line L2(e,Point(d[6],d[7]));
Point c=(L&L2).second;
printf("%.3f %.3fn",c.x,c.y);
}
return 0;
}