如何与QUOT;扣"定向(2D)向量指南针(N,NE,E,SE,S,SW,W,NW)? [英] How to "snap" a directional (2D) vector to a compass (N, NE, E, SE, S, SW, W, NW)?
问题描述
我有一大堆的向量垂直于窗口表面的三维建模软件。预计到xy平面,我想知道在哪个方向,他们所面对的,转换到 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).
载体的工作是这样的:
- X轴重新presents东西(与东为正)
- y轴重新presents北 - 南(与北为正)
- 在这样
- (0,1)==北
- (1,0)==东
- (0,-1)==南
- ( - 1,0)==西
- 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
给定一个向量(X,Y)我在寻找最接近的8罗盘坐标。如何做到这一点的任何想法优雅?
Given a vector (x, y) I am looking for the closest of the 8 compass coordinates. Any ideas on how to do this elegantly?
推荐答案
这在Java中,计算值0 ... 7的八个方向:
This works in Java, computing a value 0...7 for the eight directions:
import static java.lang.Math.*; int compass = (((int) round(atan2(y, x) / (2 * PI / 8))) + 8) % 8;
结果映射到指南针如下:
The result maps to the compass as follows:
0 => E 1 => NE 2 => N 3 => NW 4 => W 5 => SW 6 => S 7 => SE
这篇关于如何与QUOT;扣"定向(2D)向量指南针(N,NE,E,SE,S,SW,W,NW)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!