MATLAB: How to make a point in matlab AVI video moving in a radom path

matlab avi 2dvideo

I made a 2D AVI animation in Matlab, there are one green dot and one blue dot moving along a line into different directions. But now i want to add into this AVI animation video another dot, which move in a random path together with the other two dots, what should i write codes here now ? attached below is my codes. Thanks very much for your help!
%time steps
N=20;
%start position of one moving object
x_pos=10;
y_pos=10;
x1_pos=100;
y1_pos=100;
%speed of the moving object
speed_x=3;
speed_y=2;
speed_x1=-4;
speed_y1=-2;
%ini white image with size 100 x 150
image=ones(100,150,3);
%initialization of videowriter
outputVideo = VideoWriter('test.avi');
outputVideo.FrameRate = 5; %set frame rate of the image
open(outputVideo)
%drawing a moving object on an image plane and creating the video
for i=1:N
%update states of moving object
x_pos=x_pos+speed_x;
y_pos=y_pos+speed_y;
x1_pos=x1_pos+speed_x1;
y1_pos=y1_pos+speed_y1;
%draw the measurement on the image using a green point with specific size
size=4;
image((x_pos-size/2):(x_pos+size/2),(y_pos-size/2):(y_pos+size/2),1)=0;
image((x_pos-size/2):(x_pos+size/2),(y_pos-size/2):(y_pos+size/2),3)=0;
image((x1_pos-size/2):(x1_pos+size/2),(y1_pos-size/2):(y1_pos+size/2),2)=0;
image((x1_pos-size/2):(y1_pos+size/2),(y1_pos-size/2):(y1_pos+size/2),1)=0;
%give the image to the video writer object
writeVideo(outputVideo,image)

Best Answer

See my attached demo that you can adapt to do that.