gpt4 book ai didi

python - 使用 Python Matplotlib 使用起点、终点、中心和半径将圆弧绘制为多边形

转载 作者:行者123 更新时间:2023-12-01 09:26:30 31 4
gpt4 key购买 nike

我使用了以下代码,仅输入start_pointend_point:

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

start_point = (25, 50)
end_point = (50, 25)
center = (25, 25)
radius = (25
# We need to some how automatically calculate this midpoint
mid_point = (45, 45)
verts = [start_point, mid_point, end_point]
codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3]#, Path.CLOSEPOLY]

path = Path(verts, codes)
shape = patches.PathPatch(path, facecolor='none', lw=0.75)
plt.gca().add_patch(shape)
plt.axis('scaled')

我得到以下输出

enter image description here

我需要以下输出(我使用 MS Paint 创建了以下输出)

[说明:将圆弧转换为以直线连接的点集。我需要这些点集作为列表]

enter image description here

最佳答案

要获取沿通过曲线(CURVE3CURVE4)定义的路径的坐标,您可以使用 Path.to_polygons() 方法。这将给出一个 N x 2 形状的坐标数组。

poly, = path.to_polygons()
plt.plot(*zip(*poly), marker="o", ls="None")

enter image description here

<小时/>

无法操纵 .to_polygon 创建的点数。如果这是一个要求,您可以创建您的 own Bezier curve从给定的点开始,如下所示。这里我们选择路径上的 32 个点。

import matplotlib.pyplot as plt
import numpy as np
from scipy.special import binom

bernstein = lambda n, k, t: binom(n,k)* t**k * (1.-t)**(n-k)

def bezier(points, num=200):
N = len(points)
t = np.linspace(0, 1, num=num)
curve = np.zeros((num, 2))
for i in range(N):
curve += np.outer(bernstein(N - 1, i, t), points[i])
return curve

start_point = (25, 50)
end_point = (50, 25)
mid_point = (45, 45)

nodes1 = np.array([start_point, mid_point, end_point])
curve1 = bezier(nodes1, num=32)

plt.plot(curve1[:,0], curve1[:,1], color="red", ls="", marker="o", ms=3)
plt.axis('scaled')

plt.show()

enter image description here

关于python - 使用 Python Matplotlib 使用起点、终点、中心和半径将圆弧绘制为多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50346166/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com