问题描述
光源是位于单个坐标中的 2D 空间中的实体.
A light source is an entity in 2D space that sits in a single coordinate.
在不同的位置周围有多个光源,每个光源在N、S、E、W、NW、NE、SW、SE
方向发出8束光线.所有灯的坐标都是已知的.
There are multiple light sources around in various locations and each gives off 8 rays of light in directions N, S, E, W, NW, NE, SW, SE
. The coordinates of all lights are known.
给定一个随机点(x, y)
,我需要确定它是否被光线击中.
Given a random point (x, y)
, I need to determine if it is being hit by a ray of light.
int width = 10000;
int height = 10000;
List<Point> lights = a bunch of randomly placed light sources.
Point position = new Point(8888, 5555);
现在我需要迭代 lights 的集合并确定我的位置 (
position`) 是否被每个灯击中.
Now I need to iterate the lights' collection and determine if my location (
position`) is being hit by each.
对 lights[n].X == position.X
的简单检查可以告诉我水平命中和类似的垂直命中.如何以最有效的方式检测对角线命中?由于对角线始终为 45 度角,我可以避免昂贵的浮点计算吗?
A simple check for lights[n].X == position.X
could tell me a horizontal hit and similarly vertical. How do I detect the diagonal hits in the most efficient way possible? Since the diagonal is always at 45 degree angles, can I avoid costly floating point calculations?
推荐答案
为什么不直接使用:
Math.abs(lights[n].X - position.X) == Math.abs(lights[n].Y - position.Y)
使用角度(三角函数)几乎肯定会更慢、更复杂.
Using angles (trig functions) will almost definitely be slower and more complex.
这篇关于使用 C# 检测二维空间中的光投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!