How to draw interesting Images with Matlab
This article mainly introduces how to use Matlab to draw interesting images, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Random cherry tree
Function sakura% @ author:slandarer% Random shape binary Tree Cherry Blossom Tree drawing hold on,axis equalaxis (0.5 + [- 10 lyric 50]) set (gca,'xtick', [], 'ytick', [],' xcolor','w','ycolor','w') set (gca,'color', [0.5020 0.5020 0.5020]) length_trunk=6;width_trunk=4;k1=0.9;k2=0.8;number_branch=15;alp=pi/10;length_branch=k1*length_trunk Width_branch=k2*width_trunk;trunk=; plot (trunk (:, 1), trunk (:, 2), 'color', [0 000],' Linewidth',width_trunk) begins= [trunk (2), pi/2,1]; grow=begins;plotdata= [0 000 000]; plotdata (1) = []; for i=1:number_branch control=randi (25, [length (grow (:, 1), 1]) > = 10; ag=grow (:, 3) L=length (ag); parta= [length _ branch.* k1 ^ alp (:, 4). * cos (ag+ones (lmagin1) * alp), length_branch.* k1.^ (:, 4). * sin (ag+ones (lmage1) * alp), ones (lmem1) * alp,ones (lmem1)] Partb= [length _ branch.* k1 ^ alp (:, 4). * cos (ag-ones (lmagin1) * alp), length_branch.* k1 ^ alp (:, 4). * sin (ag-ones (lmag1) * alp),-ones (lmag1) * alp,ones (lmag1)] Parta2= [0.8. * length_branch.* k1 ^ sin (:, 4). * cos (ag), 0.8.roomthymbranch.k1.^ (:, 4). * sin (ag), zeros (lprit 1), ones (lMagol 1)] Partb2= [0.8. * length_branch.* k1.^ (:, 4). * cos (ag), 0.8.roomthumbbranch.k1.^ (:, 4). * sin (ag), zeros (lmage1), ones (lmage1)]; parta=control.*parta+ (1-control). * parta2; partb=control.*partb+ (1-control). * partb2; parta=parta+grow; partb=partb+grow; congress= [parta;partb]; grow= [grow;grow] Judge= [grow,congress]; judge=unique (judge,'rows'); grow=judge (:, 5:end); plotdata= [plotdata;judge]; endfor i=1:number_branch temp_w=width_branch* 0.8 ^ I; temp_branch=plotdata (plotdata (:, 4) = = I branch:); plx= [temp _ branch (:, 1), temp_branch (:, 5)]; ply= [temp _ branch (:, 2), temp_branch (:, 6)]; plx=plx';ply=ply' Plot (plx,ply,'color', [0.3020 0.3020] + I * [0.3020 0.6824 0.6824]. / number_branch,'Linewidth',temp_w) endbloom_pos=plotdata (plotdata (:, 8) = = number_branch+1, [5je 6]); scatter (bloom_pos (:, 1), bloom_pos (:, 2), 10 bloom_pos=plotdata bloom_pos=plotdata (plotdata (:, 8) = = number_branch, [5Power6]) Scatter (bloom_pos (:, 1), bloom_pos (:, 2), 8 end2. Apple drawing
Function appleG [upright v] = meshgrid (linspace (- pi,pi,100)); XMesh= (1+cos (v)). * cos (u) + 0.085.*cos (5.shouu) + (0.994.*v./pi). ^ 100 1+cos YMesh = (1+cos (v)). * sin (u); ZMesh= (6.*sin (v) + 2.*cos (v)-2.9.*log (1-v./pi)). / 4.2 Grid onsurf (XMesh,YMesh,ZMesh,'FaceColor', [140 ~ 192 ~ ~ 30]. / 255 ~ 5) view (45 ~ 25) hobbies light = h.Color.Color= [11]. Pumpkin drawing
% construct grid [tPowerp] = meshgrid (linspace (0rect 2pirect. 05200), linspace (0mempyr.05200);% binary piecewise function foutline=@ (tmemp) (p. 14). * (cos (20.0t) + 70). * (p. * (pi-p+.1)). ^. 2% of the spherical coordinates are transformed into XmaeY Zhanghucos (p). * foutline (tjingp); R=sin (p). * foutline (tjimp); Rendr X=cos (t). * Rpolitics Yellowsin (t). * CMap=ones ([size (t), 3]); tMap=ones (size (t)); C1 = [253 size (3)]; C1 = [253lemagne 158 cm 3]. / 255 Shield c2 = [76 magistrate 103 86]. / 255 position for iTunes 1 3 (:,:) = C1 (I); tMap (p0umbimshow (cm) 12. The shy mimosa
Function mimose% @ author:slandarer% =% basically draw a flattened and tilted semicircle together% and then multiply it by a similar arc curve so that each small leaf is different in length.% calculate a flattened and tilted semicircular curve t _ 1 (t _ 1) _ T1=t1.*cos (pi/9)-y.*sin (pi/9); y=t1.*sin (pi/9) + y.*cos (pi/9);% splice multiple flattened and tilted semicircular curves T = []; Y = []; for iLiq1 pi/9 20 T = [T, (iMuth1) * (pi+2*pi/100) + T1]; Y = [YMagy]; end% multiplied by a similar arc curve to make each leaflet different in length L=length (T) T2=linspace (pi/8,pi-2*pi/5,L); Y=Y.*sin (T2);% rotate and adjust the scale X=T.*cos (pi)-Y.*sin (pi); Y=T.*sin (pi) + Y.*cos (pi); X=X-min (X) Position', [500, 100, 500, 500] 'Numbertitle','off',... 'Color', [1 11],... 'resize','off',... 'visible','on',... 'name','mimose',... 'menubar','none'); ax=axes (' Units','pixels',... 'parent',fig,... 'Color', [0.2667 0.4039 0.2745],... 'Position', [00 500 500], 'XLim', [0100],... 'YLim', [0100],... 'XColor', [1 11],... 'YColor', [1 1 1]) Hold (ax,'on')% plot (ax,X+20,Y+50)% call the function drawing defined later = =% draw branches drawBranch ([20 pi/1.7 0], [50 sin 10]) drawBranch ([20 pi/1.7 0], [35 sin 12]) drawBranch ([0L0], [15Power45]) drawBranch ([4jue 0], [0Mague 20])% draw leaves leaf.l1=drawLeaf ([50L10] + 1.* [cos (pi/1.7), sin (pi/1.7)], pi/1.7,X,Y,0.8,1) Leaf.l2=drawLeaf ([50jing10] + 1.* [cos (- pi/8), sin (- pi/8)],-pi/8,X,Y,0.8,1); leaf.l3=drawLeaf ([50L10) + 1.* [cos (pi/12), sin (pi/12)], pi/12,X,Y,1,1); leaf.l4=drawLeaf ([50jing10] + 1.* [cos (pi/3), sin (pi/3)], pi/3,X,Y,1,1) Leaf.l5=drawLeaf ([0je 45] + 1.* [cos (pi/3), sin (pi/3)], pi/3,X,Y,1,1); leaf.l6=drawLeaf ([0Jing 30] + 1.* [cos (pi/5), sin (pi/5)], pi/5,X,Y,1,1);% draw flower flower.fl1=drawFlower ([35jue 12]); flower.fl2=drawFlower ([20jue 5]);% mouse movement callback = fps=25 Mitimer=timer ('ExecutionMode',' fixedRate', 'Period',1/fps,' TimerFcn', @ miMove); start (mitimer)% Flower and Leaf retraction function function miMove (~, ~) for ii=1:6 if leaf. (['fl',num2str (ii)]). Ratio0.6 flower. ([' fl',num2str (ii)]. Ratio-0.2 End% distance detection function, the leaf shape is almost elliptical, if the distance between the two focal points and less than a certain value is considered to be close enough, if it is close enough to return TRUE, otherwise return FALSE function bool=closeLeaf (pos1,pos2,pos3) pL=pos1+ (pos2-pos1). * 0.1; pR=pos1+ (pos2-pos1). * 0.9 LTotal=norm (pL-pos3) + norm (pR-pos3); bool=lTotal=layerPos (iMur1ListPos)-5 yPos=layerPos (iMur1ListPos)-interval; end yPos (yPos