题意:给你镜子的位置(用两点确定的一条直线表示),光源,光的反射点,求光在镜子的折射点
计算几何的模板,注意斜率!!!
模板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; }