I am trying to draw a 20x20 size bitmap at random positions on the screen. I want that the bitmaps should not overlap each other and should be scattered uniformly.
For accomplishing this, I am generating the positions as below -
void function() {
int bitmap_size = 20*20;
int i, width = 320, height = 480;
int numslots = width*height/bitmap_size;
int *array = malloc(sizeof(int)*numslots);
for(i=0; i<numslots; i++)
array[i] = i;
// Randomize pixel indices
for(i=0; i<numslots; i++) {
int x = (rand()%(numslots-i)) + i;
swap(array+i, array+x);
}
// Generate 20 random positions for bitmaps
for(i=0; i<20; i++) {
int pos = array[i];
pos *= bitmap_size;
int y = pos / height;
int x = pos % width;
printf("%d, %d\n", x, y);
}
free(array);
}
However, when i run this code, the pixel positions are somewhat cluttered as you can see from the sample output (x, y) below for 20 positions -
240, 85
160, 255
80, 37
240, 115
80, 280
160, 118
0, 223
80, 170
80, 147
240, 45
0, 253
160, 225
160, 261
0, 66
80, 230
80, 30
0, 236
80, 27
160, 238
0, 200
Aucun commentaire:
Enregistrer un commentaire