Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

python - Looking for solution on calculating distance between two curves

I need to calculate minimum and maximum distance between two curves (normal) i.e. points from one curve have a perpendicular on another. What i have done so far:

from sympy import *
from pandas import DataFrame
init_printing(use_unicode=False, wrap_line=False)
x = Symbol('x')

F=x+1 #first curve
G=x**2 #second curve
#on the interval [1:5]

My_list =np.transpose( [np.arange(1, 5, 1)] )
df = DataFrame(My_list, columns=['x']) # x array to data frame
df['yF'] = df['x']+1  #adding first function
df['yG'] = df['x']** 2 #adding second function
df['r']= abs(df['yF']-df['yG']) #calculating distance
df.describe()  #to look min-max

The problem is: distances are not really a perpendicular to a curve... solution does not look optimal.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are NOT finding the perpendicular distance. You are finding the minimum of the y-values with fixed x values. Your steps need to look like this:

  1. find all perpendicular lines to one of the curves.
  2. find the corresponding intersection point on the other curve.
  3. calculate the distance.

Note that this method is not commutative, the perpendicular distance from f to g may not be the same as the perpendicular distance from g to f. This is because you are not guaranteed that if a line is perpendicular to curve f that it must also be perpendicular to curve g.

Some math:

  1. the derivative of F is 1
  2. the derivative of G is 2*X
  3. the perpendicular line of Y=X+1 at a point x is Y=-X+(2*x+1).
  4. the perpendicular line of Y=X**2 at a point x is Y=-1/(2*x)*X+(x**2+0.5)
  5. the intersection points could have more than one solution

I will show in numpy.

import numpy as np

#Functions, d is the derivative
F  = lambda x : x+1  
dF = lambda x : np.ones(x.shape)
G  = lambda x : x**2
dG = lambda x : 2*x

#Domain
X  = np.arange(1,5,1)

#We calculate the distance from G to F
P_m = -1/dG(X)                                  #Perpendicular slopes
P_b = X**2+0.5                                  #The y-intercept
C   = (2*X**3-X)/(2*X+1)                        #The x-coor of the intersection
D   = np.sqrt((X-C)**2+((P_m*X+P_b)-F(C))**2)   #Distance
print(D.min())

#Now the other way (this way has two intersection points). 
P_m = -1/dF(X)                                  #The perpendicular slopes 
P_b = 2*X+1                                     #The y-intercepts 
C1  = 0.5*(-P_m+np.sqrt(P_m**2+4*P_b))          #First solution
C2  = 0.5*(-P_m-np.sqrt(P_m**2+4*P_b))          #Second solution 
D1  = np.sqrt((X-C1)**2+((P_m*X+P_b)-G(C1))**2) #Euclidian distance to first solution
D2  = np.sqrt((X-C2)**2+((P_m*X+P_b)-G(C2))**2) #Euclidian distance to second solution
D   = np.concatenate([D1,D2])                   #Distance
print(D.min())                                  #Minimum distance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...