前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >「HDU - 2857」Mirror and Light(点关于直线的对称点)

「HDU - 2857」Mirror and Light(点关于直线的对称点)

作者头像
饶文津
发布2020-06-02 15:40:43
3630
发布2020-06-02 15:40:43
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

题目链接 Mirror and Light

题意

一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点。(都在一个二维平面内)

题解

找出入射光线关于镜子直线的对称点,然后和反射光线连边,与镜子的交点即是所求点。 用这题来测测板子。然后wa了一下,因为没注意要保留3位小数。这种输出错误要多注意啦,类似最近忘记加Case #x的错误- -||。

代码

代码语言:javascript
复制
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-8;
int sgn(double x){
    if(fabs(x)<eps)return 0;
    return (x<0)?-1:1;
}
struct Point{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
    Point operator -(const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const{
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const{
        return x*b.x+y*b.y;
    }
};
struct Line{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e):s(_s),e(_e){}
    pair<int,Point> operator &(const Line& b)const{
        Point res=s;
        if(sgn((s-e)^(b.s-b.e))==0){
            if(sgn((s-b.e)^(b.s-b.e))==0)
                return make_pair(0,res);
            return make_pair(1,res);
        }
        double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x+=(e.x-s.x)*t;
        res.y+=(e.y-s.y)*t;
        return make_pair(2,res);
    }
};
double dist(Point a,Point b){
    return sqrt((a-b)*(a-b));
}
void format(Line L,double& A,double& B,double& C){
    A=(L.e.y-L.s.y)/dist(L.s,L.e);
    B=(L.s.x-L.e.x)/dist(L.s,L.e);
    C=-L.s.y*B-L.s.x*A;
}
Point mirror(Point P,Line L){
    double A,B,C;
    format(L,A,B,C);
    double A2=A*A,B2=B*B,AB=A*B;
    return Point(((B2-A2)*P.x-AB*P.y*2-A*C*2)/(A2+B2),
            ((A2-B2)*P.y-AB*P.x*2-B*C*2/(A2+B2)));
}
int t;
double d[8];
int main(){
    scanf("%d",&t);
    while(t--){
        for(int i=0;i<8;++i)scanf("%lf",d+i);
        Line L(Point(d[0],d[1]),Point(d[2],d[3]));
        Point s(d[4],d[5]);
        Point e=mirror(s,L);
        Line L2(e,Point(d[6],d[7]));
        Point c=(L&L2).second;
        printf("%.3f %.3f\n",c.x,c.y);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • 题解
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档