Detecting light projections in 2D space using C#

微笑、不失礼 提交于 2019-12-12 02:43:50

问题


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

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.

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

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

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?


回答1:


Why not just use:

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.



来源:https://stackoverflow.com/questions/11679828/detecting-light-projections-in-2d-space-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!