Mfc can anyone help with the algorithm for the airbrush I just don't understand how
Is there a way to fill an ellipse or straight line by points, like in the airbrush tool in mspaint?
I couldn't find a way to create an empty rectangle or ellipse and then fill them pixel by pixel, or set random pixels on the screen in a circular fashion.
Can I tell setPixel to fill inside dcellipse or something?
10x
a source to share
You need to create a region with CRgn , then select that as clipping region in your CDC with SelectClipRgn . Then you can use CDC :: SetPixel to set random pixels anywhere in your shape's bounding box, and only those in the clipping region will be colored.
Remember this will be slow and will need to be redone every time a window is painted (for example, when another window is dragged over it).
a source to share
In the "make random pixels" loop, simply exclude a pixel if it is outside the 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
}
a source to share