开发者

Get surrounding pixels in array

Hey. My maths isn't great so I'm hoping someone can help me with this. I have a 1D array of pixels (representing a 2d image). In order to access a specific pixel, I'm using this formula:

image.Pixels[row * imageWidth + col] = pixelColor;

This is working, but 开发者_如何学运维I would also like to include pixels around the selected pixel. What's the fastest way, without using pointers directly, to get a group of pixels around the selected pixel with a radius of r and set their values to pixelColor? I'm trying to create a paint-type app and would like to vary brush sizes, which would be dictated by the radius size. Thanks for any help.


I don't know C# specifically, but something to the effect of this should do you

for (i=-r ; i< r ; i++) {
    for (j=-(r - i); j<(r - i); j++) {
       image.Pixels[(row+i)*imageWidth + (col+j)]=pixelColour;
    }
}

Edit the above actually paints a diamond, i've given my first hack idea to do a proper circle below

for (i=-r ; i<r ; i++) {
    for (j=-r; j<r; j++) {
       if((i*i + j*j)<(r*r)){
           image.Pixels[(row+i)*imageWidth + (col+j)]=pixelColour;
       }
    }
}


The simple slow way would be to step through the range of pixels in row +- r and col +- r, and calculate the distance from row and col is not greater than r. Distance from col,row is squareroot (difference in x squared + difference in y squared).

A slightly faster way is to compare the square of the radius to the difference in x squared + difference in y squared as they are comparable.

Still faster is Bresenham's circle algorithm,

Another article: Bresenham's Line and Circle Algorithms

Once you have the distance from col for any row, you can fill the pixels from col - distance to col + distance, no need to calculate both. So you can get away with calculating only half the circle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜