在python中如何计算点与交线的距离?

2024-04-30 00:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数学很生疏,不知道如何计算从最高点H到中间两个最低点交点之间的分布。在

Picture of the Chart

import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np

y= [10.5,10,12,13,10,11,16,10,9,13,10]
x= np.linspace(1, len(y), len(y), endpoint=True)
dist = np.linalg.norm(y-x)

print dst

fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(x, y, color='red')

Tags: fromimportlenmatplotlibasnpplt数学
3条回答

我看了你的图表。这条线不垂直。它是一条从点到线段的垂直线。在

假设顶点是(x0,y0),基点是(x1,y1)和(x2,y2):

连接2个点(x1,y1)和(x2,y2)的直线方程为:

y = (y2-y1)/(x2-x1) * x + (y2 * (x2-x1) - x1 * (y2-y1)) / (x2-x1)

得到直线上的y轴截距:

^{pr2}$

您的距离是:

y0 - ymid

http://pythonfiddle.com/SO-33162756/

import heapq
import operator
import math


y = [10.5,10,12,13,10,11,16,10,9,13,10]
x= np.linspace(1, len(y), len(y), endpoint=True)

y1,y2 = heapq.nsmallest(2, enumerate(y), key=operator.itemgetter(1))
x1,y1 = y1
x1 = x[x1]

x2,y2 = y2
x2 = x[x2]

m = (y2-y1)/(x2-x1)
print("Equation of line: y = {}(x-{}) + {}".format(m, x1, y1))

apexPoint = (5,4)  # or wherever the apex point is    
X,Y = apexPoint
M = 1/m
print("Equation of perpendicular line: y = {}(x-{}) + {}".format(M, X, Y))

intersect_x = ((M*X)+Y-(m*x1)-y1)/(M-m)
intersect_y = m*(intersect_x - x1) + y1

dist = math.sqrt((X - intersect_x)**2 + (Y - intersect_y)**2)  # this is your answer

这并不能直接回答您的问题,但您可能希望签出awesomepython模块shapely。在

您可以使用模块创建几何对象,如LineStringsPoints。在

一个简单的电话:

object.project(other[, normalized=False])

Returns the distance along this geometric object to a point nearest the other object.

会给你答案的。在

以下是它的文档: Shapely Documentation

相关问题 更多 >