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 ...
随机推荐
- [Apple开发者帐户帮助]三、创建证书(6)创建创建VoIP服务证书
VoIP:基于IP的语音传输(英语:Voice over Internet Protocol,缩写为VoIP)是一种语音通话技术,经由网际协议(IP)来达成语音通话与多媒体会议,也就是经由互联网来进行 ...
- Cookie、Token与Session介绍(非原创)
文章大纲 一.Cookie介绍二.Token介绍三.Session介绍四.Token.Cookie与Session比较五.参考文章 一.Cookie介绍 1. Cookie是什么 cookie机制 ...
- BZOJ 4800 折半暴搜
思路: 把它拆成两半 分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...
- B - Eleven
Problem description Eleven wants to choose a new name for herself. As a bunch of geeks, her friends ...
- 用CSS伪类制作一个不断旋转的八卦图?
前言 介绍一下如何制作一个不断旋转的八卦图.快速预览代码及效果,点击:八卦图 代码如下: HTML部分 <!DOCTYPE html> <html> <head> ...
- windows常用的cmd命令和常用操作。
这几日部署Jenkins,牵扯到很多东西,比如用到许多cmd命令和Linux命令.查找比较花时间,因此将查看的文档留下,以避免下次重新查找浪费时间. Windows cmd命令: http://blo ...
- iOS-如何返回某个字符串的拼音助记码
我也是看了网上的一个示例代码后,在它的基础上进行的修改.因为项目上会用到,我相信很多人的项目上也会用到.所以实现后,也赶紧分享出来,希望后来人不需要花费时间了. 提示:这里用到了正则表达式,使用了一个 ...
- react新版本生命周期
给componentWillMount componentWillReceiveProps componentWillUpdate生命周期加上UNSAFE_前缀,表明其不安全性,并将在未来版本将其移除 ...
- XML的解析方式
//解析和输出XML public void showXml() { string filepath = Application.dataPath + @"/my.xml"; if ...
- BZOJ [Poi2000]病毒 AC自动机_DFS_细节
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...