HDU 2857 镜面反射

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

初中常做的题目。


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 ;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值