Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How to calculate the distance between two points on lines in python

I have two lines. namely (x1,y1) and (x2,y2). I need to calculate the distance between the points. see my code snippets below

import numpy as np
import plotly.express as px
import plotly.graph_objects as go

x1= np.array([525468.80914272, 525468.70536016])
y1= np.array([175517.80433391, 175517.75493122])

x2= np.array([525468.81174, 525468.71252])
y2= np.array([175517.796305, 175517.74884 ])

Here is the code for the plot:

fig= go.Figure()

fig.add_trace(go.Scatter(x=x1, y=y1, name="point1"))
fig.add_trace(go.Scatter(x=x2, y=y2, name="point2"))

See the figure here

1

The black line is the distance I want to calculate

my expectations are: (0.008438554274975979, 0.0085878435595034274819)

question from:https://stackoverflow.com/questions/65915301/how-to-calculate-the-distance-between-two-points-on-lines-in-python

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

1 Answer

0 votes
by (71.8m points)

You can solve this with math library

import math

distancePointA  = math.sqrt(((x1[0] - x2[0]) ** 2) + ((y1[0] - y2[0]) ** 2))
distancePointB  = math.sqrt(((x1[1] - x2[1]) ** 2) + ((y1[1] - y2[1]) ** 2))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...