Bruno Justino Praciano
Bruno Justino Praciano

Reputation: 552

Curve radius in Python

I have this curve and I would like to compute the radius and the center of the curve below: enter image description here

The data for this curve https://pastebin.com/FZTi3bAf

My code so far:

poly = np.polyfit(df['x'], df['y'],2)
p = np.poly1d(poly)

I need to have new point (x, y) as input in this problem.

Upvotes: 0

Views: 3799

Answers (2)

V. Ayrat
V. Ayrat

Reputation: 2719

You can plot the line from 2 edge points and calculate the distance from the middle of this line to curve (dh). Then if dl is the distance between edge points you can write (dl/2)^2 = (2R - dh) * dh and get R from this equation.

a = df.to_numpy()
x_middle = (a[0, 0] + a[-1, 0]) / 2
y_middle = (a[0, 1] + a[-1, 1]) / 2
dx = a[-1, 0] - a[0, 0]
dy = a[0, 1] - a[-1, 1]
dl = np.sqrt(dx ** 2 + dy ** 2)
cos = dx / dl
sin = dy / dl
dh = cos * (np.interp(x_middle, a[:, 0], a[:, 1]) - y_middle)
R = dl ** 2 / 8 / dh + dh / 2
x0 = x_middle - (R - dh) * sin
y0 = y_middle - (R - dh) * cos


plt.scatter(a[:, 0], a[:, 1])
circle = plt.Circle((x0, y0), R, fill=False)
plt.gca().add_artist(circle)
plt.show()

plot

Upvotes: 1

amzon-ex
amzon-ex

Reputation: 1744

Since the radius will obviously be variable for a non-circular curve, all it boils down to is calculating the distances of all the points from a chosen centre point.
If this centre point is your origin, distances are just sqrt(x**2 + y**2) where x, y is a particular coordinate in your dataset. If the centre is at some point x0, y0, then this becomes sqrt((x - x0)**2 + (y - y0)**2).

All of these can be very conveniently achieved by considering the points as numpy arrays.

Upvotes: 0

Related Questions