首页 > 解决方案 > 使用 Python Matplotlib 使用 Start、End、Center 和 Radius 将弧绘制为多边形

问题描述

我使用了下面的代码,只有输入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')

我得到以下输出

在此处输入图像描述

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

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

在此处输入图像描述

标签: pythonmatplotlib

解决方案


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

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

在此处输入图像描述


无法操纵由.to_polygon. 如果这是一个要求,您可以改为从给定点创建自己的贝塞尔曲线,如下所示。这里我们选择沿路径的 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()

在此处输入图像描述


推荐阅读