【问题标题】:Rotating points in a binary grid二进制网格中的旋转点
【发布时间】:2021-06-23 18:45:58
【问题描述】:

我正在基于this project 在 C 中制作一个二进制俄罗斯方块程序。 8 x 8 网格由unsigned long 表示,其中 0 表示空格,1 表示填充空格。另一个unsigned long 用于表示一个peice。 github页面描述了这样的基本概念:

董事会:

                  00000         .....
798               11000   =>    ##... 
                  11110         ####.

方块

3                 00011  =>     ...##

检查碰撞

                  00000         .....
798&3 = 2         00000  =>     .....
                  00010         ...X.

...

我如何将这些概念扩展到也适用于旋转块而不求助于算术运算符?

【问题讨论】:

  • 您可以使用<< 1(左)或>>1(右)移动一个点。不知道我是否理解你的问题
  • @AntoninGAVREL 感谢您的评论!对不起,如果有点混乱,我的意思是我想旋转一组点,例如旋转 90 度。

标签: c binary bit-manipulation


【解决方案1】:

对于每个块,您必须确定重心是什么。

然后,您可以为每个块设置一个函数数组,这有点像 2d 字符的精灵:

你的董事会:

0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

你的作品 3:

block_value = 0b11 << 16  // located on the right side, two rows before end
0b00000011
0b00000000
0b00000000
// rotate left
int mask = 1 << (__builtin_ctz(block_value) + 1);
block_value = block_value ^ mask | (mask << 9);
// this will give you the following block:
0b00000010
0b00000010
0b00000000
0b00000000
rotate left
// algo to transform it 90 degrees
rotate left
// algo to transform it 90 degrees
rotate left
back to first item 

您将这些操作作为函数指针存储在两个由 4 个函数指针组成的数组中,这取决于您是向左还是向右旋转,然后应用逻辑 AND:

current_index += rotation; //(+1 for left, -1 for right for example, or clockwise)
current_index = (current_index + 4) & 3; // to make sure you remain in the boundaries of your fptr array
if (rotation == 1)
    block_value = fptr_right[current_index](block_value); // call the transformation function
else // if (rotation == -1)
    block_value = fptr_left[current_index](block_value); // call the transformation function

如果它足够有用,请告诉我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-15
    • 2019-01-07
    • 2017-08-10
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多