import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
def draw_arrow(v, **kwargs):
pt.arrow(0, 0, v[0], v[1], length_includes_head=True,
head_width=0.1, **kwargs)
def set_up_plot():
pt.xlim([-2,2])
pt.ylim([-2,2])
pt.gca().set_aspect("equal")
pt.grid()
Let's grab some vector $v$:
np.random.seed(18)
v = np.array([1.6, 0.4])
v
set_up_plot()
draw_arrow(v, color="blue")
Now let's find a unit normal vector $n$:
b = np.random.randn(2)
b = b - b.dot(v)/v.dot(v)*v
n = b/la.norm(b, 2)
Check:
print("Norm =", la.norm(n))
print("n.v =", n.dot(v))
Plot:
set_up_plot()
draw_arrow(v, color="blue")
draw_arrow(n, color="red")
Now let's grab a point $p$ and compute $r$:
np.random.seed(355342)
p = np.random.randn(2)
r = p.dot(n)
set_up_plot()
draw_arrow(v, color="blue")
draw_arrow(n, color="red")
draw_arrow(p)
Next, evaluate signed distance on a grid of points in the plane:
xs = np.linspace(-2, 2, 100)
ys = np.linspace(-2, 2, 100)
# make an grid of points in the plane
xys = np.empty((2, 100, 100))
xys[0] = xs
xys[1] = xs.reshape(-1, 1)
signed_dist = (
xys[0]*n[0] + xys[1]*n[1] - r
)
set_up_plot()
draw_arrow(v, color="blue")
draw_arrow(n, color="red")
draw_arrow(p)
pt.imshow(signed_dist, extent=(-2, 2, 2, -2), cmap="RdBu", vmin=-3, vmax=3)
pt.colorbar()
Where is the line parallel to $v$ through $p$? (that satisfies $x\cdot n -r = 0$)?
(Hint: Look for plot values = 0.)