% % triangle_recursive.m Demonstrating the recursion and % graphics interaction % function triangle_recursive(level,a,b,len,factor) % % % x(1)=a; x(2)=x(1)+len; x(3)=x(2)-len/2; % should be 2 not factor. x(4)=x(1); %-------------------- y(1)=b; y(2)=y(1); y(3)=y(2)+len*sin(pi/3); y(4)=y(1); % for i=1:3 line([x(i) x(i+1)],[y(i) y(i+1)]); if(level==1) % text(x(i),y(i),num2str(i)); end end % i % for i=1:level len_small=len/(factor*1.0); triangle_recursive(level-1,x(1),y(1),len_small,factor); triangle_recursive(level-1,x(2)-len_small,y(2),len_small,factor); xt=x(3)-(len_small*cos(pi/3)); yt=y(3)-(len_small*sin(pi/3)); triangle_recursive(level-1,xt,yt,len_small,factor); end