sgu 110 Dungeon
这道题是计算几何,这是写的第一道计算几何,主要是难在如何求入射光线的反射光线。
我们可以用入射光线 - 入射光线在法线(交点到圆心的向量)上的投影*2 来计算反射光线,自己画一个图,非常清晰明了。
具体到程序里,我们可以 v2 = v1 - fa / Length(fa) * 2 * ( Dot(v1, fa) / Length(fa)) 来求,简单来说就是用内积(点积)求出入射光线在法线上的长度,然后用法线的单位向量乘这个长度就可以了。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 55
#define inf 0x7f7f7f7f
using namespace std; struct Point3
{
double x, y, z;
Point3(double x=, double y=, double z=):x(x), y(y), z(z) { }
};
typedef Point3 Vector3;
const double eps = 1e-; int dcmp(double x)
{
if (fabs(x) < eps) return ;
else return x < ? - : ;
} int n;
double r[N];
Point3 Begin, circle[N];
Vector3 Direct; Vector3 operator + (Vector3 A, Vector3 B) { return Vector3(A.x+B.x, A.y+B.y, A.z+B.z); }
Vector3 operator - (Point3 A, Point3 B) { return Vector3(A.x-B.x, A.y-B.y, A.z-B.z); }
Vector3 operator * (Vector3 A, double p) { return Vector3(A.x*p, A.y*p, A.z*p); }
Vector3 operator / (Vector3 A, double p) { return Vector3(A.x/p, A.y/p, A.z/p); } double min(double x, double y) { return x < y ? x : y; } double Dot (Vector3 A, Vector3 B) { return A.x*B.x + A.y*B.y + A.z*B.z; }
double Length (Vector3 A) { return sqrt(Dot(A, A)); }
double Angle (Vector3 A, Vector3 B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
Vector3 Cross (Vector3 A, Vector3 B) { return Vector3(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.y*B.z - A.z*B.y); }
double disPointLine(Point3 P, Point3 A, Point3 B)
{
Vector3 v1, v2;
v1 = B - A; v2 = P - A;
return Length(Cross(v1, v2)) / Length(v1);
} int main()
{
scanf("%d", &n);
double x, y, z;
for (int i = ; i <= n; ++i)
{
scanf("%lf%lf%lf%lf", &x, &y, &z, &r[i]);
circle[i] = Point3(x, y, z);
}
scanf("%lf%lf%lf", &x, &y, &z); Begin = Point3(x, y ,z);
scanf("%lf%lf%lf", &x, &y, &z); Direct = Point3(x, y, z) - Begin;
int nowcircle = ;
for (int w = ; w <= ; ++w)
{
int nextcircle = ;
double mindis = inf;
for (int i = ; i <= n; ++i)
{
if (i == nowcircle) continue;
double a, b, c;
Point3 nc = circle[i];
a = Direct.x*Direct.x + Direct.y*Direct.y + Direct.z*Direct.z;
b = * ((Begin.x-nc.x) * Direct.x + (Begin.y-nc.y)*Direct.y + (Begin.z-nc.z)*Direct.z);
c = (Begin.x-nc.x)*(Begin.x-nc.x) + (Begin.y-nc.y)*(Begin.y-nc.y) + (Begin.z-nc.z)*(Begin.z-nc.z) - r[i]*r[i];
double delta;
delta = b*b - *a*c;
if (delta < ) continue;
double ans1, ans2;
ans1 = (-b + sqrt(delta)) / (*a);
ans2 = (-b - sqrt(delta)) / (*a);
if (dcmp(ans1) < && dcmp(ans2) < ) continue;
else if (dcmp(ans1) < && ans2 < mindis)
{
mindis = ans2;
nextcircle = i;
}
else if (dcmp(ans2) < && ans1 < mindis)
{
mindis = ans1;
nextcircle = i;
}
else if (min(ans1, ans2) < mindis)
{
mindis = min(ans1, ans2);
nextcircle = i;
}
}
if (!nextcircle) break;
else if (w == )
{
printf(" etc.");
break;
}
else
{
if (w == ) printf("%d", nextcircle);
else printf(" %d", nextcircle);
Point3 jiao;
Vector3 v1, v2, fa;
jiao = Begin + Direct*mindis; v1 = Direct;
fa = circle[nextcircle] - jiao;
v2 = (fa / Length(fa)) * ( * Dot(v1, fa) / Length(fa));
nowcircle = nextcircle;
Begin = jiao; Direct = v1 - v2;
}
}
printf("\n");
}
sgu 110 Dungeon的更多相关文章
- SGU 110. Dungeon 计算几何 难度:3
110. Dungeon time limit per test: 0.25 sec. memory limit per test: 4096 KB The mission of space expl ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- PostMan的在线安装和简单使用
Postman是一款很流行的WEB接口测试工具,因其强大的功能及清新的界面,赢得许多测试及开发者的喜爱. 1.PostMan的在线安装 因google退出中国,使得chrome上的扩展插件无法在线 ...
- SGU Volume 1
SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- sgu 240 Runaway (spfa)
题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...
- [变]C#谜题(1-10)表达式篇
[变]C#谜题(1-10)表达式篇 最近偶然发现了<Java谜题>,很有意思,于是转到C#上研究一下. 本篇是关于表达式的一些内容. 谜题1:奇数性(负数的取模运算) 下面的方法意图确定它 ...
- [No00006D]下载离线版的github for windows【以Github for Windows 3.0.110.为例】
目录 先上地址后讲原理: 原理: 11个目录的文件怎么一口气下载呢? 最后,把下好的文件批量名,同时将GitHub.exe.manifest也放到软件根目录下(与GitHub.exe同级): 今后的猜 ...
随机推荐
- 【47】请使用traits classes表现类型信息
1.考虑下面的需求,对迭代器移动d个单位.因为不同类型的迭代器,能力不同,有的迭代器(vector,deque内置迭代器)可以一步到位移动到指定位置,有的迭代器(list内置迭代器)必须一步一步移动, ...
- android常见错误-Unexpected namespace prefix "xmlns" found for tag LinearLayout
有一次升级android开发工具后发现xml脚本出现错误“Unexpected namespace prefix "xmlns" found for tag LinearLayou ...
- Matplotlib 工具包 使用教程索引
官方文档链接中: http://matplotlib.org/gallery.html 这里给了非常多演示样例图片.能够查看对应源码,是一个非常好学习途径. matplotlib 函数API :函数A ...
- Oracle Hints具体解释
在向大家具体介绍Oracle Hints之前,首先让大家了解下Oracle Hints是什么,然后全面介绍Oracle Hints,希望对大家实用.基于代价的优化器是非常聪明的,在绝大多数情况下它会选 ...
- iOS开发——UI篇&提示效果
提示效果 关于iOS开发提示效果是一个很常见的技术,比如我们平时点击一个按钮,实现回馈,或者发送网络请求的时候! 技术点: 一:View UIAlertView UIActionSheet 二:控制器 ...
- 设计模式 - 命令模式(command pattern) 多命令 具体解释
命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...
- hdu 1348 Wall(凸包模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) M ...
- Spring(AbstractRoutingDataSource)实现动态数据源切换--转载
原始出处:http://linhongyu.blog.51cto.com/6373370/1615895 一.前言 近期一项目A需实现数据同步到另一项目B数据库中,在不改变B项目的情况下,只好选择项目 ...
- Asp.Net 之 调用远程Web_Service
一.添加web service引用 1.右键 Web 项目 → “添加服务引用”: 2.右键已有的 App_WebReferences 文件夹 → “添加服务引用”: 二.引用远程web servic ...
- Asp.Net 之 汉字转拼音
1.利用微软提供的拼音库,计算出汉字的拼音的方法,此方法支持多音字符 下载 Visual Studio International Pack类库,该类库扩展了.NET Framework对全球化软件开 ...