題意:給你鏡子的位置(用兩點確定的一條直線表示),光源,光的反射點,求光在鏡子的折射點
計算幾何的模板,注意斜率!!!
模板1:
#include
#include
#include
#include
#include
模板2(利用點到直線上的最近點避免斜率):
#include
#include
using namespace std;
const double eps=1e-8;
struct point{
double x,y;
};
point intersection(point u1,point u2,point v1,point v2)
{
point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}
point ptoline(point p,point l1,point l2)
{
point t=p;
t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;
return intersection(p,t,l1,l2);
}
point m1,m2,l1,l2;
int main()
{
int n;
scanf("%d",&n);
while(n--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&m1.x,&m1.y,&m2.x,&m2.y,&l1.x,&l1.y,&l2.x,&l2.y);
point dot = ptoline(l1,m1,m2);
point now;
now.x = 2*dot.x - l1.x;//源點與對稱點的中點落在直線上
now.y = 2*dot.y - l1.y;
point ans = intersection(now,l2,m1,m2);
printf("%.3lf %.3lf\n",ans.x,ans.y);
}
return 0;
}