q78 explained

This commit is contained in:
Hemanth21k 2025-07-23 21:52:39 +00:00
parent 3d136da45b
commit dbb54197e8

View File

@ -1094,7 +1094,7 @@ P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p = np.random.uniform(-10,10,( 1,2))
def distance(P0,P1,p):
def distance_faster(P0,P1,p):
'''
Author: Hemanth Pasupuleti
Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
@ -1111,17 +1111,17 @@ def distance(P0,P1,p):
d = np.abs(np.einsum("ij,ij->i",r,v)) / norm # Shape: (n_lines,), Compute: d = |(x1-x0)*(y2-y1)-(y1-y0)*(x1-x0)|/sqrt((x2-x1)**2 + (y2-y1)**2)
return d
print(distance(P0, P1, p))
print(distance_faster(P0, P1, p))
##--------------- OR ---------------##
def distance(P0, P1, p):
def distance_slower(P0, P1, p):
T = P1 - P0
L = (T**2).sum(axis=1)
U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L
U = U.reshape(len(U),1)
D = P0 + U*T - p
return np.sqrt((D**2).sum(axis=1))
print(distance(P0, P1, p))
print(distance_slower(P0, P1, p))
< q79
Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)