RosettaCodeData/Task/Peripheral-drift-illusion/Octave/peripheral-drift-illusion.octave
2025-02-27 18:35:13 -05:00

69 lines
2.1 KiB
Text

function pdi_circle(cell_size = 50, numrows = 15, numcols = 15, radius = 15, offset = 5, rotx = 2, roty = 2, color1 = [0, 0, 255], color2 = [0, 255, 0])
% creates peripheral drift illusion using circles
% pdi_circle(cell_size, numrows, numcols, radius, offset, rotx, roty, color1, color2)
% pdi_circle(50, 15, 15, 15, 5, 2, 2, [0, 0, 255], [0, 255, 0])
% color dimension
colorB = uint8([0, 0, 0]);
colorW = uint8([255, 255, 255]);
color1 = uint8(color1);
color2 = uint8(color2);
% pixels per cell
centerC = cell_size * [1 1] / 2;
[cellX, cellY] = ndgrid(1:cell_size, 1:cell_size);
cell_ones = ones(cell_size, cell_size, "uint8");
% total image size
img_size = [numrows, numcols] * cell_size
final_image = zeros(img_size(1), img_size(2), 3, "uint8");
% offset steps
stepx = 2 * pi * rotx / numrows;
stepy = 2 * pi * roty / numcols;
% loop over cells
for nr = 1:numrows, for nc = 1:numcols
% find offset centers
step_phase = (nr-1) * stepx + (nc-1) * stepy;
offsetC = offset * [cos(step_phase), sin(step_phase)];
centerB = centerC + offsetC;
centerW = centerC - offsetC;
% fill background
image1 = cell_ones * color2(1);
image2 = cell_ones * color2(2);
image3 = cell_ones * color2(3);
% fill white
insideW = sqrt((cellX - centerW(1)).^2 + (cellY - centerW(2)).^2) <= radius;
image1(insideW) = colorW(1);
image2(insideW) = colorW(2);
image3(insideW) = colorW(3);
% fill black
insideB = sqrt((cellX - centerB(1)).^2 + (cellY - centerB(2)).^2) <= radius;
image1(insideB) = colorB(1);
image2(insideB) = colorB(2);
image3(insideB) = colorB(3);
% fill foreground
insideC = sqrt((cellX - centerC(1)).^2 + (cellY - centerC(2)).^2) <= radius;
image1(insideC) = color1(1);
image2(insideC) = color1(2);
image3(insideC) = color1(3);
% generate image
offset_image = cat(3, image1, image2, image3);
final_rows = (nr-1) * cell_size + [1:cell_size];
final_cols = (nc-1) * cell_size + [1:cell_size];
final_image(final_rows, final_cols, :) = offset_image;
endfor, endfor
% show and save image
imshow(final_image)
imwrite(final_image, "PeripheralDriftOctave.png")
endfunction