如何“捕捉”到罗盘的方向(2D)矢量(N、NE、E、SE、S、SW、W、NW)?
我在 3D 建模软件中有一堆垂直于窗户表面的向量。投影到 XY 平面,我想知道它们面向哪个方向,转换为 8 罗盘坐标(北、东北、东、东南、南、西南、西 > 和西北)。
向量的工作方式如下:
- X 轴代表东西向(东为正),
- y 轴代表南北(北为正),
- 因此
- (0, 1) == 北
- (1, 0) == 东
- (0,-1) == 南
- (-1,0) == 西
给定一个向量 (x, y) 我正在寻找8 个罗盘坐标中最接近的一个。关于如何优雅地做到这一点有什么想法吗?
I have a bunch of vectors normal to window surfaces in a 3D modelling software. Projected to the XY-Plane, I would like to know in which direction they are facing, translated to the 8 compass coordinates (North, North-East, East, South-East, South, South-West, West and North-West).
The vectors work like this:
- the X axis represents East-West (with East being positive)
- the y axis represents North-South (with North being positive)
- thus
- (0, 1) == North
- (1, 0) == East
- (0,-1) == South
- (-1,0) == West
Given a vector (x, y) I am looking for the closest of the 8 compass coordinates. Any ideas on how to do this elegantly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这在 Java 中有效,计算八个方向的值 0...7:
结果映射到罗盘,如下所示:
This works in Java, computing a value 0...7 for the eight directions:
The result maps to the compass as follows:
我可能只是调用 atan2() 来计算航向角度(“偏航"),然后使用一系列
if
:s 或一些数学运算将其“捕捉”到 90 度的倍数。I would probably just do a call to atan2() to figure out the heading angle ("yaw"), and then use either a sequence of
if
:s or some maths to "snap" it to a multiple of 90 degrees.不需要执行atan 函数。
如果你这样做: y/x 你会得到直线的斜率。根据得到的数字来判断,您可以确定角度/八分圆。
对于正 x (x>0)
和负 x 的类似列表
,最后是例外情况:
附录:我只报告这种方法,因为当计算 atan 是不行的(例如在嵌入式系统上)时,
我必须挖掘一下。这是我使用的高度优化的例程(用于手机游戏)。
输入:x1,y1 = 向量的起点
x2,y2 = 向量的端点
输出 (0-7) = 0=north, 1=northwest, 2=west,...etc
这将产生以下输出:(
测试的向量可能看起来很奇怪,但我对它们进行了一些调整明显位于一个八分圆内,而不是在确切的边界上)
no need to do an atan function.
if you do: y/x you'll get the slope of the line. Judging by the number you get you can determine the angle/octant.
for positive x's (x>0)
and a similar list for the negative x's
and finally the exception cases:
addendum: I only report this method for when calculating an atan is a no go (for instance on an embedded system))
I had to dig a bit. Here's a highly optimized routine I use (used in mobile games).
input: x1,y1 = startpoint of vector
x2,y2 = endpoint of vector
output (0-7) = 0=north, 1=northwest, 2=west,...etc
This will result in the following output:
(The vectors for the test might look oddly chosen, but I tweaked them all a bit to be clearly in one octant and not on the exact border)
这个不使用atan2,并且每次调用最多进行 4 次比较和 2 个产品。在 4 个内部块中比较 x 与 y(我仅在第一个块中编辑它),可以将其减少到正好 4 次比较和每次调用 1 个产品。
This one does not use atan2, and does at worst 4 comparisons and 2 products per call. Comparing x to y in the 4 inner blocks (I edited it in the first block only), it can be reduced to exactly 4 comparisons and 1 product per call.