Mokus
Mokus

Reputation: 10400

How can I generate an arc in numpy?

If I know the center(x,y,z) of the arc and the diameter, and the starting and ending point, how can I generate the values between the start and the end?

Upvotes: 4

Views: 4519

Answers (1)

Hooked
Hooked

Reputation: 88198

It sounds like your "arc" is an circular approximation to a curve between two known points. I guessing this from the word "diameter" (which is twice the radius) in your post. To do this you parameterize the circle (t) -> (x,y) where t goes from 0..2pi. Given a center, two end points and a radius we can approximate a portion of the curve like this:

from numpy import cos,sin,arccos
import numpy as np

def parametric_circle(t,xc,yc,R):
    x = xc + R*cos(t)
    y = yc + R*sin(t)
    return x,y

def inv_parametric_circle(x,xc,R):
    t = arccos((x-xc)/R)
    return t

N = 30
R = 3
xc = 1.0
yc = 3.0

start_point = (xc + R*cos(.3), yc + R*sin(.3))
end_point   = (xc + R*cos(2.2), yc + R*sin(2.2))


start_t = inv_parametric_circle(start_point[0], xc, R)
end_t   = inv_parametric_circle(end_point[0], xc, R)

arc_T = np.linspace(start_t, end_t, N)

from pylab import *
X,Y = parametric_circle(arc_T, xc, yc, R)
plot(X,Y)
scatter(X,Y)
scatter([xc],[yc],color='r',s=100)
axis('equal')
show()

enter image description here

This example is only in 2D, but it is easily adaptable since the curve will always lie along the plane between the two points and the center.

Upvotes: 11

Related Questions