چهارشنبه, ۱۵ تیر ۱۴۰۱، ۰۶:۲۸ ب.ظ
رشد چند ضلعی حول محور z , y
در این پست یک مقدمه ای برای رشد اشکال هندسی بسته و باز حول یک مسیر معین تشریح شده
شما می توانین متغیر side چند ضلعی رشد و مسیر را تعیین کنین و تغییرات را مشاهده کنین
رشد چند ضلعی حول محور y،
%% clear commands
function xx
delete(allchild(0));close all;
clear;
clc;
%% path commands
file=matlab.desktop.editor.getActive;
try
fileDetail=dir(file.Filename);
folderName=fileDetail.folder;
catch
index=strfind( file.Filename,'\');
folderName=file.Filename(1:index(end)-1);
end
paths=genpath(folderName);
addpath(paths);
cd(folderName);
side=100;
t=linspace(0,2*pi ,side+1);
% t(6)=NaN;
side1=50;
t1=linspace(0, 2*pi ,side1+1);
% ph=0;
% x=r' *cos(t+ph);
% z=(0:side)' * ones(1,side+1);
% y=r' *sin(t+ph) ;
ph=0;
x= cos(t+ph) /5 ;
y= 0*(0:side)';
z= sin(t+ph) /5 ;
% plot3(x,y,z);hold on
% path=(t1+ph).^2;path=path/max(path)
%
% path.x=[0,1,1,1,2,2,2,1,1,0];
% path.y=0:length(path.x)-1;
path.x= cos (t1+ph)*5 ;
path.z= sin(t1+ph) *5 ;
plot3([path.x*0;path.x ] ,[path.z*0 ;path.z]*0 ,[path.z*0 ;path.z] ,'r' );hold on
[path.x,path.z]=divider(path.x,path.z,2);
[t1,t1]=divider(t1,t1,2);
% plot(path.x ,path.y , 'r');hold on
% xlabel( 'X'); ylabel( 'Y'); zlabel( 'Z')
matrixObj.x= cos(t ) ;
matrixObj.z= 0*(0:side)';
matrixObj.y= sin(t ) ;
for in=1:length(path.x)-1
[ x, y, z]=rotateParameter(matrixObj,[0,1,0],-rad2deg(t1(in)));
% path.x= cos (t + ph(in)) ;
% path.y=sin (t+ ph(in) ) ;
%
plot3(x+path.x(in),y,z+path.z(in),'b-');hold on
% surf([x+path.x(in);x+path.x(in+1) ],[y+path.y(in);y+path.y(in+1) ],[z;z],'facecolor','k');hold on; alpha(0.2)
% plot3(x ,y ,z,'b');hold on
xlabel('x');
ylabel('y');
zlabel('z');
view(3);
axis('equal')
end
end
function [ newx, newy, newz]=rotateParameter(matrixObj,azel,alpha)
u = azel(:)/norm(azel);
alph = alpha*pi/180;
cosa = cos(alph);
sina = sin(alph);
vera = 1 - cosa;
x = u(1);
y = u(2);
z = u(3);
rot = [cosa+x^2*vera x*y*vera-z*sina x*z*vera+y*sina; ...
x*y*vera+z*sina cosa+y^2*vera y*z*vera-x*sina; ...
x*z*vera-y*sina y*z*vera+x*sina cosa+z^2*vera]';
x = matrixObj.x;
y = matrixObj.y;
z = matrixObj.z;
[m,n] = size(z);
if numel(x) < m*n
[x,y] = meshgrid(x,y);
end
[m,n] = size(x);
newxyz = [x(:) , y(:) , z(:) ];
newxyz = newxyz*rot;
newx = reshape(newxyz(:,1),m,n);
newy = reshape(newxyz(:,2),m,n);
newz = reshape(newxyz(:,3),m,n);
end
نمونه خروجی:

رشد چند ضلعی حول محور z:
%% clear commands
function xx
delete(allchild(0));close all;
clear;
clc;
%% path commands
file=matlab.desktop.editor.getActive;
try
fileDetail=dir(file.Filename);
folderName=fileDetail.folder;
catch
index=strfind( file.Filename,'\');
folderName=file.Filename(1:index(end)-1);
end
paths=genpath(folderName);
addpath(paths);
cd(folderName);
side=20;
t=linspace(0,2*pi ,side+1);
% t(6)=NaN;
side1=50;
t1=linspace(0, 2*pi ,side1+1);
% ph=0;
% x=r' *cos(t+ph);
% z=(0:side)' * ones(1,side+1);
% y=r' *sin(t+ph) ;
ph=0;
x= cos(t+ph) /5 ;
y= 0*(0:side)';
z= sin(t+ph) /5 ;
% plot3(x,y,z);hold on
% path=(t1+ph).^2;path=path/max(path)
%
% path.x=[0,1,1,1,2,2,2,1,1,0];
% path.y=0:length(path.x)-1;
path.x= cos (t1+ph)*5 ;
path.y= sin(t1+ph) *5 ;
plot([path.x*0;path.x ] ,[path.y*0 ;path.y] ,'r' );hold on
[path.x,path.y]=divider(path.x,path.y,2);
[t1,t1]=divider(t1,t1,2);
% plot(path.x ,path.y , 'r');hold on
% xlabel( 'X'); ylabel( 'Y'); zlabel( 'Z')
matrixObj.x= cos(t ) ;
matrixObj.y= 0*(0:side)';
matrixObj.z= sin(t ) ;
for in=1:length(path.x)-1
[ x, y, z]=rotateParameter(matrixObj,[0,0,1],rad2deg(t1(in)));
% path.x= cos (t + ph(in)) ;
% path.y=sin (t+ ph(in) ) ;
%
plot3(x+path.x(in),y+path.y(in),z,'b-');hold on
% surf([x+path.x(in);x+path.x(in+1) ],[y+path.y(in);y+path.y(in+1) ],[z;z],'facecolor','k');hold on; alpha(0.2)
% plot3(x ,y ,z,'b');hold on
view(3);
axis('equal')
end
end
function [ newx, newy, newz]=rotateParameter(matrixObj,azel,alpha)
u = azel(:)/norm(azel);
alph = alpha*pi/180;
cosa = cos(alph);
sina = sin(alph);
vera = 1 - cosa;
x = u(1);
y = u(2);
z = u(3);
rot = [cosa+x^2*vera x*y*vera-z*sina x*z*vera+y*sina; ...
x*y*vera+z*sina cosa+y^2*vera y*z*vera-x*sina; ...
x*z*vera-y*sina y*z*vera+x*sina cosa+z^2*vera]';
x = matrixObj.x;
y = matrixObj.y;
z = matrixObj.z;
[m,n] = size(z);
if numel(x) < m*n
[x,y] = meshgrid(x,y);
end
[m,n] = size(x);
newxyz = [x(:) , y(:) , z(:) ];
newxyz = newxyz*rot;
newx = reshape(newxyz(:,1),m,n);
newy = reshape(newxyz(:,2),m,n);
newz = reshape(newxyz(:,3),m,n);
end
نمونه خروجی:

دانلود هر دو کد:
۰۱/۰۴/۱۵