- /
-
Scrolling rectangles
on 6 Nov 2023
- 5
- 14
- 0
- 1
- 631
drawframe(10);
Write your drawframe function below
function drawframe(f)
% Set up parameters
totalframes = 48;
width = 300;
height = 300;
rectWidth = 30;
rectHeight = 30;
% Create figure and set its size
figure();
% figure('Position', [100, 100, width, height]);
% Set the background to black and hold the plot
set(gca, 'color', 'k', 'YTick', [], 'XTick', []);
axis([0 width 0 height]);
hold on;
% Calculate positions for rectangles using timeLoop function
pos1 = timeLoop(totalframes, 0, f) * width;
pos2 = timeLoop(totalframes, 20, f) * width;
pos3 = timeLoop(totalframes, 40, f) * width;
% Draw rectangles
rectangle('Position', [pos1, 90, rectWidth, rectHeight], 'FaceColor', 'w');
rectangle('Position', [pos2, 120, rectWidth, rectHeight], 'FaceColor', 'w');
rectangle('Position', [pos3, 150, rectWidth, rectHeight], 'FaceColor', 'w');
% Hold off the plot
hold off;
end
function pos = timeLoop(totalframes, offset, frameCount)
% This helper function calculates the position of the rectangle
pos = mod(frameCount + offset, totalframes) / totalframes;
end