Need help with fractal tree plot.

4 views (last 30 days)
Catherine
Catherine on 12 Apr 2013
Hi all,
I'm working on a project right now that requires me to construct a function that takes 5 inputs (x1,y1,x2,y2,r) and returns a plot of a fractal tree. The inputs correspond to the endpoints of the "trunk" and r is the number of recursions you wish the function to execute.
For each level drawn two new branches should be drawn at a 90 degree angle from each previous branch. For example, for r=1, two new branches (a left branch and a right branch) would be drawn from the point (x2,y2).
Here is what I have so far (be aware that it is very incomplete):
%To run this file in the command window, call the function fractal_tree1 %and input the appropriate arguments. This function takes input arguments %of staring enpoint and final endpoint of the first branch and the number %levels you wish to be iin your tree.
function recursions = fractal_tree1(x1, y1, x2, y2,r) theta = pi/2; endpoint1 = [x1; y1]; endpoint2 = [x2; y2]; recursions = zeros(2); plot([x1 x2],[y1 y2]) %Draws the initial line that will be the "trunk". hold on
if r > 0 function right_branches = draw_right_branch(x1,y1,x2,y2) theta = pi/2; R_r = [cos(theta) sin(theta);-sin(theta) cos(theta)]; v_12 = [x2-x1;y2-y1]; v_rotated_r = (R_r*v_12)*0.6; endpointnew_r = v_rotated_r+endpoint2; x1 = endpointnew_r(1); y1 = endpointnew_r(2); plot([x2 x1],[y2 y1]) end
function left_branches = draw_left_branch(x1,y1,x2,y2)
theta = pi/2;
R_l = [cos(-theta) sin(-theta);-sin(-theta) cos(-theta)];
v_rotated_l = (R_l*v_12)*0.6;
endpointnew_l = v_rotated_l+endpoint2;
x1 = endpointnew_l(1);
y1 = endpointnew_l(2);
plot([x2 x1],[y2 y1])
end
end
hold off end
If you have any suggestions as to what i can do from here, let me know!

Answers (1)

Iman Ansari
Iman Ansari on 12 Apr 2013
Hi This works for r=1:
%To run this file in the command window, call the function fractal_tree1
%and input the appropriate arguments. This function takes input arguments
%of staring enpoint and final endpoint of the first branch and the number
%levels you wish to be iin your tree.
function recursions = fractal_tree1(x1, y1, x2, y2,r)
theta = pi/2;
endpoint1 = [x1; y1];
recursions = zeros(2);
plot([x1 x2],[y1 y2]) %Draws the initial line that will be the "trunk".
hold on
if r > 0
draw_left_branch(x1,y1,x2,y2);
draw_right_branch(x1,y1,x2,y2);
end
hold off
end
function left_branches = draw_left_branch(x1,y1,x2,y2)
theta = pi/2;
R_l = [cos(-theta) sin(-theta);-sin(-theta) cos(-theta)];
v_12 = [x2-x1;y2-y1];
v_rotated_l = (R_l*v_12)*0.6;
endpoint2 = [x2; y2];
endpointnew_l = v_rotated_l+endpoint2;
x1 = endpointnew_l(1);
y1 = endpointnew_l(2);
plot([x2 x1],[y2 y1])
end
function right_branches = draw_right_branch(x1,y1,x2,y2)
theta = pi/2;
R_r = [cos(theta) sin(theta);-sin(theta) cos(theta)];
v_12 = [x2-x1;y2-y1];
v_rotated_r = (R_r*v_12)*0.6;
endpoint2 = [x2; y2];
endpointnew_r = v_rotated_r+endpoint2;
x1 = endpointnew_r(1);
y1 = endpointnew_r(2);
plot([x2 x1],[y2 y1])
end

Categories

Find more on Fractals in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!