这道题是计算几何,这是写的第一道计算几何,主要是难在如何求入射光线的反射光线。

我们可以用入射光线 - 入射光线在法线(交点到圆心的向量)上的投影*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的更多相关文章

  1. SGU 110. Dungeon 计算几何 难度:3

    110. Dungeon time limit per test: 0.25 sec. memory limit per test: 4096 KB The mission of space expl ...

  2. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  3. PostMan的在线安装和简单使用

    Postman是一款很流行的WEB接口测试工具,因其强大的功能及清新的界面,赢得许多测试及开发者的喜爱.   1.PostMan的在线安装 因google退出中国,使得chrome上的扩展插件无法在线 ...

  4. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

  5. [LeetCode] Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  6. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  7. sgu 240 Runaway (spfa)

    题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...

  8. [变]C#谜题(1-10)表达式篇

    [变]C#谜题(1-10)表达式篇 最近偶然发现了<Java谜题>,很有意思,于是转到C#上研究一下. 本篇是关于表达式的一些内容. 谜题1:奇数性(负数的取模运算) 下面的方法意图确定它 ...

  9. [No00006D]下载离线版的github for windows【以Github for Windows 3.0.110.为例】

    目录 先上地址后讲原理: 原理: 11个目录的文件怎么一口气下载呢? 最后,把下好的文件批量名,同时将GitHub.exe.manifest也放到软件根目录下(与GitHub.exe同级): 今后的猜 ...

随机推荐

  1. cocos2d-x 动画加速与减速

    转自:http://novacreo.com/%E7%A8%8B%E5%BA%8F%E7%BB%84/cocos2d-x%E5%8A%A8%E7%94%BB%E5%8A%A0%E9%80%9F%E4% ...

  2. C++ 变量初始化规则

    1.定义变量的时候,如果没有初始化,它的值是什么呢? 它的值取决于变量的类型和变量定义的位置. 2.考虑基本类型的变量,定义时没有初始化.如果定义在方法外部,初始化为0,如果定义在方法内部,不被初始化 ...

  3. 05---JSON学习(Java)

    一.简介         JSON:JavaScript对象表示法(JavaScript object Notation)         JSON是存储和文本交换信息的语法         JSON ...

  4. [Effective C++ --021]必须返回对象时,别妄想返回其reference

    引言 在条目20中,我们知道了值传递和引用传递的效率问题,因此在设计程序时,我们可能就尽可能来返回引用而不是值. 可是,可能会犯下面的一些错误:传递一些引用指向其实并不存在的对象. 第一节:返回临时变 ...

  5. cocos2d 小游戏

    今天写了一个小游戏,发现看过的代码自己来写还是会经常出错,还是要多自己动手写写哈. 先上几张游戏界面图 void HelloWorld::addTarget() { //首先初始化精灵 CCSprit ...

  6. Database ORM

    Database ORM Introduction Basic Usage Mass Assignment Insert, Update, Delete Soft Deleting Timestamp ...

  7. Java基础知识强化之网络编程笔记16:Android网络通信之 使用Http的Get方式读取网络数据(基于HTTP通信技术)

    使用Http的Get方式读取网络数据,使用Get方式与网络通信是最常见的Http通信,建立链接之后就可以通过输入流读取网络数据. 详见:Android(java)学习笔记209:采用get请求提交数据 ...

  8. 虚反矩阵指令pinv之应用

    pinv指令     在多数解的例子中,有时并不是仅要将其中一变数设定为零之解.为使整个系统得到最佳化,亦可利用pinv指令求得最小模组之合理解.pinv(A)又称为虚反矩阵(pseudoinvers ...

  9. 使用jstack分析java程序cpu占用率过高

    在项目中经常会碰到CPU占用率过高的问题,那么碰到这类问题应当如何处理呢?下面提供一种处理思路: 首先top -H -p <pid>以线程的模式查看java应用的运行情况,找到占用cpu或 ...

  10. JavaScript 零散知识点1 (正则表达式+定时器+hover)

    1.clear:both清楚浮动影响//css中 2.正则表达式 search方法 :指明是否存在相应的匹配,如找到一个返回一个整数值,表明这个匹配距离字符串开始的偏移位置,如果没有找到匹配返回-1f ...