tags:

views:

62

answers:

2

Hi all,

Is there any way to fill an ellipse or a rect by point to point like in an airbrush tool in mspaint?

I could not find a way to create an empty rect or an ellipse and then fill them up pixel by pixel or setting random pixels on screen in a circle way....

Can i tell setPixel to fill inside a dcellipse or anything like that?

10x

+2  A: 

You need to create a region with CRgn, then select that as the clipping region in your CDC with SelectClipRgn. Then you can use CDC::SetPixel to set random pixels anywhere within the bounding rectangle of your shape, and only the ones within the clipping region will be painted.

Be aware that this will be slow, and will need to be redone every time the window paints (such as when another window is dragged over it).

Mark Ransom
10x, had to do some twicks, but it work great!!! :-)
Erez
A: 

In your "make random pixels" loop, just exclude the pixel if it's outside your desired circle.

num_pixels = 20; // how many pixels
circle_radius = 32;  // 32-pixel radius, or whatever you'd like
circle_radius2 = circle_radius * circle_radius;

while (num_pixels-- > 0)
{
    // get a random number between (-circle_radius / 2, circle_radius / 2)
    pixel_x = rand(circle_radius) - circle_radius / 2; 
    pixel_y = rand(circle_radius) - circle_radius / 2; 

    // compute squared distance between generated pixel and radius, 
    // exclude if out of range
    if ( (center_x - pixel_x) * (center_x - pixel_x) + 
         (center_y - pixel_y) * (center_y - pixel_y) > circle_radius2 )
        continue; // generate another pixel

    // do stuff with pixel
} 
Seth