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同级): 今后的猜 ...
随机推荐
- 前端 - 使用gulp搭建es6运行环境
1.创建一个项目目录2.全局安装Traceur,在控制台输入 npm install -g traceur3.打开项目目录,安装gulp以及gulp-traceur插件 npm install -g ...
- 【39】明智而审慎第使用private继承
1.private继承意味着,根据某物实现出,继承父类的实现,关闭父类的接口,并不是Is-A的关系,不满足里氏代换,继承的内容访问权限都修改为private. 2.那么问题来了,复合也表达根据某物实现 ...
- Delphi获取目录下所有文件名
//获取一个文件夹下的所有文件 //不包括文件夹里面的文件 //ListBox1.Items:= searchfile('Z:\'); //注意,path后面要有'\'; function Sear ...
- android学习日记06--SurfaceView视图
一.API关SurfaceView的介绍 SurfaceView是视图(View)的继承类,这个视图里内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸.Surfacev ...
- LeetCode 341. Flatten Nested List Iterator
https://leetcode.com/problems/flatten-nested-list-iterator/
- 进程关系之tcgetpgrp、tcsetpgrp和tcgetsid函数
需要有一种方法来通知内核哪一个进程组是前台进程组,这样,终端设备驱动程序就能了解将终端输入和终端产生的信号送到何处. #include <unistd.h> pid_t tcgetpgrp ...
- C#_delegate - 值参数和引用参数
值参数不能加,引用参数可以. 引用参数是共享的 using System; using System.Collections.Generic; using System.Linq; using Sys ...
- android离线安装adt
打开Eclipse, 在菜单栏上选择help->Install New SoftWare 出现如下界面: 点击 Add按钮,出现如下界面 在Name这而随意输入一个名字:ADT15:点击打开Ar ...
- Requirements
Requirements The framework requirements are limited. PHP 5.5 or greater. Apache Web Server or equiva ...
- 获取mp4文件信息
计算电影长度 方法1 从mvhd - movie header atom中找到time scale和duration,duration除以time scale即是整部电影的长度. time scale ...