Basic Matlab Fractal Function (Recursion Loop Help)

11 visualizaciones (últimos 30 días)
Ted
Ted el 22 de Abr. de 2012
I am attempting to create a function that creates a T-patterned fractal by rotating points and drawing lines as the counter increases. However, I cannot get past the first T shape. If I enter in additional branches (3,4 or 5), I simply get increased horizontal lines drawn onto the left branch only. Where has my code gone wrong?
close all; clear all; clc;
% Main
length = 1;
ratio = .5;
branches = 2;
theta = 90*pi/180;
xold =0;
yold =0;
index =1;
while index<=branches
if index==1
xnew =length*cos(theta);
ynew =length*sin(theta);
figure(1); hold on;
axis ([-1,1, -1,1])
line([xold, xnew],[yold, ynew])
else
% Draw Left
T = [cos(theta), sin(theta); -sin(theta),cos(theta)];
x =length*cos(theta);
y =length*sin(theta);
temp = T'*[x;y];
xnew = temp(1,1);
ynew = temp(2,1);
figure(1); hold on;
axis ([-1,1, -1,1])
line([xold, xold+xnew],[yold, yold+ynew])
% Draw Right
line([xold, xold-xnew],[yold, yold-ynew])
end
% figure(1); hold on;
% axis equal
% line([xold, xnew],[yold, ynew])
% line([xold, xnew],[yold, ynew])
length = length*ratio;
xold = xnew+xold;
yold = ynew+yold;
index = index+1;
end

Respuestas (2)

Walter Roberson
Walter Roberson el 22 de Abr. de 2012
It is not possible to code a recursive routine in MATLAB without using the "function" statement to name the routine. By definition, recursive code calls itself.
You might perhaps have converted a recursive routine to iterative; if so then it is no longer recursive.
  3 comentarios
Walter Roberson
Walter Roberson el 22 de Abr. de 2012
My guess is that the fractal could be done with one script, but it would not be a recursive script. For recursion it would have to be a function rather than a script (I shudder at thought of recursive scripts.)
Geoff
Geoff el 22 de Abr. de 2012
You can do recursion without function calls, but it's ugly and involves implementing your own stack. Why would you do that? That's what functions are for =)

Iniciar sesión para comentar.


Geoff
Geoff el 22 de Abr. de 2012
So the whole point of using recursion for a fractal is that you call a single function which draws your fractal. Repeated recursive calls are there to draw a smaller version of the fractal in a new location.
I don't know what you want your fractal shape to look like, but you should start with this underlying approach. Also I think you should take 'figure(1); hold on;' out of the loop and do it only once.
So what minimum parameters are required to draw your fractal?
Well:
length
ratio
branches
theta
xold
yold
I don't know about index. Let's use these to start.
Now some of these control the shape of the fractal (ratio, branches, theta) and others control the size and position (length, xold, yold).
I'm gonna take branches out and assume 2. Because I can't see from your code how you would deal with more than 2. But that'll be obvious in a bit.
So you now have an empty function that draws your fractal:
function DrawFractalT( pos, length, ratio, theta, maxIter )
Note the maxIter which controls the recursion limit. Every time you make a recursive call, you'll reduce this by 1. At the top of your function you need this:
maxIter = maxIter - 1;
if maxIter < 0
return;
end
The first thing to do is draw the stalk, using your transformation matrix to rotate a vector that describes your stalk, and offset it by the root position.
% Draw stalk
T = [cos(theta), -sin(theta); sin(theta), cos(theta)];
nextpos = pos(:) + T * [0;length];
line( [pos(1) nextpos(1)], [pos(2) nextpos(2)] );
Actually, that's probably all you need to do. Cos each branch of your fractal is a new 'root' with a different angle. This is what I think you're doing anyway.
% Draw branches
dtheta = pi / 2;
DrawFractalT( ??? ); % left
DrawFractalT( ??? ); % right
You work out what goes in place of the ???. =)
And you're done.
end
Invoke like this:
length = 1;
ratio = 0.5;
theta = 0;
maxIter = 10;
f = figure;
hold on;
DrawFractalT( [0 0], length, ratio, theta, maxIter );
hold off;

Categorías

Más información sobre Fractals en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by