HDU 2857 镜面反射

来源:互联网 发布:网络骑士全部作品 编辑:程序博客网 时间:2024/04/30 11:16

给你镜子的位置(用两点确定的一条直线表示),光源,光的反射点,求光在镜子的折射点。

初中常做的题目。


const double eps = 1e-8 ;double  sig(double x){        if(fabs(x) < eps) return 0 ;        return x > 0 ? 1 : -1 ;}double  add(double x , double y){        if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;        return x + y ;}struct  Point{        double x , y ;        Point(){}        Point(double _x , double _y):x(_x),y(_y){}        Point operator + (Point o){              return Point(add(x , o.x) , add(y , o.y)) ;        }        Point operator - (Point o){              return Point(add(x , -o.x) , add(y , -o.y)) ;        }        Point operator * (double o){              return Point(x*o , y*o) ;        }        double operator ^(Point o){               return add(x*o.y , -y*o.x) ;        }        double dist(Point o){               return sqrt((x-o.x)*(x-o.x) + (y-o.y)*(y-o.y)) ;        }        void  read(){              scanf("%lf%lf" ,&x , &y) ;        }};//直线p1p2 与直线q1q2 的交点Point  getintersect(Point p1 , Point p2 , Point q1 , Point q2){       double d1 = (p2 - p1) ^ (q1 - p1) ;       double d2 = (p2 - p1) ^ (q2 - q1) ;       double d3 = (q2 - q1) ^ (q1 - p1) ;       double d4 = (q2 - q1) ^ (p2 - p1) ;       if(fabs(d1) < eps) return q1 ;       if(fabs(d2) < eps) return q2 ;       double t = d3 / d4 ;       return  p1 + (p2 - p1) * t ;}struct Line{       Point s , t ;       Line(){}       Line(Point _s , Point _t):s(_s),t(_t){}       Point getinterpoint(Line o){             return getintersect(s , t , o.s , o.t) ;       }       Point mindistpoint(Point p){//p到直线的垂点             Point p1 = p ;             Point p2 = p + Point(s.y - t.y , t.x - s.x) ;             return getintersect(p1 , p2 , s , t) ;       }       void read(){            s.read() , t.read() ;       }} ;int  main(){     int i , j , t  ;     Line mirror  ;     Point a , b , c  , _a , s ;     cin>>t ;     while(t--){          mirror.read()  ;          a.read() ;          b.read() ;          c = mirror.mindistpoint(a) ; //垂点          _a = c * 2 - a ; //a关于镜子的对称点_a          s = mirror.getinterpoint(Line(_a , b)) ;          printf("%.3lf %.3lf\n" , s.x , s.y)  ;     }     return 0 ;}





0 0
原创粉丝点击