[英]How to calculate distance between a point and an intersection line in python?
I am rusty in my math, not sure how to calculate the distrance from the highest point H to the intersection between the 2 lowest points in the middle for the N point. 我的数学很生疏,不确定如何计算从最高点H到中间N点的中间2个最低点之间的交点的距离。
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')
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
I looked at your chart. 我看了你的图表。 The line is not perpendicular. 该线不垂直。 It is a vertical line from a point to a line segment. 它是从点到线段的垂直线。
Assuming your apex point is (x0,y0) and your base points are (x1,y1) and (x2,y2): 假设您的顶点是(x0,y0)并且您的基点是(x1,y1)和(x2,y2):
The equation of the line joining 2 points (x1,y1) and (x2,y2) is: 连接两个点(x1,y1)和(x2,y2)的线的方程为:
y = (y2-y1)/(x2-x1) * x + (y2 * (x2-x1) - x1 * (y2-y1)) / (x2-x1)
Get the y intercept on the the line: 在行上获取y截距:
ymid = (y2-y1)/(x2-x1) * x0 + (y2 * (x2-x1) - x1 * (y2-y1)) / (x2-x1)
Your distance is: 您的距离是:
y0 - ymid
See http://pythonfiddle.com/SO-33162756/ 看到http://pythonfiddle.com/SO-33162756/
This doesn't answer your question directly but perhaps you would want to checkout the awesome python module shapely
. 这并不能直接回答您的问题,但是也许您想shapely
地检查一下python 很棒的模块。
You can create geometric objects such as LineStrings
, Points
using the module. 您可以使用模块创建几何对象,如LineStrings
, Points
。
And a simple call to: 和一个简单的调用:
object.project(other[, normalized=False]) object.project(other [,normalized = False])
Returns the distance along this geometric object to a point nearest the other object. 返回沿着该几何对象到最接近另一个对象的点的距离。
will give you your answer. 会给你答案。
Here's its documentation: Shapely Documentation 这是其文档: 整齐的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.