「HDU - 2857」Mirror and Light(点关于直线的对称点)
题意
一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点。(都在一个二维平面内)
题解
找出入射光线关于镜子直线的对称点,然后和反射光线连边,与镜子的交点即是所求点。
用这题来测测板子。然后wa了一下,因为没注意要保留3位小数。这种输出错误要多注意啦,类似最近忘记加Case #x的错误- -||。
代码
#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;
}
「HDU - 2857」Mirror and Light(点关于直线的对称点)的更多相关文章
- Solution -「HDU 6875」Yajilin
\(\mathcal{Description}\) Link.(HDU 裂开了先放个私链 awa.) 在一个 \(n\times n\) 的方格图中,格子 \((i,j)\) 有权值 \(w_ ...
- Solution -「HDU 5498」Tree
\(\mathcal{Description}\) link. 给定一个 \(n\) 个结点 \(m\) 条边的无向图,\(q\) 次操作每次随机选出一条边.问 \(q\) 条边去重后构成生成 ...
- 「 HDU P4734 」 F(x)
# 题目大意 对于一个数 $x$,它的每一位数字分别是 $A_{n}A_{n-1}A_{n-2}\cdots A_{2}A_{1}$,定义其权重 $f(x)=\sum_{i=1}^{n}\left(A ...
- 「 HDU 1978 」 How many ways
# 解题思路 记忆化搜索 一个点可以跳到的点,取决于它现在的能量.而且有一个显而易见的性质就是一条可行路径的终点和起点的横坐标之差加上纵坐标之差肯定小于等于起点的能量. 因为跳到一个点之后,能量和之前 ...
- 「 HDU P2089 」 不要62
和 HDOJ 3555 一样啊,只不过需要多判断个 ‘4’ 我有写 3555 直接去看那篇吧 这里只放代码 #include <iostream> #include <cstring ...
- 「 HDU P3555 」 Bomb
# 题目大意 给出 $\text{T}$ 个数,求 $[1,n]$ 中含 ‘49’ 的数的个数. # 解题思路 求出不含 '49' 的数的个数,用总数减去就是答案. 数位 $DP$,用记忆化来做. 设 ...
- 「 HDU P3336 」 Count the string
题目大意 给出一个长度为 $n$ 的字符串 $s$ 要求你求出 $s$ 的每一个前缀在 $s$ 中出现的次数之和.$n\le 200000$. 解题思路 暴力的对每一个前缀进行一次匹配,求出出现次数后 ...
- 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)
首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...
- Solution -「HDU 6643」Ridiculous Netizens
\(\mathcal{Description}\) Link. 给定一棵含有 \(n\) 个结点的树,点 \(u\) 有点权 \(w_u\),求树上非空连通块的数量,使得连通块内点权积 \(\ ...
随机推荐
- Spring Data Elasticsearch 和 x-pack 用户名/密码验证连接
Elasticsearch Java API 客户端连接 一个是TransportClient,一个是NodeClient,还有一个XPackTransportClient TransportClie ...
- siteServer创建网站中Mysql和SqlServer的区别
mysql中使用本地数据库时使用:localhost sqlserver使用本地数据库时使用:(local)
- UITableView 的横向滑动实现
UITableView 的横向滑动实现 概述 为了实现横向滑动的控件,可以继承类 UIScrollView 或类 UIView 自定义可以横向滑动的控件,这里通过 UITableView 的旋转,实现 ...
- [options] 未与 -source 1.6 一起设置引导类路径
用ant与eclipse编译Cassandra 1.2.19,出现了“ [options] 未与 -source1.6一起设置引导类路径”的警告,并出现了一些编译错误,提示编译失败,上网找了很 多资料 ...
- Windows之文件夹中打开PowerShell
Windows之文件夹中打开PowerShell 为了解决Windows中在某个路径下使用PowerShell,而不是使用传统的cd命令切换过去,具体做法如下: 方法一 打开文件夹 在文件夹的内容区按 ...
- Access使用记录
iif函数 此函数类似编程语言中的双目运算符,官方解释如下: 在任何可以使用表达式的位置均可使用 IIf.您可以使用 IIf 确定另一个表达式为 True 还是 False.如果表达式为 True,则 ...
- springIOC源码分析(BeanFactroy)
启动spring容器加载bean的方式有两种:最基本的容器BeanFactory和高级容器ApplicationContext.这篇文章介绍使用BeanFactory加载bean时的整个过程,当然,A ...
- Codeforces 1154C Gourmet Cat
题目链接:http://codeforces.com/problemset/problem/1154/C 题目大意: 主人有一只猫.周一&周四&周日:吃鱼周二&周六:吃兔子周三 ...
- 运行pip报错:Fatal error in launcher: Unable to create process using '"'
参考: https://blog.csdn.net/cjeric/article/details/73518782
- 关于Linux系统下jdk版本切换问题(alternatives命令的使用)
[root@danny lib]# cd /usr/lib/jvm [root@danny jvm]# [root@danny jvm]# ls java java--openjdk-.b13.el7 ...