旋转1d RGBA阵列

时间:2018-05-31 22:30:07

标签: javascript arrays rotation p5.js

我正在处理如下所示的1D像素RGBA数组:

pixelArray =[0,0,0,255,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255];

绘制时此pixelArray对应的是2个黑色像素和4个白色像素:

BB
WW
WW

我的目标是旋转数组内像素的顺序,以便绘制时的图片看起来像

BWW  or WWB
BWW     WWB

这意味着我需要将pixelArray转换为

rotatedPixelArray = [0,0,0,255,255,255,255,255,255,255,255,255,0,0,0,255,255,255,255,255,255,255,255,255]

上面的例子只是一个例子。实际的rgba可以代表任何图像,并且可以具有100万+的长度。 我已经尝试了各种算法like this,然后转换为2d数组,然后旋转和展平(这确实有效),但我想避免这种情况,因为速度/内存是一个问题。

2 个答案:

答案 0 :(得分:0)

所以我想出来了,在我的情况下,它需要向左或向右旋转。我使用的代码如下:

function rotatePixelArray(pixelArray,w,h) {
  var rotatedArray = [];

  for (var x=0;x<w;x++) {
    for(var y=0;y<h;y++) {
      index = (x+y*w)*4;
      rotatedArray.push(pixelArray[index]);
      rotatedArray.push(pixelArray[index+1]);
      rotatedArray.push(pixelArray[index+2]);
      rotatedArray.push(pixelArray[index+3]);
    }
  }

  return rotatedArray;
}

要将其向后旋转,您可以传入切换的w,h变量。

答案 1 :(得分:0)

这可能对p5.js旋转90度(向左和向右)很有帮助(以前建议的解决方案似乎有一些“不对称”错误):

DatabaseHelper myDb;
EditText editTitle, editDescription;
Button btnAddData;
Button btnViewAll;
SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
ListView mList;
Cursor mCsr;


int[] item_layout_ids_for_list = new int[]{
        R.id.textview_name,
        R.id.textview_description,
        R.id.imagelist // the image view of the layout
};

String[] columns_to_list = new String[]{
        DatabaseHelper.COL_2,
        DatabaseHelper.COL_3,
        DatabaseHelper.COL_4// contains path of the image
};

private void manageListView() {
    mCsr = myDb.getAllData();
    if (mSCA == null) {
        // Builds the Adapter for the List
        mSCA = new SimpleCursorAdapter(
                this,
                R.layout.mylistview_item, mCsr,
                columns_to_list,
                item_layout_ids_for_list,
                0
        );
        mList.setAdapter(mSCA); // Ties the Adapter to the ListView
    } else {
        mSCA.swapCursor(mCsr); // Refresh the List
    }
}