如何在 MATLAB 或 Python 中生成 3D 螺旋线?
How to generate 3D helix in MATLAB or Python?
我编写了一个代码来生成螺旋的 x、y、z 点并得到了这个结果:
代码:
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
我想要一个类似于这个的螺旋结构
你的第二张照片给你:
- x、y 和 z 的参数方程。
- 对
u
和v
的限制
您已具备创建几何图形所需的所有信息:
%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
现在您只需调整参数方程以适合您的线。
编辑:
要将相同的解决方案应用于您的特定情况,您只需在 meshgrid
函数中将 u
和 v
替换为您的 theta
和 R
变量:
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
结果:
顺便说一句,我不太喜欢在同一个脚本中混合使用 cosd
和 cos
,但随心所欲。
花了一些时间寻找这个,并想分享一份代码副本,并对每个变量的作用进行更多评论。如果我说错了,请纠正我,但我已经玩过代码,这似乎是正确的。希望这能帮助那些最终寻找更多细节的人,因为微积分 III 已经有一段时间了:)
%% 3D Helical Curve
% tested on MATLAB R2018a
% See also
% See also
%
% 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
样本(假设单位为毫米):
- 输入:
R = 0.25
、r = 1
、t = 1
、res = 25
- 线径,
R
,为0.25mm
- 线圈半径,
r
,为1mm
- 圈数,
t
,为1(无量纲)
- 绘图分辨率,
res
,为 25(无单位)
- 输入:
R = 0.15
、r = 4
、t = 3
、res = 25
- 线径,
R
,为0.15mm
- 线圈半径,
r
,为4mm
- 圈数,
t
,为3(无量纲)
- 绘图分辨率,
res
,为 25(无单位)
我编写了一个代码来生成螺旋的 x、y、z 点并得到了这个结果:
代码:
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
我想要一个类似于这个的螺旋结构
你的第二张照片给你:
- x、y 和 z 的参数方程。
- 对
u
和v
的限制
您已具备创建几何图形所需的所有信息:
%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
现在您只需调整参数方程以适合您的线。
编辑:
要将相同的解决方案应用于您的特定情况,您只需在 meshgrid
函数中将 u
和 v
替换为您的 theta
和 R
变量:
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
结果:
顺便说一句,我不太喜欢在同一个脚本中混合使用 cosd
和 cos
,但随心所欲。
花了一些时间寻找这个,并想分享一份代码副本,并对每个变量的作用进行更多评论。如果我说错了,请纠正我,但我已经玩过代码,这似乎是正确的。希望这能帮助那些最终寻找更多细节的人,因为微积分 III 已经有一段时间了:)
%% 3D Helical Curve
% tested on MATLAB R2018a
% See also
% See also
%
% 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
样本(假设单位为毫米):
- 输入:
R = 0.25
、r = 1
、t = 1
、res = 25
- 线径,
R
,为0.25mm - 线圈半径,
r
,为1mm - 圈数,
t
,为1(无量纲) - 绘图分辨率,
res
,为 25(无单位)
- 输入:
R = 0.15
、r = 4
、t = 3
、res = 25
- 线径,
R
,为0.15mm - 线圈半径,
r
,为4mm - 圈数,
t
,为3(无量纲) - 绘图分辨率,
res
,为 25(无单位)