function drawTriangle(x,y,level)
% x and y are 3-vectors that define the vertices of a triangle.
% Draw recursively colored triangles.
% level is an integer that specifies the level of the recursion

maxLevel= 5; % Maximum recursion level

if level == maxLevel
% Recursion limit reached. Color the triangle yellow.
fill(x,y,'y')
else
% Draw the triangle
plot([x x(1)],[y y(1)],'k')
% Draw and color the interior triangle mauve
a = [(x(1)+x(2))/2 (x(2)+x(3))/2 (x(3)+x(1))/2];
b = [(y(1)+y(2))/2 (y(2)+y(3))/2 (y(3)+y(1))/2];
pause(.1)
fill(a,b,'m')
pause(.1)
% Apply the process to the three "corner" triangles
drawTriangle([x(1) a(1) a(3)],[y(1) b(1) b(3)],level+1)
pause(.1)
drawTriangle([x(2) a(2) a(1)],[y(2) b(2) b(1)],level+1)
pause(.1)
drawTriangle([x(3) a(3) a(2)],[y(3) b(3) b(2)],level+1)
pause(.1)
end