使用Matlab画出圆台圆锥圆柱

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

用Matlab画圆柱圆台圆锥自己建一个M文件,命名任意,如tu.m在命令窗口输入:tu%以下为tu.m文件clcclearall%圆柱[X,Y,Z]=cylinder(15.7,20);h=1.22*Z;surf(X,Y,h);holdon%圆台a=25/180*pi;%可修改b=12.06/180*pi;%可修改r=3.2;%可修改h1=r*sin(a+b)/sin(a-b)*sin(a);%h1=0.66;X1=[001.22];%底面中心坐标X2=[001.22+h1];%顶面中心坐标L2=15.7-2.6/tan(a);r=[15.7L2];n=20;cyl_color='b';closed=1;lines=1;[cone,EndPlate1,EndPlate2]=Cone(X1,X2,r,n,cyl_color,closed,lines)%调用圆台的函数%圆锥[X2,Y2,Z2]=cylinder(L2:-0.2:0);h2=1.5;%可修改Z2=h2*Z2;Z2=Z2+ones(size(Z2))*(1.22+h1);surf(X2,Y2,Z2);gridon%r=0:0.1:3.2;R=0:0.1:15.7;%alpha=0:pi/20:2*pi;%角度[0,2*pi]%x=R*cos(alpha);%y=R*sin(alpha);%a=39.0;b=12.06;%h=1.22+R.*tan(b)+r.*sin(a);%[x,y]=meshgrid([-4:0.5:4]);%surf(h)新建一个M文件,如下命名为Cone.Mfunction[Cone,EndPlate1,EndPlate2]=Cone(X1,X2,R,n,cyl_color,closed,lines)%%Thisfunctionconstructsacylinderconnectingtwocenterpoints%%Usage:%[Cone,EndPlate1,EndPlate2]=Cone(X1,X2,R,n,cyl_color,closed,lines)%%Cone-------Handleofthecone%EndPlate1------HandleoftheStartingEndplate%EndPlate2------HandleoftheEndingEndplate%X1andX2arethe3x1vectorsofthetwopoints%Ristheradiusofthecylinder/coneR(1)=startradius,R(2)=endradius%nistheno.ofelementsonthecylindercircumference(more--refined)%cyl_coloristhecolordefinitionlike'r','b',[0.520.520.52]%closed=1forclosedcylinderor0forhollowopencylinder%lines=1fordisplayingthelinesegmentsonthecylinder0foronly%surface%%TypicalInputs%X1=[101010];%X2=[352040];%r=[15];%n=20;%cyl_color='b';%closed=1;%%NOTE:ThereisaMATLABfunctioncylindertorevolveacurveaboutan%axis.ThisCylinderprovidesmorecustomizationlikedirectionandetc%圆台的函数,文件名为Cone.m%CalculatingthelengthoftheConelength_cyl=norm(X2-X1);%Creating2circlesintheYZplanet=linspace(0,2*pi,n)';xa2=R(1)*cos(t);xa3=R(1)*sin(t);xb2=R(2)*cos(t);xb3=R(2)*sin(t);%CreatingthepointsintheX-Directionx1=[0length_cyl];%Creating(Extruding)thecylinderpointsintheX-Directionsxx1=repmat(x1,length(xa2),1);xx2=[xa2xb2];%xx2=repmat(x2,1,2);xx3=[xa3xb3];%xx3=repmat(x3,1,2);%Drawingtwofilledcirlcestoclosethecylinderifclosed==1holdonEndPlate1=fill3(xx1(:,1),xx2(:,1),xx3(:,1),'r');EndPlate2=fill3(xx1(:,2),xx2(:,2),xx3(:,2),'r');end%PlottingthecylinderalongtheX-Directionwithrequiredlengthstarting%fromOriginCone=mesh(xx1,xx2,xx3);%DefiningUnitvectoralongtheX-directionunit_Vx=[100];%Calulatingtheanglebetweenthexdirectionandtherequireddirection%ofConethroughdotproductangle_X1X2=acos(dot(unit_Vx,(X2-X1))/(norm(unit_Vx)*norm(X2-X1)))*180/pi;%Findingtheaxisofrotation(singlerotation)toroatetheConein%X-directiontotherequiredarbitrarydirectionthroughcrossproductaxis_rot=cross([100],(X2-X1));%RotatingtheplottedConeandtheendplatecirclestotherequired%anglesifangle_X1X2~=0%RotationisnotneededifrequireddirectionisalongXrotate(Cone,axis_rot,angle_X1X2,[000])ifclosed==1rotate(EndPlate1,axis_rot,angle_X1X2,[000])rotate(EndPlate2,axis_rot,angle_X1X2,[000])endend%TillnowConehasonlybeenalignedwiththerequireddirection,but%positionstartsfromtheorigin.soitwillnowbeshiftedtotheright%positionifclosed==1set(EndPlate1,'XData',get(EndPlate1,'XData')+X1(1))set(EndPlate1,'YData',get(EndPlate1,'YData')+X1(2))set(EndPlate1,'ZData',get(EndPlate1,'ZData')+X1(3))set(EndPlate2,'XData',get(EndPlate2,'XData')+X1(1))set(EndPlate2,'YData',get(EndPlate2,'YData')+X1(2))set(EndPlate2,'ZData',get(EndPlate2,'ZData')+X1(3))endset(Cone,'XData',get(Cone,'XData')+X1(1))set(Cone,'YData',get(Cone,'YData')+X1(2))set(Cone,'ZData',get(Cone,'ZData')+X1(3))%SettingthecolortotheConeandtheendplatesset(Cone,'AmbientStrength',1,'FaceColor',cyl_color,'FaceLighting','gouraud');%,'EdgeColor','none')ifclosed==1set([EndPlate1EndPlate2],'AmbientStrength',1,'FaceColor',cyl_color,'FaceLighting','gouraud');%,'EdgeColor','none')elseEndPlate1=[];EndPlate2=[];end%Iflinesarenotneededmakingitdisapeariflines==0set(Cone,'EdgeAlpha',0)end%shadingfaceted%facetedflatinterp;%camlight;%light;%lightinggouraud;%flatgouraudphongnonematerialshiny;%shinydullmetal%colormap(bone)%camlightheadlight;%light('Style','local','Position',[7200500]);%light('Style','local','Position',[0480500]);参考文献:

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功