gpt4 book ai didi

c++ - 一种在一维数组(位图)内迭代矩形区域的算法

转载 作者:可可西里 更新时间:2023-11-01 16:40:22 25 4
gpt4 key购买 nike

这是一个奇怪的问题,我很难为它写一个标题。

我正在处理像素(更具体地说是位图),无法弄清楚实用地访问每个数组单元格的(简单)数学。

我的 Canvas 是 [n16 x 16] 像素,n 始终为 1 或更大。

这是一张基本的 n = 2 Canvas 的照片:

http://i.imgur.com/mabwQfJ.png

enter image description here

我想让我的神奇算法做的是从 0 运行到 495 而不触及浅灰色区域,然后从 16 运行到 512(实际上是单元格 511,我的错)而不触及深灰色区域。

因此,0 到 15,跳过 16 到 31,然后是 32 到 47,等等。

对于 n = 3:

http://i.imgur.com/TqJMWl6.png

enter image description here

在这种情况下,0-735 会跳过较浅的灰色区域,16-751 会跳过两边的区域,32-767 会跳过较深的灰色区域。

我尝试过的:

这是我的代码的摘录,希望它有用并展示了我已经尝试过的内容。这是计算“idxpos”值的部分。

// Let's say length = 3 for now.
for (int character = 0; character < length; ++character)
{
// in case you're wondering, it grabs 16x16 characters from an ASCII spritesheet
charpos = (string[character] - ' ') * 16 * 16;

// Runs through the spritesheet character map
// this is a huge 16x1520 bitmap.
for (int pixel = 0; pixel < 16 * 16; ++pixel)
{
// ignore this, just me messing around with pixel tinting
r = (((CharMap[charpos + pixel] >> 0) & 0xFF) + 255 - u);
g = (((CharMap[charpos + pixel] >> 8) & 0xFF) + 255 - v);
b = (((CharMap[charpos + pixel] >> 16) & 0xFF) + 255 - w);
newcolour = RGB(r, g, b);

// THIS is the part I am stuck on:
idxpos = pixel + (character * 16 * 16);

bitmap[idxpos] = CharMap[charpos + j];
}
}

你可能明白了。这对我来说听起来很简单,但我想不通。

哦,我对可以为我处理所有位图内容的神奇库不感兴趣,我无法使用它。

最佳答案

如果我正确地回答了你的问题,你想按照你提到的顺序访问它们。这是执行该操作的代码(给定您的 n):

for(int i = 0; i < n; i++) //which section we are going through
{
for(int row = 0; row < size; row++) //size = 16, better use on of your constants
{
for(int col = 0; col < size; col++)
{
int pixelIndex = size * (row * n) + col + size * i;
/*the last one is an offset - it moves the
index to the right as far as we need.
If you need two coordinates (as in (x,y))
instead of one number, it is: */
int x = row, y = col + size * i;
doSomethingWithPixel(pixelIndex);
}
}
}

希望这对您有所帮助。

关于c++ - 一种在一维数组(位图)内迭代矩形区域的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29803370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com