使用C#检测二维空间中的光线投影[英] Detecting light projections in 2D space using C#

本文是小编为大家收集整理的关于使用C#检测二维空间中的光线投影的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

光源是位于单个坐标中的2D空间中的实体.

各个位置周围有多个光源,每个位置都在方向上散发出8射线.所有灯的坐标都是已知的.

给出一个随机点(x, y),我需要确定它是否被光线击中.

int width = 10000;
int height = 10000;
List<Point> lights = a bunch of randomly placed light sources.
Point position = new Point(8888, 5555);

现在,我需要迭代lights' collection and determine if my location (位置`)被每个位置击中.

对lights[n].X == position.X的简单检查可以告诉我水平命中率和类似的垂直命中.如何以最有效的方式检测对角线命中?由于对角线总是在45度角,我可以避免昂贵的浮点计算吗?

推荐答案

为什么不只是使用:

Math.abs(lights[n].X - position.X) == Math.abs(lights[n].Y - position.Y)

使用角度(TRIG功能)几乎肯定会更慢,更复杂.

本文地址:https://www.itbaoku.cn/post/35928.html

问题描述

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?

推荐答案

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.