【问题标题】:intersection between line and closed curve直线与闭合曲线的交点
【发布时间】:2020-04-07 16:59:30
【问题描述】:

我正在尝试计算直线和闭合曲线(存储在文件中)之间的交点

我尝试适应this

import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.optimize import bisect
import numpy as np
import csv
from scipy.interpolate import interp1d
from scipy.optimize import bisect
#reading curve from file
r = ...
z = ...


# vertical line
x = 1.885, 1.885
y = [-3,3]



z_ves = interp1d(r_ves, z_ves, fill_value="extrapolate")
plt.figure(num=None, figsize=(10, 6), dpi=100, facecolor='w', edgecolor='k')
plt.plot(r_ves, z_ves(r_ves))
plt.axvline(x=1.885,ymin=-3,ymax=3)


# #use interp1d to get interpolated points
y = interp1d(x, y, fill_value="extrapolate")
# stress = interp1d(strain, stress)
# #set starting points
x1 = max(x[0], r_ves[0])
x2 = min(x[-1], r_ves[-1])
max_err = .01
# #create function
f = lambda x : z_ves(x) - y(x)
#find x1 where f(x1) = 0
x1 = bisect(f, x1, x2, xtol = .001)
y1 = z_ves(x1)
#
plt.figure(num=None, figsize=(10, 6), dpi=100, facecolor='w', edgecolor='k')
plt.plot(r_ves, z_ves(r_ves))
plt.plot(x, y(x))
plt.scatter(x1, y1)
plt.show()

我收到一条运行时警告,因为我认为这条线是垂直的。

RuntimeWarning:在乘法中遇到无效值 y_new = 坡度*(x_new - x_lo)[:, None] + y_lo

我知道有两个路口(见图)

如何获得交叉点?

【问题讨论】:

  • 只是因为它可能会为您提供更好的答案:这不是曲线。完全没有。它只是一个多边形,谷歌中的“python 线多边形查找交点”可能会在几分钟内为您提供答案。

标签: python line curve


【解决方案1】:

函数y 应该插入垂直线,但您已经知道它的等式:x=1.885。因此,将给定的x 值插入曲线方程应该会给您需要求解的方程,以获得交点的y-坐标。 z_ves(1.885) 应该为您提供所需的结果交点。

【讨论】:

【解决方案2】:

我用 shapely 解决了

from shapely.geometry import LineString,Polygon
LOS1 = LineString([(1.885,-5), (1.885,3)])
VesselCoordTuple=list(zip(r_ves, z_ves))
polygonVesselBound = Polygon(VesselCoordTuple)
x1 = polygonVesselBound.intersection(LOS1)
r1 = x1.xy[0][0]
z1 = x1.xy[1][0]
r2 = x1.xy[0][1]
z2 = x1.xy[1][1]
plt.figure(1, figsize=SIZE, dpi=90) #1, figsize=(10, 4), dpi=180)
plt.plot(r_ves, z_ves)
plt.plot(x1.xy[0][0],x1.xy[1][0], color='r',linestyle=' ',marker='x',markersize=5)
plt.plot(x1.xy[0][1],x1.xy[1][1], color='r',linestyle=' ',marker='x',markersize=5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多