直线关于球的多次反射,求最后一次反射点

#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的更多相关文章

  1. Hdu 1079 Calendar Game

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1079 一道博弈题.刚开始想用判断P点和N点的方法来打表,但无奈不知是哪里出错,总是WA.于是 ...

  2. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. PCB C# 连接MongoDB 数据库

    一.C# MongoDB 驱动下载 1.百度网盘:nuget下载地址(V2.7版本) https://pan.baidu.com/s/1VDsVcH1TMrXqhRCZVewZgA 2.VS 中NUg ...

  2. C99新增内容之变长数组(VLA)

    我们在使用多维数组是有一点,任何情况下只能省略第一维的长度.比如在函数中要传一个数组时,数组的行可以在函数调用时传递,当属数组的列却只能在能被预置在函数内部.看下面一个例子: #define COLS ...

  3. position中的absolute、fixed区别

    absolute: 绝对定位,相对于body.   fixed: 固定定位,相对于浏览器视窗,不随滚动条的滚动而滚动. 这两个属性概念比较模糊,一般在做左边列表菜单,右边内容区域的时候会用到这样的定位 ...

  4. Windows7环境下Composer 安装包的Cache目录位置

    http://segmentfault.com/a/1190000000355928 https://getcomposer.org/doc/ 要说Composer的用法,以后再说,现在只记录wind ...

  5. Android常见错误整理

    1.当我new class的时候,提示以下错误: Unable to parse template "Class" Error message: This template did ...

  6. Boost-QT兼容问题:#define FUSION_HASH #

    使用原始的MSVC10+QT48很长时间,需要把PCL升级到新的版本,不再使用自行编译的PCL1.7.2版本. 在使用MSVC2012的时候,使用MSVC12-的PCL1.8.0版本,出现了一个不大不 ...

  7. 三维重建:多点透视cvSolvePNP的替代函数(Code)

           在调试JNI程序时,所有的Shell都已经加载完成,而唯一真正核心的cv::SolvePnP却不能在JNI里面获得通行证,经过反复测试都不能运行,因此只能忍痛舍弃,自行编写一个具有相 ...

  8. 【sqli-labs】 less16 POST - Blind- Boolian/Time Based - Double quotes (基于bool型/时间延迟的双引号POST型盲注)

    ' or 1=1# -->失败 1" or 1=1# -->失败 1') or 1=1# -->失败 1") or 1=1# -->成功 判断为双引号变形注 ...

  9. Vue 爬坑之路—— 使用 Vuex + axios 发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios  ...

  10. mysql 是如何保证在高并发的情况下autoincrement关键字修饰的列不会出现重复

    转载自 https://juejin.im/book/5bffcbc9f265da614b11b731/section/5c42cf94e51d45524861122d#heading-8 mysql ...