dimanche 1 août 2021

randomly draw pixels on a screen with unequal probability distribution

I want to draw randomly pixels on a screen whereby the probability should not be equally distributed.

Example: Let's say the screen has 1920 x 1080 pixels. On a draw event the probability to be drawn for pixels which lie in a 100 x 100 rectangle at position (500,500) should be 10 times higher then for pixels outside the rectangle.

To achieve this I create first an array which contains the probability. The positions inside the rectangle get a value of 10, all other positions get a value of 1.

 for i := 1 to 1920 do
 begin
   for j := 1 to 1080 do
   begin
     FProbability[i, j]:=1;
     if InRange(i, 500, 600) and InRange(j, 500, 600) then
     begin
       FProbability[i, j]:=10;
     end;
   end;
 end;

Then I make a list of all pixels:

 FPixelList:=TList<TPoint>.Create;
 for i := 1 to 1920 do
 begin
   for j := 1 to 1080 do
   begin
     for k := 1 to FProbabilty[i, j] do
     begin
       FPixelList.Add(TPoint.Create(i,j))
     end;
   end;
 end;

The pixel list has now 10 entries for each pixel inside the rectangle and 1 entry for all other pixel positions.

On a draw event I get the pixel position to be drawn by

FPixelList[RandomRange(0, FPixelList.Count-1)]

This works fine.

However I was wondering if there are other solutions for this problem. My solution uses a lot of memory if the screen sizes become bigger and I can only use integer values for the probability.




Aucun commentaire:

Enregistrer un commentaire