- . algorithms , O (N) ( N - / //any), , Bayer , , O (24N), .
, 32x32:

Based on ordered smoothing based on a matrix, a given matrix can be generated in a simple way:
matrix = generate_bayer(32);
for (y=0; y<height; y++) {
for (var x=0; x<width; x++) {
i = x % matrix.length;
j = y % matrix.length;
function_val = gaussian(x, y); #returns a value 0<=x<=1
scaled_val = function_val * matrix.length * matrix.length; #scale to the max element in the matrix
if (scaled_val > matrix[j][i]) {
draw_pixel(x, y);
}
}
}
Generating a matrix is somewhat more complicated, but here is an iterative approach for matrices where the side length is 2:
function generate_bayer(size) {
base = [[0, 2],
[3, 1]];
old_matrix = base;
mult = 4;
while (old_matrix.length < size) {
new_size = old_matrix.length * 2;
matrix = new Array[new_size][new_size];
for (y=0; y<old_matrix.length; y++) {
for (x=0; x<old_matrix[y].length; x++) {
matrix[y*2] [x*2] = old_matrix[y][x] + (mult * base[0][0]);
matrix[y*2] [x*2 + 1] = old_matrix[y][x] + (mult * base[0][1]);
matrix[y*2 + 1][x*2] = old_matrix[y][x] + (mult * base[1][0]);
matrix[y*2 + 1][x*2 + 1] = old_matrix[y][x] + (mult * base[1][1]);
}
}
mult *= 4;
old_matrix = matrix;
}
return old_matrix;
}
source
share