%Branching Tree!
%try r=5/6, theta=pi/10 and 7 iterations
function[]=tree2(r,theta,iterations)
%here are the two transformations
A1=[r*cos(theta) , -r*sin(theta) ; r*sin(theta) , r*cos(theta)];
A2=[r*cos(theta) , r*sin(theta) ; -r*sin(theta) , r*cos(theta)];
t=[0;1];
%here is the original branch
y=[0,1];
x=zeros(1,length(y));
%let's write the points in vector form
v=[x;y]; 
%Let's start with the original three branches
%here is the left branch    
v(:,3)=v(:,2);  
v(:,4)=A1*v(:,2)+t;    
%then, do the right branch
v(:,5)=v(:,2);    
v(:,6)=A2*v(:,2)+t;
%plot the original picture
hold off
plot(v(1,:),v(2,:),'Color',[0.58 0.39 0.39],'Linewidth',1)
axis('square','equal','off');
set(gcf, 'color', [0.87 0.92 0.98]);
%let's make a grassy looking floor
x=[-3:0.1:3];
hold on
plot(x,zeros(1,length(x)),'^','Color',[0.75 0.75 0],'Markerfacecolor',[0.75 0.75 0],'Markersize',4)
%Now, we can perforn the transformations on this picture, and build on it
for n=1:iterations
    for j=1:(2^(n-1))*6
    v(:,j+(2^(n-1))*6)=A2*v(:,j)+t;
    v(:,j)=A1*v(:,j)+t;
    end; 
     %plot the tree
     hold on
     for k=1:length(v(1,:))/6
     plot(v(1,6*(k-1)+1:6*k),v(2,6*(k-1)+1:6*k),'Color',[0.58 0.39 0.39],'Linewidth',1) 
     %this connects the points with lines, 
     %but only draws the connections we want to see...
     end;
     plot(v(1,:),v(2,:),'m*','Markersize',4) %the points we keep track of
end;
