mercredi 8 mai 2019

Prevent curve flattening in for loop

I'm trying to plot the normal for each point on my curve. When I do something odd happens and the curve completely flattens? Please could I ask how I'm using for loops wrongly?

Find Points and Tangent Vector

%matplotlib    
# Define a set of points for curve to go through
Points = [[0,1.5],[2,2],[3,1],[4,0.5],[5,1],[6,2],[7,3]]

# Calculate the Catmull-Rom splines through the points
c, cp = CatmullRomChain(Points)

# Convert the Catmull-Rom curve points into x and y arrays and plot
x,y = zip(*c)
plt.plot(x,y)

# Plot the control points
px, py = zip(*Points)
plt.plot(px,py,'or') 

Plot the normal for each point on catmull rom

###plotting normal
###c is all my coordinates in the catmull rom curve
###cp is the tangent vector for each point
qx, qy = zip(*c)
u, v = zip(*cp)

#convert tuples to list
qx_l = list(qx)
qy_l = list(qy)
u_l = list(u)
v_l = list(v)

for i in range(len(v_l)):
    u = u_l[i]  
    v = v_l[i]
    a = qx_l[i]
    b = qy_l[i]

    if u == 0.0 or v == 0.0:
        slope = 0.0
        print("minima not plotted")
    else: 
        slope = v/u
        norm_slope = (1.0)*((-1.0)/float(slope))
        d = b - ((float(norm_slope))*a)
        x3= np.linspace(a, a+0.1, 10)
        y3 = float(norm_slope)*x3 +d
        plt.plot(x3, y3)
        plt.show()

Outputted graph

Flattened curve

I think it's my for loop because if I change the indentation slightly I plot only the last normal

for i in range(len(v_l)):
    u = u_l[i]  
    v = v_l[i]
    a = qx_l[i]
    b = qy_l[i]

if u == 0.0 or v == 0.0:
    slope = 0.0
    print("minima not plotted")
else: 
    slope = v/u
    norm_slope = (1.0)*((-1.0)/float(slope))
    d = b - ((float(norm_slope))*a)
    x3= np.linspace(a, a+0.1, 10)
    y3 = float(norm_slope)*x3 +d
    plt.plot(x3, y3)
    plt.show()

Last normal

Thank you.

Aucun commentaire:

Enregistrer un commentaire