[Python] Find the Minimum Distance and Closest Points Between Two Lines

Python
higashi
higashi

Hi, I’m Higashi.

 

I’ll show you how to estimate the minimum distance between two nonintersecting lines and find the closest point on each.

 

Sketch of the minimum distance between the lines

Image of calculate minimum distance from two lines

Sketch of the closest points on the two lines

Image of calculate center point of common perpendicular line

 

Supply the start and end coordinates of both lines: a, b, c, and d.

Let’s get started!

 

Sponsored Links

How the search works

 

How can we find the closest pair of points numerically?

This example uses an exhaustive search over sampled points.

 

The diagrams illustrate the two steps.

Step 1: Place candidate points on both lines.

Image of calculation process

Step 2: Check pairs of candidate points and keep the pair with the smallest distance.

Image of calculation process

 

Let’s put that into code.

 

Sponsored Links

Sample code for finding the distance and closest points

This code estimates the minimum distance and the coordinates of the closest point on each line.

#import library
import numpy as np
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D

#designate two line's(ab,cd) start & end point coordinate
a=[0,0,0]
b=[1,2,3]
c=[4,5,-6]
d=[7,8,9]
line1=[a,b]
line2=[c,d]
a,b=line1[0],line1[1]
c,d=line2[0],line2[1]
ABvec=np.array([b[0]-a[0],b[1]-a[1],b[2]-a[2]])
CDvec=np.array([d[0]-c[0],d[1]-c[1],d[2]-c[2]])

#function of calculate distance
def calc_distance(s,t):
    a,b=line1[0],line1[1]
    c,d=line2[0],line2[1]
    ABvec=np.array([b[0]-a[0],b[1]-a[1],b[2]-a[2]])
    CDvec=np.array([d[0]-c[0],d[1]-c[1],d[2]-c[2]])
    PQvec=(c+CDvec*s)-(a+ABvec*t)
    distance=np.linalg.norm(PQvec)
    return distance

#function of calculate position on line
def calc_pos(s,t):
    Qpos=(c+CDvec*s)
    Ppos=(a+ABvec*t)
    return Ppos,Qpos
#function for search
def make_map(ss,tt):
    z_list=[]
    ij_list=[]
    st_list=[]
    for i in range(len(ss)):
        for j in range(len(tt)):
            s,t=ss[i],tt[j]
            z=calc_distance(s,t)
            z_list.append(z)
            ij_list.append([i,j])
            st_list.append([s,t])
    return z_list,ij_list,st_list

#setting for search
ini_max=2000
ini_min=-2000
delta=200
ss=np.linspace(ini_min,ini_max,delta)
tt=np.linspace(ini_min,ini_max,delta)
z,ij,st=make_map(ss,tt)
min_val=np.amin(np.array(z))
min_index=z.index(min_val)

#main program
minz_list=[]
for k in range(7):
    i,j=ij[min_index][0],ij[min_index][1]
    smax,smin=min(ss[i+2],ini_max),max(ss[i-2],ini_min)
    tmax,tmin=min(tt[j+2],ini_max),max(tt[j-2],ini_min)
    ss=np.linspace(smin,smax,delta)
    tt=np.linspace(tmin,tmax,delta)
    z,ij,st=make_map(ss,tt)
    min_val=np.amin(np.array(z))
    min_index=z.index(min_val)
    minz_list.append(min_val)
    Ppos,Qpos=calc_pos(st[min_index][0],st[min_index][1])
print('min_distance=', min_val)
print('mean_pos=', (Qpos+Ppos)/2)

Enter the coordinates of the four endpoints on lines 7–10.

 

Sponsored Links

Sample output

Here’s the output from running the code.

Console Result of Program

I checked the result against a hand calculation and got approximately the same answer.

 

Thanks for reading!

コメント