200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > CCF 06-4 光线追踪 python

CCF 06-4 光线追踪 python

时间:2021-04-13 06:04:06

相关推荐

CCF 06-4 光线追踪 python

CCF 06-4 光线追踪 python

CCF官网题目

此题因为运行超时得到了30分,仅为没思路的同学提供一些想法,如果有改进建议欢迎评论!

题目分析

首先将反射镜转换为单独的反射点,去除反射镜的两端点。例如,反射镜(0,0)–(5,5)就可以转换为反射点(1,1),(2,2),(3,3),(4,4)。

题目中说“平面中有一些激光光源,每个光源位于一个坐标为整数的点上,会向某个水平或竖直的方向发射一定强度的激光。”。由此可知,光线与反射镜相交时一定是位于一个整数点上,所以反射镜就可以进行离散化,这样可以将坐标系内的点提前进行标记是否为反射点,光源移动到达某个整数点只需要判断其是否为反射点,从而进行相应的操作。每个光源单独追踪,以时间和光强进行控制,移动后判断是否到达反射点,更改其光向及光强。

当到达设定时间阈值t或者光强I<1时,输出结果。本文提前将反射点加入字典point_dict,每次光源移动都要判断是否到达反射点。

反射点根据其所在反射镜的方向可以分为两类,斜率为-1及1。光线与这两种反射镜相交后的方向改变需要分别进行讨论。

而光源的移动,有上下左右四种情况,所以每个光源需要有一个参数—光向,这样就可以编程自动化了。设置reflection、light两个类,并灵活使用python的字典数据结构。

代码

class reflection:def __init__(self, x1, y1, x2, y2, a):self.x1 = x1self.x2 = x2self.y1 = y1self.y2 = y2self.a = adef gradient(self):return (self.x1 - self.x2) / (self.y1 - self.y2)def insert_reflection(self):if self.x1 < self.x2:j = self.y1for i in range(self.x1 + 1, self.x2):if self.gradient() == 1:j = j + 1else:j = j - 1point_dict[(i, j)] = [self.gradient(), self.a]else:j = self.y2for i in range(self.x2 + 1, self.x1):if self.gradient() == 1:j = j + 1else:j = j - 1point_dict[(i, j)] = [self.gradient(), self.a]def remove_reflection(self):if self.x1 < self.x2:j = self.y1for i in range(self.x1 + 1, self.x2):if self.gradient() == 1:j = j + 1else:j = j - 1del point_dict[(i, j)]else:j = self.y2for i in range(self.x2 + 1, self.x1):if self.gradient() == 1:j = j + 1else:j = j - 1del point_dict[(i, j)]class light:def __init__(self, x, y, d, I, t):self.x = xself.y = yself.d = dself.I = Iself.t = tdef move(self):# d 的含义为:d = 0 表示沿 x 坐标增加的方向,d = 1 表示沿 y 坐标增加的方向,d = 2 表示沿 x 坐标减小的方向,d = 3 表示沿 y 坐标减小的方向if self.d == 0:self.x = self.x + 1elif self.d == 1:self.y = self.y + 1elif self.d == 2:self.x = self.x - 1else:self.y = self.y - 1def change_d(self, g):if g == 1:if self.d == 0:self.d = 1elif self.d == 1:self.d = 0elif self.d == 2:self.d = 3else:self.d = 2elif g == -1:if self.d == 0:self.d = 3elif self.d == 1:self.d = 2elif self.d == 2:self.d = 1else:self.d = 0def time_pass(self):while self.t > 0 and self.I >= 1:self.t = self.t - 1self.move()# 判断此时的(x,y)是否处于反射点 | 反射点的方向 1 或者 -1 | 修改dif (self.x, self.y) in point_dict.keys():reflection = point_dict[(self.x, self.y)]self.change_d(reflection[0])self.I = reflection[1] * self.Iif __name__ == "__main__":# 是否为反射镜point_dict = {}reflection_dict = {}m = int(input())# 1 x1 y1 x2 y2 a :反射系数为 a 的反射面# 2 k :删除第 k 个操作插入的反射面# 3 x y d I t :发射光线的方向为 d ,强度为 I ,求其所经 t 时刻后光线到达的坐标以及采样得到的光线强度for i in range(m):input_list = list(input().split())if int(input_list[0]) == 1:reflection_dict[i + 1] = reflection(x1=int(input_list[1]), y1=int(input_list[2]), x2=int(input_list[3]),y2=int(input_list[4]),a=float(input_list[5]))reflection_dict[i + 1].insert_reflection()elif int(input_list[0]) == 2:reflection_dict[int(input_list[1])].remove_reflection()else:light_temp = light(x=int(input_list[1]), y=int(input_list[2]), d=int(input_list[3]), I=float(input_list[4]),t=int(input_list[5]))light_temp.time_pass()if int(light_temp.I) == 0:print(0, 0, 0)else:print(light_temp.x, light_temp.y, int(light_temp.I))

结果

觉得不错,记得帮忙点个赞哟!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。