使用C#检测光投影在二维空间空间

2023-09-07 14:55:14 作者:青春是首不老歌。

一个光源是在二维空间,它位于一个实体上的一个坐标。

A light source is an entity in 2D space that sits in a single coordinate.

有多个光源围绕在不同的地点,每个散发出的方向8射线光 N,S,E,W,西北,东北,西南,东南 。所有灯光的坐标是已知的。

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);

现在我需要遍历灯收集和确定我的位置( position`)是被击中每一个。

Now I need to iterate the lights' collection and determine if my location (position`) is being hit by each.

一个简单的检查灯[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.