如何在MATLAB或Python中生成3D螺旋?

问题描述 投票:-2回答:1

我编写了一个代码来生成螺旋的x,y,z点并得到了这个结果:

What I got

码:

clear all; delete all, clc;
% Spiral constants
THETA_0 = 5; % constant
THETA_1 = 10.3; % starting angle
A = 3.762;
B = 0.001317;
C = 7.967;
D = 0.1287;
E = 0.003056;

s=2;
% Calculate (x,y,z) coordinates of points defining the spiral path
theta = THETA_1:.1:910.3;  % range, starting degree angle:final degree angle
for i = 1:length(theta)
    if (theta(i)<=99.9)
        R(i) = C*(1-D*log(theta(i)-THETA_0));
    else
%       theta_mod = 0.0002*theta(i)^2+.98*theta(i);
        R(i) = A*exp(-B*theta(i));
    end
    % scaling 
    x(i) = s*R(i)*cosd(theta(i));
    y(i) = s*R(i)*sind(theta(i));
    z(i) = s*E*(theta(i)-THETA_1);
end

helix=animatedline('LineWidth',2);
axis equal;
axis vis3d;
% set (gca,'XLim', [-5 5],'YLim', [-10 10], 'ZLim',[0 6])
view(43,24);
hold on;
for i=1:length(z)
    addpoints(helix, x(i),y(i),z(i));
    head=scatter3 (x(i),y(i),z(i));
    drawnow
%   pause(0.01);
    delete(head);
end

我希望它周围有一个类似于它的螺旋结构

Intended 3D

python matlab 3d figure helix
1个回答
0
投票

你的第二张照片给你:

  • x,y和z的参数方程。
  • uv的限制

您拥有创建几何图形的所有必要信息:

%plot the mesh
u=linspace(0,4*pi,50);
v=linspace(0,2*pi,50);
[u,v]=meshgrid(u,v);
x=(1.2+0.5*cos(v)).*cos(u);
y=(1.2+0.5*cos(v)).*sin(u);
z=0.5*sin(v)+u/pi;
surf(x,y,z)
hold on

%plot the 3d line
u = linspace(0,4*pi,40)
x=1.2.*cos(u);
y=1.2.*sin(u);
z=u/pi;
plot3(x,y,z,'r');

axis equal

现在您只需调整参数方程以适合您的线。

enter image description here

编辑:要将相同的解决方案应用于您的特定情况,您只需将uv替换为theta函数中的Rmeshgrid变量:

THETA_0 = 5; % constant
THETA_1 = 10.3; % starting angle
A = 3.762;
B = 0.001317;
C = 7.967;
D = 0.1287;
E = 0.003056;

s=2;
% Calculate (x,y,z) coordinates of points defining the spiral path
theta = THETA_1:5:910.3;
for i = 1:length(theta)
    if (theta(i)<=99.9)
        R(i) = s*C*(1-D*log(theta(i)-THETA_0));
    else
        R(i) = s*A*exp(-B*theta(i));
    end

end

x = R.*cosd(theta);
y = R.*sind(theta);
z = E.*(theta-THETA_1);
plot3(x,y,z,'r','linewidth',2)

hold on
[u,v]=meshgrid(theta,R);
x=(R+0.5*cos(v)).*cosd(u);
y=(R+0.5*cos(v)).*sind(u);
z=0.5*sin(v)+E.*(u-THETA_1);
mesh(x,y,z,'facecolor','none')

axis equal

结果:

enter image description here

顺便说一下,我不是在同一个剧本中混合cosdcos的忠实粉丝,但做你想做的事。

© www.soinside.com 2019 - 2024. All rights reserved.