So I'm creating a very basic fractal tree function in matlab. The function is supposed to create a fractal tree where each branch comes out at 90 degrees so it looks like a bunch of T's just put together. Currentley I am running into the problem where I can not get the branches to plot in the correct spot. I was hoping someone would be able to point me in the right direction or give me and idea of where I can fix my code to change this program.(adsbygoogle = window.adsbygoogle || []).push({});

t/here are a couple errors in the code, but overall it still works. I am not looking for those errors to be fixed, i can do that on my own. I am just looking for a way to plot my points right.

Code (Text):%To use this there are 7 input arguments, the original points Xo, Yo. The

%new points X and Y. The theta angle (given in radians), the length_ratio

%of how much you want the branch to decrease and then how many branch pairs

%you want.

function fractree_b2(Xo,Yo,X,Y,theta,length_ratio,branches)

plot([Xo X],[Yo Y],'r')

hold on

if branches <= 0

else

draw_right_branch(Xo,Yo,X,Y,theta,length_ratio,branches);

draw_left_branch(Xo,Yo,X,Y,theta,length_ratio,branches);

end

% Above coding creates the first line, the trunk and the point matrix to

% create further lines below is what will rotate the lines and create

% branches. It also calls the two imbeded functions that actually create

% the left and right branches. It also calls in a loop that will end the

% program and only draw the trunk if the value entered into branches is

% equal to or less than 0

function draw_right_branch(Xo,Yo,X,Y,theta,length_ratio,branches)

rotational_matrix_r = [cos(theta) sin(theta); -sin(theta) cos(theta)];

endpoint_1 = [Xo;Yo];

endpoint_2 = [X;Y];

endpoint_1_final = rotational_matrix_r*endpoint_1*length_ratio;

endpoint_2_final = rotational_matrix_r*endpoint_2*length_ratio;

plot([endpoint_1_final(1)+X endpoint_2_final(1)+X],[endpoint_1_final(2)+Y endpoint_2_final(2)+Y],'g')

hold on

if branches >1

draw_left_branch(Xo,Yo,X,Y,theta,length_ratio,branches)

Xo = X;

Yo = Y;

X = X*length_ratio;

Y = Y*length_ratio;

matrix_Po = [Xo;Yo];

matrix_P = [X;Y];

branch_o = rotational_matrix_r*matrix_Po;

branch = rotational_matrix_r*matrix_P;

draw_right_branch(branch_o(1),branch_o(2),branch(1),branch(2),theta,length_ratio,branches-1)

else

end

%This coding is what will draw the right branches of a the fractal

%tree. It takes the original point and multiplies it with the

%rotational matrix and length_ratio to get the proper point. Then

%an if loop is called in so the left branch function can be called

%again as well as the right branch function. Before calling the

%right branch function the Xo and X are redifined and rotated for

%when the function is called again.

end

function draw_left_branch(Xo,Yo,X,Y,theta,length_ratio,branches)

rotational_matrix_l = [cos(theta) -sin(theta); sin(theta) cos(theta)];

endpoint_1 = [Xo;Yo];

endpoint_2 = [X;Y];

endpoint_1_final = rotational_matrix_l*endpoint_1*length_ratio;

endpoint_2_final = rotational_matrix_l*endpoint_2*length_ratio;

plot([endpoint_1_final(1)+X endpoint_2_final(1)+X],[endpoint_1_final(2)+Y endpoint_2_final(2)+Y],'b')

hold on

if branches >1

draw_right_branch(Xo,Yo,X,Y,theta,length_ratio,branches-1)

Xo = X;

Yo = Y;

X = X*length_ratio;

Y = Y*length_ratio;

matrix_Po = [Xo;Yo];

matrix_P = [X;Y];

branch_o = rotational_matrix_l*matrix_Po;

branch = rotational_matrix_l*matrix_P;

draw_left_branch(branch_o(1),branch_o(2),branch(1),branch(2),theta,length_ratio,branches-1)

else

end

%This coding is what will draw the left branches of a the fractal

%tree. It takes the original point and multiplies it with the

%rotational matrix and length_ratio to get the proper point.

end

end

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Basic Fractal Tree Matlab

**Physics Forums | Science Articles, Homework Help, Discussion**