Q:

Using Vector projection to find distance from point to line

I'm trying to calculate the perpendicular distance between a point and a line. I'm using vector projection to plot the vector rejection. However, when I plot the vector rejection it is not at a right angle to the original vector as it should be. Can anyone help please?

The green line and orange line should be perpendicular, but aren't. 
x=np.linspace(1,40,10000)
y=3+.5*x
xini, yini=1,3.5
plt.plot(x,y,label='vector1')
am=np.stack((x,y))
am[:,0]
xy,length=am.shape
xatom,yatom=2,3

xm=am[0,-1]
ym=am[1,-1]
Vector1=[xatom-xini,yatom-yini,0]
Vector2=[xm-xini,ym-yini,0]
unitb=Vector2/np.linalg.norm(Vector2)
impactvector=Vector1-unitb*np.dot(Vector1, unitb)

plt.plot(x,y)
plt.scatter(xatom,yatom)
b=np.linalg.norm(impactvector)
plt.plot(x,yatom+(impactvector[1]/impactvector[0])*(x-xatom))
plt.xlim(0,4)
plt.ylim(2,4)

A:

It is actually at a right angle; you are being fooled by the different scales used for your X and Y axis.
If you adjust the scales to be equal, the graph will show perpendicularity.
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':

    x = np.linspace(1, 40, 10000)
    y = 3 + 0.5 * x
    xini, yini = 1, 3.5
    plt.plot(x, y, label='vector1')
    am = np.stack((x, y))
    xy, length = am.shape
    xatom, yatom = 2, 3

    xm = am[0, -1]
    ym = am[1, -1]
    Vector1 = [xatom - xini, yatom - yini, 0]
    Vector2 = [xm - xini, ym - yini, 0]
    unitb = Vector2 / np.linalg.norm(Vector2)
    impactvector = Vector1 - unitb * np.dot(Vector1, unitb)

    plt.plot(x, y)
    plt.scatter(xatom, yatom)
    b = np.linalg.norm(impactvector)
    plt.plot(x, yatom + (impactvector[1] / impactvector[0]) * (x - xatom))
    plt.xlim(0, 4)
    plt.ylim(2, 4)

    plt.axis('equal')

    plt.show()

