1

I have used the below code with only inputs start_point and 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')

I get the following output

enter image description here

I need the following output (I created the below output using MS Paint)

[Explanation: Converted the arc into set of points joined as straight lines. I need those set of points as list]

enter image description here

Kartheek Palepu
  • 972
  • 8
  • 29

1 Answers1

1

To obtain the coordinates along a Path that has been defined via curves (CURVE3 or CURVE4) you may use the Path's .to_polygons() method. This will give a N x 2 shaped array of coordinates.

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

enter image description here


There is no way to manipulate the number of points created by .to_polygon. If that is a requirement, you may instead create your own Bezier curve from the given points as shown below. Here we choose 32 points along the path.

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712