% movie indicated the output movie. N, F and filename are the arguments:
% N is the matrix's size, F is the frame number, filename is the name of
% file
function movie = my_sierpinski_video(N, F, filename)
% Let's create an avi object file named by filename (avifile function)
% And if you like you can add the parameter fps to 25, it's optional.
movie = avifile(filename, 'fps', 25);
% First, we create random 3 points for argument P
% Initial random coordinates of 3 points and their opponents (x and y)
% floor(rand*N + 1) would create a random point between 1 and N (the boundary)
p1.x = floor((rand*N+1));
p1.y = floor((rand*N+1));
p2.x = floor((rand*N+1));
p2.y = floor((rand*N+1));
p3.x = floor((rand*N+1));
p3.y = floor((rand*N+1));
% D means direction, I used String array but you can use number array too.
D = {'up' 'down' 'left' 'right'};
% Now we will create initial direction values of points so that
% they will follow this direction
p1D = D(floor(rand*4)+1);
p2D = D(floor(rand*4)+1);
p3D = D(floor(rand*4)+1);
% i will be from 1 to F, because F is frame's number of video we'll create.
for i=1:F
% BEGIN SWITCH STATEMENT
% Right now, we could create a function to use the below switch
% statement but that'd increase the number of function files. So you
% can create it if you like.
% Switch statement will ask for the direction of each points.
% The structure of switch statement is same for all points. You can
% copy and paste, just change the point's name.
% If the direction is up, it'll reduce Y-coordinate by 1.
% Same logic for other directions, you can have a look:
switch char(p1D) % we must chane String to character, don't forget
case 'up'
if p1.y == 1
p1D = 'down';
p1.y = p1.y;
else
p1.y = p1.y - 1;
end
case 'down'
if p1.y == N
p1D = 'up';
p1.y=p1.y;
else
p1.y = p1.y + 1;
end
case 'left'
if p1.x == 1
p1D = 'right';
p1.x=p1.x;
else
p1.x = p1.x - 1;
end
case 'right'
if p1.x == N - 1
p1D = 'left';
p1.x=p1.x;
else
p1.x = p1.x + 1;
end
end
switch char(p2D)
case 'up'
if p2.y == 1
p2D = 'down';
p2.y = p2.y;
else
p2.y = p2.y - 1;
end
case 'down'
if p2.y == N
p2D = 'up';
p2.y=p2.y;
else
p2.y = p2.y + 1;
end
case 'left'
if p2.x == 1
p2D = 'right';
p2.x=p2.x;
else
p2.x = p2.x - 1;
end
case 'right'
if p2.x == N - 1
p2D = 'left';
p2.x=p2.x;
else
p2.x = p2.x + 1;
end
end
switch char(p3D)
case 'up'
if p3.y == 1
p3D = 'down';
p3.y = p3.y;
else
p3.y = p3.y - 1;
end
case 'down'
if p3.y == N
p3D = 'up';
p3.y=p3.y;
else
p3.y = p3.y + 1;
end
case 'left'
if p3.x == 1
p3D = 'right';
p3.x=p3.x;
else
p3.x = p3.x - 1;
end
case 'right'
if p3.x == N - 1
p3D = 'left';
p3.x=p3.x;
else
p3.x = p3.x + 1;
end
end
% END SWITCH STATEMENT
% BEGIN ADDING EACH FRAME TO MOVIE
% Creating a P argument with 3 points and giving it to sierpinski function
% imshow would create an image in black and white format, you can use
% another functions such as imagesc.
imshow(my_sierpinski(N, [p1.y p1.x; p2.y p2.x; p3.y p3.x]));
% Add each frame to movie. getframe is a function get's the current
% image's snapshot and saves it. frame will hold that snapshot.
% and by using addframe function, we'll add this snapshot to our movie.
frame = getframe;
movie = addframe(movie, frame);
% END ADDING EACH FRAME TO MOVIE
end
% THE END:
% Close function will say that I'm done, let's finish the movie.
movie = close(movie);
% Don't forget the message. lol.
fprintf('I''M DONE BABE, WHAT ABOUT YOU? :)\n');