没事做,贴个代码。
2011-07-11 16:28
225 查看
Problem E
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11 Accepted Submission(s): 2
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.
SampleInput
1 0.000 0.000 4.000 0.000 1.000 1.000 3.000 1.000
SampleOutput
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;
}
相关文章推荐
- 天天对着代码没事也谢谢博客
- 闲的没事,自己动手修改上兴3.2源代码(2)
- 没事写代码之连续质数
- 没事写个小程序---代码统计
- 没事敲代码-SqlHelper
- 闲的没事,自己动手修改上兴3.2源代码
- 在家没事,看看boost的代码
- 没事晒晒代码
- 在家没事,看看boost的代码
- 好好写代码吧,没事别瞎B去创业!
- 在家没事,看看boost的代码
- 贴点国外大神代码,没事瞅瞅
- 在家没事,看看boost的代码
- 在家没事,看看boost的代码
- 在家没事,看看boost的代码
- 2012世界末日倒计时代码 原来没事虚惊一场
- 在家没事,看看boost的代码
- 至今让我回味无穷的代码,我要没事回味回味
- 2012世界末日倒计时代码 原来没事虚惊一场
- 常用的正则表达式(转)及在C#代码中用正则表达式验证数据(转)