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

时间:2019-03-12 10:50:35

标签: python matlab 3d figure helix

我已经编写了生成螺旋的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

1 个答案:

答案 0 :(得分:1)

花了一些时间寻找此代码,并希望共享代码副本,并对每个变量的作用提供更多注释。如果我把它们弄错了,请纠正我,但是我玩过代码,这似乎是对的。希望这可以帮助其他最终寻找更多细节的人,因为自微积分III以来已经有一段时间了:)

    %% 3D Helical Curve
    % tested on MATLAB R2018a
    % See also https://stackoverflow.com/a/55126315/11895567
    % See also https://stackoverflow.com/q/55119694/11895567
    %
    % Code is in the state used to obtain the second of the two images shown below
    clear, clc, close all
    format compact
    
    %% Input Parameters
    res = 25;   % plot resolution; higher -> finer resolution
    R = 0.15;   % wire radius
    r = 4;      % radius to wire centerline
    t = 3;      % number of coils
    
    %% Surface of the wire
    a = 3;  % scalar for resolutoin for surface.u to make it look nicer
    b = 2;  % Set to 2 for upper and lower surfaces. Set to 1 for upper surface only
    
    surface.u = linspace(0, t*2*pi, a*res); % Path of the wire surface
    surface.v = linspace(0, b*pi, res);     % Wire surface
    [surface.u, surface.v] = meshgrid(surface.u, surface.v);
    
    surface.x = (r + R*cos(surface.v)).*cos(surface.u);
    surface.y = (r + R*cos(surface.v)).*sin(surface.u);
    surface.z = R*sin(surface.v) + surface.u/pi;
    
    %% Center line of the wire
    c = a;  % scalar for resolution for line.u to make it look nicer
    
    line.u = linspace(0, t*2*pi, c*res);
    line.x = r.*cos(line.u);
    line.y = r.*sin(line.u);
    line.z = line.u/pi;
    
    %% Plotting
    f = figure();
    grid on
    hold on
    plot3(line.x, line.y, line.z, 'r');
    surf(surface.x, surface.y, surface.z)
    view(45, 12);   % Azimuth = 45, Elevation = 12
    axis equal

样品(假设单位为毫米):

  1. 输入:R = 0.25r = 1t = 1res = 25
  • 导线半径R为0.25mm
  • 线圈半径r为1mm
  • t的线圈数是1(无单位)
  • 绘图分辨率res为25(无单位)

enter image description here

  1. 输入:R = 0.15r = 4t = 3res = 25
  • 导线半径R为0.15毫米
  • 线圈半径r为4mm
  • t的线圈数是3(无单位)
  • 绘图分辨率res为25(无单位)

enter image description here