Skip to main content
 首页 » 编程设计

python之如何在 numpy 中生成弧线

2025年05月04日11myhome

如果我知道圆弧的中心 (x,y,z) 和直径,以及起点和终点,如何生成起点和终点之间的值?

请您参考如下方法:

听起来您的“弧线”是两个已知点之间曲线的圆形近似值。我从你帖子中的“直径”这个词(它是半径的两倍)猜到这一点。为此你parameterize the circle (t) -> (x,y) 其中 t 从 0..2pi 开始。给定一个中心、两个端点和一个半径,我们可以像这样近似曲线的一部分:

from numpy import cos,sin,arccos 
import numpy as np 
 
def parametric_circle(t,xc,yc,R): 
    x = xc + R*cos(t) 
    y = yc + R*sin(t) 
    return x,y 
 
def inv_parametric_circle(x,xc,R): 
    t = arccos((x-xc)/R) 
    return t 
 
N = 30 
R = 3 
xc = 1.0 
yc = 3.0 
 
start_point = (xc + R*cos(.3), yc + R*sin(.3)) 
end_point   = (xc + R*cos(2.2), yc + R*sin(2.2)) 
 
 
start_t = inv_parametric_circle(start_point[0], xc, R) 
end_t   = inv_parametric_circle(end_point[0], xc, R) 
 
arc_T = np.linspace(start_t, end_t, N) 
 
from pylab import * 
X,Y = parametric_circle(arc_T, xc, yc, R) 
plot(X,Y) 
scatter(X,Y) 
scatter([xc],[yc],color='r',s=100) 
axis('equal') 
show() 

这个例子只是二维的,但它很容易适应,因为曲线总是沿着两点和中心之间的平面。