2010-04-26 22 views

回答

2

你需要創建一個區域與CRgn,然後在CDC選擇作爲裁剪區域與SelectClipRgn。然後,您可以使用CDC :: SetPixel在形狀的邊界矩形內的任意位置設置隨機像素,並且只會繪製剪切區域內的像素。

請注意,這會很慢,並且每次窗口繪製時都需要重新進行(例如當另一個窗口被拖動時)。

+0

10x,不得不做一些twicks,但它工作很棒! :-) – Erez 2010-04-26 22:20:15

1

在您的「製作隨機像素」循環中,只要將像素排除在所需的圓圈外即可。

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 
} 
相關問題