找到数字键盘上两个键之间方向的算法?

发布于 2024-12-05 13:07:22 字数 554 浏览 0 评论 0原文

给定以下方向枚举:

typedef enum {
    DirectionNorth = 0,
    DirectionNorthEast,
    DirectionEast,
    DirectionSouthEast,
    DirectionSouth,
    DirectionSouthWest,
    DirectionWest,
    DirectionNorthWest
} Direction;

和类似于数字键盘的数字矩阵:

7 8 9
4 5 6
1 2 3

如何编写一个函数来返回矩阵中相邻数字之间的方向?说:

1, 2 => DirectionEast
2, 1 => DirectionWest
4, 8 => DirectionNorthEast
1, 7 => undef

如果您愿意,您可以更改枚举的数值。首选可读的解决方案。 (不是作业,只是我正在开发的应用程序的算法。我有一个工作版本,但我对更优雅的方式感兴趣。)

Given the following direction enum:

typedef enum {
    DirectionNorth = 0,
    DirectionNorthEast,
    DirectionEast,
    DirectionSouthEast,
    DirectionSouth,
    DirectionSouthWest,
    DirectionWest,
    DirectionNorthWest
} Direction;

And number matrix similar to the numeric pad:

7 8 9
4 5 6
1 2 3

How would you write a function to return the direction between adjacent numbers from the matrix? Say:

1, 2 => DirectionEast
2, 1 => DirectionWest
4, 8 => DirectionNorthEast
1, 7 => undef

You may change the numeric values of the enum if you want to. Readable solutions preferred. (Not a homework, just an algorithm for an app I am working on. I have a working version, but I’m interested in more elegant takes.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紙鸢 2024-12-12 13:07:22
int direction_code(int a, int b)
{
    assert(a >= 1 && a <= 9 && b >= 1 && b <= 9);
    int ax = (a - 1) % 3, ay = (a - 1) / 3,
        bx = (b - 1) % 3, by = (b - 1) / 3,
        deltax = bx - ax, deltay = by - ay;
    if (abs(deltax) < 2 && abs(deltay) < 2)
        return 1 + (deltay + 1)*3 + (deltax + 1);
    return 5;
}

结果代码是

1 south-west
2 south
3 south-east
4 west
5 invalid
6 east
7 north-west
8 north
9 north-east
int direction_code(int a, int b)
{
    assert(a >= 1 && a <= 9 && b >= 1 && b <= 9);
    int ax = (a - 1) % 3, ay = (a - 1) / 3,
        bx = (b - 1) % 3, by = (b - 1) / 3,
        deltax = bx - ax, deltay = by - ay;
    if (abs(deltax) < 2 && abs(deltay) < 2)
        return 1 + (deltay + 1)*3 + (deltax + 1);
    return 5;
}

resulting codes are

1 south-west
2 south
3 south-east
4 west
5 invalid
6 east
7 north-west
8 north
9 north-east
秋凉 2024-12-12 13:07:22

我将重新定义枚举中的值,以便北、南、东和西各取不同的位。

typedef enum {
    undef = 0,
    DirectionNorth = 1,
    DirectionEast = 2,
    DirectionSouth = 4,
    DirectionWest = 8,
    DirectionNorthEast = DirectionNorth | DirectionEast,
    DirectionSouthEast = DirectionSouth | DirectionEast,
    DirectionNorthWest = DirectionNorth | DirectionWest,
    DirectionSouthWest = DirectionSouth | DirectionWest
} Direction;

有了这些新值:

int ax = ( a - 1 ) % 3, ay = ( a - 1 ) / 3;
int bx = ( b - 1 ) % 3, by = ( b - 1 ) / 3;

int diffx = std::abs( ax - bx );
int diffy = std::abs( ay - by );

int result = undef;
if( diffx <= 1 && diffy <= 1 )
{
    result |= ( bx == ax - 1 ) ? DirectionWest : 0;
    result |= ( bx == ax + 1 ) ? DirectionEast : 0;
    result |= ( by == ay - 1 ) ? DirectionSouth : 0;
    result |= ( by == ay + 1 ) ? DirectionNorth : 0;
}
return static_cast< Direction >( result );

更新:最后,我认为现在是正确的。

I would redefine the values in the enum so that North, South, East and West take a different bit each.

typedef enum {
    undef = 0,
    DirectionNorth = 1,
    DirectionEast = 2,
    DirectionSouth = 4,
    DirectionWest = 8,
    DirectionNorthEast = DirectionNorth | DirectionEast,
    DirectionSouthEast = DirectionSouth | DirectionEast,
    DirectionNorthWest = DirectionNorth | DirectionWest,
    DirectionSouthWest = DirectionSouth | DirectionWest
} Direction;

With those new values:

int ax = ( a - 1 ) % 3, ay = ( a - 1 ) / 3;
int bx = ( b - 1 ) % 3, by = ( b - 1 ) / 3;

int diffx = std::abs( ax - bx );
int diffy = std::abs( ay - by );

int result = undef;
if( diffx <= 1 && diffy <= 1 )
{
    result |= ( bx == ax - 1 ) ? DirectionWest : 0;
    result |= ( bx == ax + 1 ) ? DirectionEast : 0;
    result |= ( by == ay - 1 ) ? DirectionSouth : 0;
    result |= ( by == ay + 1 ) ? DirectionNorth : 0;
}
return static_cast< Direction >( result );

Update: Finally, I think its correct now.

只为守护你 2024-12-12 13:07:22

对于这个数字矩阵,以下内容成立:
1) 差值 1(+ve 或 -ve)始终意味着方向是东或西。
2)相似,北或南方向相差3。
3) 东北或西南相差 4 度。
4) 西北或东南相差2。

With this matrix of numbers the following holds true:
1) a difference of 1 (+ve or -ve) always implies that the direction is either east or west.
2) similary, a difference of 3 for direction north or south.
3) a difference of 4 north east or south west.
4) a difference of 2 north west or south east.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文