python - 使用 Python Matplotlib 使用 Start、End、Center 和 Radius 将弧绘制为多边形
问题描述
我使用了下面的代码,只有输入start_point
和end_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
)
[解释: 将圆弧转换为连接为直线的点集。我需要这些点作为列表]
解决方案
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()
推荐阅读
- json - Webpack:如何为 JSON 添加 js 文件的内容哈希
- javascript - React Native If 语句在状态下无法正常运行
- php - 使用 PHP 和 cURL 调用 web 服务 MS .Net 到公开并返回值的函数
- java - 用户 UID 与 Firestore Android 中的文档 ID 不同
- google-cloud-platform - 如何在 Google Cloud 中创建类似于 Salesforce 插件的自定义插件?
- swift - 查找 AVPictureInPictureController 的共享实例以获取 isPictureInPictureActive 值
- mongodb - $indexOfArray 按数组元素 mongoDB 内的对象属性搜索
- python - 在屏幕而不是相机上显示蒙版对象检测
- python - 在匹配查询的数据框中查找最后一次出现的连续(时间)行
- powershell - 如何在 Go 中查询 Windows 计划任务信息?