English 中文(简体)
确定2D矢量方向,分8个方向(横向、纵向和方言)
原标题:Determining direction of 2D vector with 8 discrete directions (horizontal, vertical and diagonals)

我收到了一个把病媒推向前进的游戏台。 病媒的数值范围界定了介于1、1至+1、+1的试金体内的循环。

我想知道,8个可能的方向中,病媒接近什么。 我对病媒(0、0)的病例表示担忧,单独处理。 <>?

Note: This is for a game, so this function will be ran very frequently. Anything to speed up the function would help, for example I know I could use the dot product and trig to work out the angle between my vector and each direction vector, however I wonder if something better could be done given I don t need the exact angle.

最佳回答

Imagine a square inside the -1..1 square, where the stick position (X) is on one of the sides:

+-----------------+  +-----------------+
|                 |  |                 |
|                 |  |                 |
|          X      |  |    +-----X-+    |
|                 |  |    |       |    |
|        O        |  |    |   O   |    |
|                 |  |    |       |    |
|                 |  |    +-------+    |
|                 |  |                 |
|                 |  |                 |
+-----------------+  +-----------------+

你必须找到坐标的哪一方,并检查它是否靠近一方的中心,还是一角。 如果内地面积太小,你可以认为该带是集中的。

Something like:

Public enum Direction {
  None,
  LeftUp, Up, RightUp, Right, RightDown, Down, LeftDown, Left
}

public Direction GetDirection(double x, double y) {
  double absX = Math.Abs(x);
  double absY = Math.Abs(y);
  if (absX < 0.1 && absY < 0.1) {
    // close to center
    return Direction.None;
  }
  if (absX > absY) {
    // vertical side
    double half = absX * 0.4142;
    if (x > 0) {
      // left side
      if (y > half) return Direction.LeftDown;
      if (y < -half) return Diretion.LeftUp;
      return Direction.Left;
    } else {
      // right side
      if (y > half) return Direction.RightDown;
      if (y < -half) return Direction.RightUp;
      return Direction.Right;
    }
  } else {
    // horisontal side
    double half = absY * 0.4142;
    if (y > 0) {
      // bottom
      if (x > half) return Direction.RightDown;
      if (x < -half) return Direction.LeftDown;
      return Direction.Down;
    } else {
      // top
      if (x > half) return Direction.RightUp;
      if (x < -half) return Direction.LeftUp;
      return Direction.Up;
    }
  }
}

无三角,只是简单比较,因此应当迅速。

(尽管我使用了三角,计算出0.4142点,即:tan(22.5),或角线为45/2的一侧的位置。)

问题回答

sina = tana

页: 1

Y = 罪状

X = coa

因此,对Y/X适用反向的附带功能,而你则倾向于。

EDIT: In a full circle, there are two angles with the same tangent value (a and a + pi). Use the sign of X and Y to determine which one is the valid one.

您可使用一个tan-1的预定近似值表。 根据您的病媒投入的规模计算出一个角,或许你胜出的必然需要一个非常大的分辨率(事实上,如果你只想8个不同的方向,那么,你表格中的8个条目就足够了)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签