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 ...
随机推荐
- 【BZOJ 1230】 开关灯
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1230 [算法] 线段树 [代码] #include<bits/stdc++.h ...
- Java多线程技术-wait/notify/join
wait/notify的作用 wait()方法的作用是使当前执行代码的线程进行等待,wait()是Object类的方法,用来将当前线程置入预执行队列中,并且在wait()所在的代码处停止执行,直到接到 ...
- [BZOJ1601] 灌水
难点:找到正确方式建图 知识点:Kruskal 分析:这种题肯定要把点权转换到边权上,但肯定无法搞到和其他点相连的边上,怎么办呢?那就再造一个点呗,这个“超级点”和所有点相连,且边权=点权,于是就可以 ...
- AES && DES加解密
MD5加密一般不可逆,只能暴力突破.所以这边记录一下一些关于字符串的加解密的两种方法,以便自己学习 AES public class AESHelper { public static string ...
- python3爬取豆瓣排名前250电影信息
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : doubanmovie.py # @Author: Anthony.waa # @Dat ...
- 【java并发】(1)深入理解volatile关键字
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- C#如何判断操作系统语言版本
using System.Runtime.InteropServices; static void Main(string[] args) { System.Console.WriteLine(Sys ...
- 为什么使用dispatch_sync
1.充分利用多线程的性能: 2.将分散在多线程中的核心操作归并到一个queue执行(通常为一个串行队列). 3.便于在任务线程中进行后继操作. 术语: 任务线程: 同步模块.
- RN-第三方之react-native-pull 下拉刷新、上拉加载
有一个很好的下拉刷新.上拉加载库:react-native-pull地址:https://github.com/greatbsky/react-native-pull-demo 使用 import { ...
- vue.js的ajax和jsonp请求
首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...