You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Julia中绘制两点间等角螺旋曲线的技术实现问题

生成连接两点的等角螺旋曲线(Julia实现)

需求

在2D笛卡尔坐标系中,生成包含N个点的等角螺旋曲线,连接指定两点p1和p2。

初始代码与问题

初始尝试的代码如下,但生成的曲线仅以p2为中心,端点无法靠近p1:

using PyPlot
using LinearAlgebra
p1 = [5,7]
p2 = [1,2]
r = norm(p1-p2)
theta_offset = tan((p1[2]-p2[2])/(p1[1]-p2[1]));
# Number of points
rez = 500
# Number of revolutions
rev = 5
# Radius as spiral decreases
t = range(0,r,rez)
# Angle as spiral decreases
theta = range(0, 2*pi*rev, rez) .+ theta_offset
x = cos.(theta).*exp.(-t).+p2[1];
y = sin.(theta).*exp.(-t).+p2[2];

figure()
plot(x,y)
scatter(p1[1],p1[2],c="red",s=5)
scatter(p2[1],p2[2],c="red",s=10)
show(); gcf()

修正方案

核心修正两处逻辑:

  1. 角度偏移量theta_offset需用**反正切函数atan**计算两点连线的方向角,而非正切函数tan;
  2. 螺旋坐标计算时,需将缩放因子乘以两点间的距离r,确保螺旋起点匹配p1。

修正后的关键代码片段:

theta_offset = atan((p1[2]-p2[2])/(p1[1]-p2[1]));
x = cos.(theta).*exp.(-t).*r.+p2[1]
y = sin.(theta).*exp.(-t).*r.+p2[2]

完整修正代码

using PyPlot
using LinearAlgebra
p1 = [5,7]
p2 = [1,2]
r = norm(p1-p2)
# 修正:用atan计算两点连线的方向角
theta_offset = atan((p1[2]-p2[2])/(p1[1]-p2[1]));
rez = 500  # 点数
rev = 5    # 旋转圈数
t = range(0,r,rez)
theta = range(0, 2*pi*rev, rez) .+ theta_offset
# 修正:乘以距离r,确保起点匹配p1
x = cos.(theta).*exp.(-t).*r.+p2[1]
y = sin.(theta).*exp.(-t).*r.+p2[2];

figure()
plot(x,y)
scatter(p1[1],p1[2],c="red",s=5)
scatter(p2[1],p2[2],c="red",s=10)
show(); gcf()

修正说明

  • atan((p1[2]-p2[2])/(p1[1]-p2[1]))正确计算了从p2指向p1的向量的角度,确保螺旋初始方向对准p1;
  • 乘以r后,当t=0时,exp(-t)=1,此时cos(theta[1])*r + p2[1] = p1[1],sin(theta[1])*r + p2[2] = p1[2],螺旋起点精确落在p1;随着t增大,exp(-t)逐渐趋近于0,螺旋收敛到p2,同时完成指定圈数的旋转。

内容的提问来源于stack exchange,提问作者AaronJPung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 07:15:37