在直线的末端画一个四分之一圆
Draw a quarter circle at the end of a straight line
我有一些点的坐标。我的任务是获取这些点的方向,并找到未来可能的点在计算方向上的位置。为此,我计划了以下 -
- 为点拟合一条线
- 在拟合线的末端画一个四分之一圆。按照常识,四分之一圆可能不是正确的选择。但是,这是另一个问题的一部分,必须以这种方式解决。
我正在使用以下代码来拟合一条线
from matplotlib import pyplot as plt
from scipy import stats
x = [1,2,3,2,5,6,7,8,9,10]
y = [2,4,11,8,8,18,14,11,18,20]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
line = [slope*i+intercept for i in x]
plt.plot(x, line)
假设,拟合线上的两点是(9,17)和(10,19)。如何在 (10,19) 沿直线方向绘制半径为 5 的四分之一圆?
最终,我将得到一个点位置,我必须检查该点是否落在四分之一圆内,我想这可以用 shapely 完成。
要检查点P是否落在四分之一圆内,你可以找到到线端B的距离([=21的长度=]BP)和单位线方向向量d与向量BP
夹角的余弦值
distance = sqrt(BP.x * BP.x + BP.y * BP.y)
cosine = (d.x * BP.x + d.y * BP.y) / (distance)
if (distance < radius) and (cosine >= sqrt(2)/2)
P in sector
单位向量 d 可能是根据您已有的数据计算得出的:
d.x = sign(slope) * sqrt(1/(1+slope**2))
d.y = sqrt(slope**2/1+slope**2)
注意分量的符号没有明确定义(因为两个相反的向量有相同的斜率)
要解决主要问题 - 可以使用旋转的(Pi/4)方向矢量计算圆弧的终点
cf = sqrt(2)/2
arcbegin.x = b.x + radius * d.x * cf - radius * d.y * cf
arcbegin.y = b.y + radius * d.x * cf + radius * d.y * cf
arcend.x = b.x + radius * d.x * cf + radius * d.y * cf
arcend.y = b.y - radius * d.x * cf + radius * d.y * cf
我认为您应该按如下方式实现 arch。 (我只是展示了你缺少的逻辑,你必须添加你的情节)。祝你好运
from matplotlib import pyplot as plt
from scipy import stats
x = [1,2,3,2,5,6,7,8,9,10]
y = [2,4,11,8,8,18,14,11,18,20]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
line = [slope*i + intercept for i in x]
# Logic Part *****************************************************
from matplotlib.patches import Arc
import math
# circuile parameters
R = 5
xEnd,yEnd = 10 , 20 #Your end point cords, in your case Point B
LowerThita = math.degrees(math.atan(slope)) - 45
UpperThita = math.degrees(math.atan(slope)) + 45
# Figure setup
fig, ax = plt.subplots()
ax.set_xlim(-R , (R+xEnd) * 1.05)
ax.set_ylim(-R , (R+yEnd) * 1.05)
# Arcs
ax.add_patch(Arc((xEnd, yEnd), R, R,
theta1=LowerThita, theta2=UpperThita, edgecolor='k'))
plt.show()
#NOTE : You Haft to add your line to the plot
您可以将其委托给 Shapely,而不是自己计算所有数学。
首先,借助buffer
:
在行尾创建一个圆圈
from shapely.affinity import rotate
from shapely.geometry import LineString, Point
from shapely.ops import split
a = (10, 20)
b = (15, 30)
ab = LineString([a, b]) # the line you got from linear regression
circle = Point(b).buffer(5)
现在,让我们用两条新线来分隔我们想要的扇区区域。我们将使用 rotate
将线在每个方向旋转 135º,这样扇形的中心角将为 360º - 135º * 2 = 90º,即四分之一圆:
left_border = rotate(ab, -135, origin=b)
right_border = rotate(ab, 135, origin=b)
最后用split
得到扇区:
splitter = LineString([*left_border.coords, *right_border.coords[::-1]])
sector = split(circle, splitter)[1]
从这里您可以使用 contains
方法轻松确定某个点是否位于扇区内。例如:
points_of_interest = [Point(16, 32), Point(12, 30)]
for point in points_of_interest:
print(sector.contains(point))
# True
# False
我有一些点的坐标。我的任务是获取这些点的方向,并找到未来可能的点在计算方向上的位置。为此,我计划了以下 -
- 为点拟合一条线
- 在拟合线的末端画一个四分之一圆。按照常识,四分之一圆可能不是正确的选择。但是,这是另一个问题的一部分,必须以这种方式解决。
我正在使用以下代码来拟合一条线
from matplotlib import pyplot as plt
from scipy import stats
x = [1,2,3,2,5,6,7,8,9,10]
y = [2,4,11,8,8,18,14,11,18,20]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
line = [slope*i+intercept for i in x]
plt.plot(x, line)
假设,拟合线上的两点是(9,17)和(10,19)。如何在 (10,19) 沿直线方向绘制半径为 5 的四分之一圆?
最终,我将得到一个点位置,我必须检查该点是否落在四分之一圆内,我想这可以用 shapely 完成。
要检查点P是否落在四分之一圆内,你可以找到到线端B的距离([=21的长度=]BP)和单位线方向向量d与向量BP
夹角的余弦值distance = sqrt(BP.x * BP.x + BP.y * BP.y)
cosine = (d.x * BP.x + d.y * BP.y) / (distance)
if (distance < radius) and (cosine >= sqrt(2)/2)
P in sector
单位向量 d 可能是根据您已有的数据计算得出的:
d.x = sign(slope) * sqrt(1/(1+slope**2))
d.y = sqrt(slope**2/1+slope**2)
注意分量的符号没有明确定义(因为两个相反的向量有相同的斜率)
要解决主要问题 - 可以使用旋转的(Pi/4)方向矢量计算圆弧的终点
cf = sqrt(2)/2
arcbegin.x = b.x + radius * d.x * cf - radius * d.y * cf
arcbegin.y = b.y + radius * d.x * cf + radius * d.y * cf
arcend.x = b.x + radius * d.x * cf + radius * d.y * cf
arcend.y = b.y - radius * d.x * cf + radius * d.y * cf
我认为您应该按如下方式实现 arch。 (我只是展示了你缺少的逻辑,你必须添加你的情节)。祝你好运
from matplotlib import pyplot as plt
from scipy import stats
x = [1,2,3,2,5,6,7,8,9,10]
y = [2,4,11,8,8,18,14,11,18,20]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
line = [slope*i + intercept for i in x]
# Logic Part *****************************************************
from matplotlib.patches import Arc
import math
# circuile parameters
R = 5
xEnd,yEnd = 10 , 20 #Your end point cords, in your case Point B
LowerThita = math.degrees(math.atan(slope)) - 45
UpperThita = math.degrees(math.atan(slope)) + 45
# Figure setup
fig, ax = plt.subplots()
ax.set_xlim(-R , (R+xEnd) * 1.05)
ax.set_ylim(-R , (R+yEnd) * 1.05)
# Arcs
ax.add_patch(Arc((xEnd, yEnd), R, R,
theta1=LowerThita, theta2=UpperThita, edgecolor='k'))
plt.show()
#NOTE : You Haft to add your line to the plot
您可以将其委托给 Shapely,而不是自己计算所有数学。
首先,借助buffer
:
from shapely.affinity import rotate
from shapely.geometry import LineString, Point
from shapely.ops import split
a = (10, 20)
b = (15, 30)
ab = LineString([a, b]) # the line you got from linear regression
circle = Point(b).buffer(5)
现在,让我们用两条新线来分隔我们想要的扇区区域。我们将使用 rotate
将线在每个方向旋转 135º,这样扇形的中心角将为 360º - 135º * 2 = 90º,即四分之一圆:
left_border = rotate(ab, -135, origin=b)
right_border = rotate(ab, 135, origin=b)
最后用split
得到扇区:
splitter = LineString([*left_border.coords, *right_border.coords[::-1]])
sector = split(circle, splitter)[1]
从这里您可以使用 contains
方法轻松确定某个点是否位于扇区内。例如:
points_of_interest = [Point(16, 32), Point(12, 30)]
for point in points_of_interest:
print(sector.contains(point))
# True
# False