In [1]:
from sympy import *
init_printing()
%matplotlib notebook
import matplotlib.pyplot as mpl
from util.plot_helpers import plot_augmat, plot_plane, plot_point, plot_line, plot_vec, plot_vecs
Vector = Matrix # define alias Vector so I don't have to explain this during video
Point = Vector # define alias Point for Vector since they're the same thing
In [2]:
# Given point r
r = Point([1,3,0])
# line L
pL = Point([0,0,2])
vL = Vector([1,-1,0]) # note this changed in v2.2 of LA book
# plane P
nP = Vector([1,1,1])
dP = 1
# compute the following closest distances:
In [3]:
# a) d(r,O) = ||r||
r.norm()
Out[3]:
In [4]:
# b) d(L,O) = || pL - proj_{L}(pL) ||
(pL - (pL.dot(vL)/vL.norm()**2)*vL).norm()
Out[4]:
In [5]:
# c) d(P,O) = || proj_{nP}(pP) ||
pP = Matrix([1,0,0])
( (nP.dot(pP)/nP.norm()**2)*nP).norm()
Out[5]:
In [6]:
# d) u=pL-r d(r,L)=||u - proj_{L}(u)||
u = pL - r
(u - (u.dot(vL)/vL.norm()**2)*vL).norm()
Out[6]:
In [7]:
# e) v=pP-r d(r,P)=||proj_{nP}(v)||
pP = Matrix([1,0,0])
v = pP - r
( (nP.dot(v)/nP.norm()**2)*nP).norm()
Out[7]:
In [8]:
#f) d(L,P)
# STEP1: check relative orientation of L and P
print('vL·nP =', vL.dot(nP), 'so line is parallel to plane. OK to proceed...')
# STEP2: find vector w=pP-pL arbitrary points on plane nad line,
pP = Matrix([1,0,0])
w = pP - pL
# then compute component of w in perp. to the plane d(L,P)=||proj_{nP}(w)||
( (nP.dot(w)/nP.norm()**2)*nP).norm()
Out[8]:
In [9]:
# # debug viz to make sure L parallel to P
# plot_vec(5*vL, at=pL, color='b')
# plot_vec(-5*vL, at=pL, color='b')
# plot_vec(w, color='r', at=pL)
# plot_plane(nP,dP)
In [ ]:
In [ ]:
In [10]:
# Setup the variables of the exercise:
p = Point([10,10,10])
pL1 = Point([3,0,5]) # an arbitrary point on L1
pL2 = Point([6,0,0]) # an arbitrary point on L2
d = Vector([1,-2,0]) # direction vector of L1 and L2
a) Projection of $p$ onto $\ell_1$
In [11]:
# define a vector from a point on L1 to p:
v1 = p - pL1
# proj_{L1}(p) = proj_{L1}(v1) + pL1
p_proj_L1 = (d.dot(v1)/d.norm()**2)*d + pL1
p_proj_L1
Out[11]:
^ This is the point on the line $\ell_1$ that is closes to the point $p$
b) (shortest) distance form $p$ to $\ell_1$
In [12]:
# d(p, L1) = subtract from v1 the part that is perp to L1 and compute the length:
(v1 - (d.dot(v1)/d.norm()**2)*d).norm()
Out[12]:
In [13]:
# ... or compute the distance directly:
(p_proj_L1-p).norm()
Out[13]:
In [14]:
(p_proj_L1-p).norm().n() # numeric approx.
Out[14]:
In [15]:
plot_line(d, pL1) # Line L1
# vector v1 and it's decomposition into parallel-to-L1 and perp-to-L1 components
plot_vec(v1, at=pL1)
plot_vec(p_proj_L1-pL1, at=pL1, color='b')
plot_vec(p-p_proj_L1, at=p_proj_L1, color='r')
ax = mpl.gca()
mpl.xlim([0,10])
mpl.ylim([0,10])
ax.set_zlim([0,10])
ax.grid(True,which='both')
Answer to a) is the tip of the blue vector; answer to b) is the length of the red vector.
In [ ]:
Use a similar approach for c) and d)
In [16]:
# define a vector from a point on L2 to p:
v2 = p - pL2
In [17]:
# p_proj_L2 =
(d.dot(v2)/d.norm()**2)*d + pL2
Out[17]:
In [18]:
# d(p, L2) =
(v2 - (d.dot(v2)/d.norm()**2)*d).norm()
Out[18]:
In [19]:
(v2 - (d.dot(v2)/d.norm()**2)*d).norm().n()
Out[19]:
In [ ]:
In [ ]:
e) distance $\ell_1$ to $\ell_2$
In [20]:
# first define a vector from a point on L1 to a point on L2:
v3 = pL2 - pL1
v3
Out[20]:
In [21]:
# d(L1, L2) =
d_L1L2 = (v3 - (d.dot(v3)/d.norm()**2)*d).norm()
d_L1L2
Out[21]:
In [22]:
d_L1L2.n()
Out[22]:
In [ ]:
In [ ]:
In [ ]: