没事做,贴个代码。
Problem E
Total Submission(s): 11 Accepted Submission(s): 2
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.
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.
1 0.000 0.000 4.000 0.000 1.000 1.000 3.000 1.000
2.000 0.000
#include<stdio.h>
#include<math.h>
#include<iostream>
#define abs(double) ((double)>0?(double):-(double))
#define eps 0.0000001
using namespace std;
struct point{
double x,y;
friend istream&operator>>(istream&in,point &p){
in>>p.x>>p.y;
return in;
}
friend ostream&operator<<(ostream&out,point p){
out<<p.x<<' '<<p.y<<endl;
return out;
}
};
struct line{
bool inf;
double k,b;
friend istream&operator>>(istream&in,line&l){
l.inf=false;
in>>l.k>>l.b;
return in;
}
friend ostream&operator<<(ostream&out,line l){
if(l.inf)out<<' '<<l.b<<endl;
else out<<l.k<<' '<<l.b<<endl;
return out;
}
friend point operator*(line l1,line l2){
point t;
if(l1.inf){
t.x=l1.b;
t.y=l2.k*t.x+l2.b;
}else if(l2.inf){
t.x=l2.b;
t.y=l1.k*t.x+l1.b;
}else{
t.x=(l2.b-l1.b)/(l1.k-l2.k);
t.y=l1.k*t.x+l1.b;
}
return t;
}
};
line point2line(point a,point b){
line t;
t.inf=abs(a.x-b.x)<eps;
if(t.inf){
t.b=a.x;
}else{
t.k=(a.y-b.y)/(a.x-b.x);
t.b=a.y-t.k*a.x;
}
return t;
}
point pointcalc(point p,line l){
point t;
if(l.inf){
t.x=l.b*2-p.x;
t.y=p.y;
}else{
t.x=((1-l.k*l.k)*p.x+2*l.k*p.y-2*l.k*l.b)/(1+l.k*l.k);
t.y=((l.k*l.k-1)*p.y+2*l.k*p.x+2*l.b)/(1+l.k*l.k);
}
return t;
}
int main(){
int cas;
line t1,t2;
point a,b,c,d,x;
cin>>cas;
while(cas--){
cin>>a>>b>>c>>d;
x=pointcalc(c,t1=point2line(a,b));
//cout<<t1<<x<<d;
t2=point2line(x,d);
//cout<<t2;
c=t1*t2;
printf("%.3f %.3f\n",c.x,c.y);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步