HDU 2912
直线关于球的多次反射,求最后一次反射点
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std;
const double inf=1e10;
const double eps=1e-8;
struct point {
double x,y,z;
// point (double _x,double _y,double _z){ x=_x; y=_y; z=_z; };
};
struct sphe {
point cent;
double r;
};
struct vect {
point st,des;
};
sphe cir[110];
vect livc;
int n; point operator -(const point &u,const point &v){
point ret;
ret.x=u.x-v.x; ret.y=u.y-v.y; ret.z=u.z-v.z;
return ret;
} double dot(point x,point y){
return x.x*y.x+x.y*y.y+x.z*y.z;
} point xmulti(point u,point v){
point ret;
ret.x=(u.y*v.z-v.y*u.z);
ret.y=(u.z*v.x-u.x*v.z);
ret.z=(u.x*v.y-u.y*v.x);
return ret;
} double dis(point x,point y){
return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y)+(x.z-y.z)*(x.z-y.z));
} double vlen(point x){
return sqrt(x.x*x.x+x.y*x.y+x.z*x.z);
} point construct(){
point crop;
crop.x=crop.y=crop.z=0;
double stoc=inf; point tmpcrop; point foot,tmpfoot; bool flag; point tmp; int k;
while(true){
flag=false; stoc=inf;
for(int i=0;i<n;i++){
if(dot(livc.des-livc.st,cir[i].cent-livc.st)>=-eps){//判断圆是否与直线同向,通过点积判方向
double D=vlen(xmulti(livc.des-livc.st,cir[i].cent-livc.st))/dis(livc.st,livc.des);
// cout<<D<<' '<<i<<endl;
if(D-cir[i].r<=eps){ //半径小于D,相交
flag=true;
// cout<<"YES"<<endl;
double u=dot(cir[i].cent-livc.st,livc.des-livc.st)/(dis(livc.st,livc.des)*dis(livc.st,livc.des));
//计算垂足。可通过向量的比例所得方程,联合垂直点积为0的方程解得
tmpfoot=livc.st;
tmpfoot.x+=u*(livc.des.x-livc.st.x);
tmpfoot.y+=u*(livc.des.y-livc.st.y);
tmpfoot.z+=u*(livc.des.z-livc.st.z);
// cout<<tmpfoot.x<<' '<<tmpfoot.y<<' '<<tmpfoot.z<<' '<<endl;
u=sqrt((cir[i].r*cir[i].r-D*D))/dis(livc.st,livc.des); //计算交点。垂足到圆上交点方向与直线反方向相同
//通过两者距离比计算出向量的转化
tmpcrop=tmpfoot;
tmp=livc.st-livc.des;
tmpcrop.x+=tmp.x*u;
tmpcrop.y+=tmp.y*u;
tmpcrop.z+=tmp.z*u;
D=dis(tmpcrop,livc.st);
// cout<<D<<endl;
if(D<stoc){ //若与多个圆相交,选取较近的一个
stoc=D; crop=tmpcrop;
k=i;
}
}
}
}
if(!flag) return crop;
double tu=dot(livc.st-cir[k].cent,crop-cir[k].cent)/(dis(crop,cir[k].cent)*dis(crop,cir[k].cent));
tmpfoot=cir[k].cent; //计算反射线。直线st点关于交点与球心的直线 对称点作为反射线的des点
tmpfoot.x+=tu*(crop.x-cir[k].cent.x);
tmpfoot.y+=tu*(crop.y-cir[k].cent.y);
tmpfoot.z+=tu*(crop.z-cir[k].cent.z); //知直线st点到反射线des点的方向与st点到关于对称线垂足方向相同且为两倍
livc.des.x=((tmpfoot.x-livc.st.x)*2+livc.st.x); //通过这样可以求对称点
livc.des.y=((tmpfoot.y-livc.st.y)*2+livc.st.y);
livc.des.z=((tmpfoot.z-livc.st.z)*2+livc.st.z);
livc.st=crop;
// cout<<livc.des.x<<' '<<livc.des.x<<' '<<livc.des.x<<endl;
}
} int main(){
point tmp; double r;
while(scanf("%d",&n),n){
livc.st.x=livc.st.y=livc.st.z=0;
scanf("%lf%lf%lf",&tmp.x,&tmp.y,&tmp.z);
livc.des=tmp;
for(int i=0;i<n;i++){
scanf("%lf%lf%lf%lf",&cir[i].cent.x,&cir[i].cent.y,&cir[i].cent.z,&cir[i].r);
}
tmp=construct();
printf("%.4lf %.4lf %.4lf\n",tmp.x,tmp.y,tmp.z);
}
}
HDU 2912的更多相关文章
- Hdu 1079 Calendar Game
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1079 一道博弈题.刚开始想用判断P点和N点的方法来打表,但无奈不知是哪里出错,总是WA.于是 ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- B3300 [USACO2011 Feb]Best Parenthesis 模拟
这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<c ...
- logistic regression二分类算法推导
- 利用存储过程插入50W+数据
转自:https://www.aliyun.com/jiaocheng/1396184.html 首先,建立部门表和员工表: 部门表: create table dept( id int un ...
- angular2之pdf文件操作大全
最近的项目中需要显示pdf内容.下载pdf.甚至是前端生成pdf 适用于angular2.4或者更高版本 情景1.需要将页面的某个部分转成pdf文件并下载(即将页面的部分html内容转成pdf文件) ...
- .Net Core项目上Azure Docker云
1.找到创建资源-容器-Container Instances 2.安装模板,填写私有映像表的相关信息 3.创建成功,运行测试.
- POJ 3481 set水过
题意:1表示插入客户K,他的优先级是P(相当于大小),2表示输出当前优先级最高的客户(即找出最大值),并且删除.3同理输出最低级的. 这题可以用splay treap AVL SBT -- (可是我并 ...
- Windows7下caffe-ssd-microsoft下编译
整个编译可谓漫长 编译了两天 网上教程也很多 但是也很杂 遇到各种错误 总归是编完了 1.下载Windows版本的Caffe-SSD源码 下载链接:https://github.com/conner9 ...
- 自定义View(6)paint设置两个图层相交时的显示方式,包含清空canvas
1.问题 在已有的图层上绘图将会在其上面添加一层新的图层. 如果新的图层是完全不透明的,那么它将完全遮挡住下面的图层,而setXfermode就可以来解决这个问题.这个函数设置两个图层相交时的模式 . ...
- 5.20 mybatis反向生成的映射文件xml(如果需要自己定义其他sql语句时如下)
解决mybatis-generator 生成的mapper.xml覆盖自定义sql的问题 mybatis-generator是个好工具,一建即可生成基本增删改成功能的mapper.xml.但这些是不够 ...
- CSS元素水平垂直居中的方法
1. 元素水平居中 1.1 设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...