- /
-
The Matrix : Laboratory
on 31 Oct 2024
- 55
- 176
- 0
- 3
- 1382
Cite your audio source here (if applicable):
% Audio Source:
% Loopop - Juno In The Space Maze
% from YouTube Audio Library
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent text_str offsets ycoor xcoor
numChars = 48;
numColumns = 200;
if f==1
figure;
set(gca,'Position',[0 0 1 1])
% Generate random characters within the specified range
randomUnicodeCodes = randi([33, 126], numChars, numColumns);
text_str = cellstr(char(randomUnicodeCodes(:)));
% offsets the start of animation for a given column
offsets = randi([0,95],numColumns,1);
xcoor = randi([0,600],numColumns,1);
ycoor = randi([-300,0],numColumns,1);
end
im = zeros(600,600,3);
textSize = 18;
ydelta = 1.2 * textSize;
trailLength = 32;
% Precompute row and column indices
[cIdx, rIdx] = meshgrid(1:numColumns, 1:numChars);
cIdx = cIdx(:);
rIdx = rIdx(:);
% Calculate position matrix
position = [xcoor(cIdx), ycoor(cIdx) + rIdx * ydelta];
% Calculate color matrix
progress = mod(offsets(cIdx) + f, 96);
inTrail = (2 * rIdx <= progress) & (2 * rIdx > progress - trailLength);
clr = zeros(numChars * numColumns, 3);
clr(inTrail, 2) = 1 - (progress(inTrail) - 2 * rIdx(inTrail)) / trailLength;
clr(inTrail, 1) = clr(inTrail, 2) / 2;
clr(inTrail, 3) = clr(inTrail, 1) / 2;
% Condition for on-screen positions
onScreenIdx = (position(:,1) >= 1 & position(:,1) <= 600 & ...
position(:,2) >= 1 & position(:,2) <= 600);
% Condition for non-black colors
nonBlackIdx = any(clr ~= 0, 2);
% Combine conditions
validIdx = onScreenIdx & nonBlackIdx;
% Filter position, text, and color arrays
filteredPosition = position(validIdx, :);
filteredText = text_str(validIdx);
filteredClr = clr(validIdx, :);
% Insert only the valid text
im = insertText(im, filteredPosition, filteredText, FontSize=textSize, ...
BoxOpacity=0, TextColor=filteredClr);
% the character at the beginning of the trail
for c=1:numColumns
im = insertText(im,[xcoor(c) ycoor(c)+mod(offsets(c) + f, 96)/2*ydelta], text_str(c+f), FontSize=textSize, ...
BoxOpacity=0, TextColor=[0.5 1 0.3]);
end
imshow(im)
end