Loop script, changes colours and save each image as jpeg
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I would really be grateful of some help please!
Im playing the script attached - but am trying to do the following.
1) Loop the script such that I can it say 50 times
2) Alter the background colour randomly per iteration of the loop
3) Alter the tree colour randomnly per iteration of the loop
4) Save each image generated as a jpeg.
Thank you!
function Tree(r)
r=4
figure('Position',[50,50,700,700]); %image size
drawBranch([0,0],90,r,r);
axis equal
axis off
fig = gcf;
fig.Color = [1,0.843,0.5]; %Sets background colour
function drawBranch(pt,angle,remainingIterations,r)
width=5*(remainingIterations/r);
len1=5*remainingIterations;
ang1=angle+15;
ang2=angle+7;
ang3=angle-7;
ang4=angle-15;
y1=len1*sind(ang1)+pt(2);
x1=len1*cosd(ang1)+pt(1);
y2=len1*sind(ang2)+pt(2);
x2=len1*cosd(ang2)+pt(1);
y3=len1*sind(ang3)+pt(2);
x3=len1*cosd(ang3)+pt(1);
y4=len1*sind(ang4)+pt(2);
x4=len1*cosd(ang4)+pt(1);
ang22=ang2-5;
ang33=ang3+5;
y22=len1/2*sind(ang22)+y2;
x22=len1/2*cosd(ang22)+x2;
y33=len1/2*sind(ang33)+y3;
x33=len1/2*cosd(ang33)+x3;
c=[0 1-(remainingIterations/r) 1-0.5*(remainingIterations/r)];
p1=plot([pt(1),x1],[pt(2),y1],'LineWidth',width,'Color',c);
hold on
p2=plot([pt(1),x2],[pt(2),y2],'LineWidth',width,'Color',c);
p3=plot([pt(1),x3],[pt(2),y3],'LineWidth',width,'Color',c);
p4=plot([pt(1),x4],[pt(2),y4],'LineWidth',width,'Color',c);
p5=plot([x2,x22],[y2,y22],'LineWidth',width,'Color',c);
p6=plot([x3,x33],[y3,y33],'LineWidth',width,'Color',c);
p1.Color(4)=0.8;
p2.Color(4)=0.8;
p3.Color(4)=0.8;
p4.Color(4)=0.8;
p5.Color(4)=0.8;
p6.Color(4)=0.8;
if remainingIterations-1>0
drawBranch([x1,y1],ang1,remainingIterations-1,r);
drawBranch([x22,y22],ang22,remainingIterations-1,r);
drawBranch([x33,y33],ang33,remainingIterations-1,r);
drawBranch([x4,y4],ang4,remainingIterations-1,r);
end
end
set(gcf, 'InvertHardcopy', 'off');
saveas(fig,'Tree%d.png');
end
0 commentaires
Réponses (4)
Sulaymon Eshkabilov
le 3 Nov 2021
Is this what you want to achieve:
r=1:50;
for ii=1:numel(r)
Tree(r(ii))
end
function Tree(r)
figure('Position',[50,50,700,700]); %image size
drawBranch([0,0],90,r,r);
axis equal
axis off
fig = gcf;
fig.Color = [1,0.843,0.5]; %Sets background colour
function drawBranch(pt,angle,remainingIterations,r)
width=5*(remainingIterations/r);
len1=5*remainingIterations;
ang1=angle+15;
ang2=angle+7;
ang3=angle-7;
ang4=angle-15;
y1=len1*sind(ang1)+pt(2);
x1=len1*cosd(ang1)+pt(1);
y2=len1*sind(ang2)+pt(2);
x2=len1*cosd(ang2)+pt(1);
y3=len1*sind(ang3)+pt(2);
x3=len1*cosd(ang3)+pt(1);
y4=len1*sind(ang4)+pt(2);
x4=len1*cosd(ang4)+pt(1);
ang22=ang2-5;
ang33=ang3+5;
y22=len1/2*sind(ang22)+y2;
x22=len1/2*cosd(ang22)+x2;
y33=len1/2*sind(ang33)+y3;
x33=len1/2*cosd(ang33)+x3;
c=[0 1-(remainingIterations/r) 1-0.5*(remainingIterations/r)];
p1=plot([pt(1),x1],[pt(2),y1],'LineWidth',width,'Color',c);
hold on
p2=plot([pt(1),x2],[pt(2),y2],'LineWidth',width,'Color',c);
p3=plot([pt(1),x3],[pt(2),y3],'LineWidth',width,'Color',c);
p4=plot([pt(1),x4],[pt(2),y4],'LineWidth',width,'Color',c);
p5=plot([x2,x22],[y2,y22],'LineWidth',width,'Color',c);
p6=plot([x3,x33],[y3,y33],'LineWidth',width,'Color',c);
p1.Color(4)=0.8;
p2.Color(4)=0.8;
p3.Color(4)=0.8;
p4.Color(4)=0.8;
p5.Color(4)=0.8;
p6.Color(4)=0.8;
if remainingIterations-1>0
drawBranch([x1,y1],ang1,remainingIterations-1,r);
drawBranch([x22,y22],ang22,remainingIterations-1,r);
drawBranch([x33,y33],ang33,remainingIterations-1,r);
drawBranch([x4,y4],ang4,remainingIterations-1,r);
end
end
set(gcf, 'InvertHardcopy', 'off');
saveas(fig,'Tree%d.png');
end
0 commentaires
Ben Foster
le 3 Nov 2021
1 commentaire
Sulaymon Eshkabilov
le 4 Nov 2021
Now, you have made an error here. Use this syntax:
r=1:10; % To simulate between 1:10
for ii=1:numel(r)
Tree(r(ii)) % Input argument names MUST match with the function
end
function Tree(r) % Don't make any changes from this point and on
...
...
end
Voir également
Catégories
En savoir plus sur Fractals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!