【问题标题】:euclidean distance to a "curved" structure到“弯曲”结构的欧几里得距离
【发布时间】:2021-06-26 03:46:02
【问题描述】:

我编写了一个程序来处理当前资源到几何图形表面的欧几里得距离。 在我们的几何考虑中,y 坐标始终为零——因此它是 x 和 z 维度的二维结构。 该图显示了它的外观:

(length yellow = 100 , length blue = 500) - 例如,如果原点位于第二个黄色部分的中间,则点源位于 coord=(700,0,510) 的坐标处当然,可以通过以下方式为蓝色或黄色部分的每个中点计算欧几里得距离:

sqrt(sum(((中点坐标))^2)

但是,如果我从某个部分通过角度 $\alpha$ 来“弯曲”这个“棍子”,我该怎么做呢?:

y 坐标再次为零,并且源再次垂直放置在 z 方向上 - 但是我如何确定到截面中心的欧几里得距离? 所以我们使用相同的长度尺寸,只是我们将棒在电极方向弯曲 30%,例如在原点或 5 段。最好的方法是相应地操纵中点的“x”值....

【问题讨论】:

  • 您是否正在寻找一种方法来计算“弯曲”段的中点位置?或者你有吗?
  • @PatrickHappel 是的,我想计算它们——因为这样我就可以简单地采用 eucl。距离
  • 如果我理解你的问题,它实际上与欧几里得距离无关。您只是在问如何找到部分的中心,对吗?
  • @YvesDaoust 是的
  • 那我不明白你为什么接受@omg 的回答,它根本没有解决这个问题。

标签: matlab geometry computational-geometry


【解决方案1】:

我现在无法访问 Matlab,所以我用 python 编写了一些代码。这是你想做的吗?:

# plotting function of a polyline, a point and, if chosen, midpoints of polyline
def plot_polyline(P, P0, equal_axes, show_midpoints):
    M = midpoints_of(P)
    plt.figure()
    plt.plot(P0[0], P0[1], 'bo')
    plt.plot(P[:,0], P[:,1])
    for k in range(P.shape[0]):
        plt.plot(P[k,0], P[k,1], 'ro')
    if show_midpoints:
        for k in range(M.shape[0]):
            plt.plot(M[k,0], M[k,1], 'yo')
    axx = plt.gca()
    if equal_axes:
        axx.set_aspect('equal')
    plt.show()
    return None

def cos_sin(angle):
    return  math.cos(angle), math.sin(angle)

# generate a rotation matrix that rotates a 2D vector at an angle alpha
# observe the matrix format is [x1, y1] = [x, y].dot([[cos, -sin],
#                                                     [sin, cos]])
# and rotation is clockwise rotation of vector written as row-vectors 
def rot_matrx(angle):
    cs, sn = cos_sin(angle)
    return np.array([[  cs, -sn], 
                     [  sn,  cs]]) 

# performs a clockwise rotation of a point around a center at an angle 
def rotate(points, angle, center):
    U = rot_matrx(angle)
    return (points - center).dot(U) + center
 
# bending
def perform_bending(polyline, angle, index_bending_point):    
    x1 = polyline.copy()  
    x_bend  = x1[index_bending_point,:]
    # rotate half angle around point x_bend first         
    x1[0:index_bending_point,:] = rotate(x1[0:index_bending_point,:], 
                                               angle/2, x_bend) 
    # rotate the result from above half angle more around point x_(bend-1)   
    x_bend = x1[(index_bending_point-1),:]
    x1[0:(index_bending_point - 1),:] = rotate(x1[0:(index_bending_point - 1),:], 
                                                   angle/2, x_bend)      
    return x1

# calculate the array of midpoints of the polyline
def midpoints_of(polyline):
    k, m = polyline.shape
    return (polyline[0:(k-1), :] + polyline[1:k, :]) / 2

# calculate the distances from point to the midpoints of a polyline
def calc_distances(point, mids_of_polyline):
    return np.linalg.norm(point-mids_of_polyline, axis=1)


   
# initialize example:   
a = 2 # yellow segments' lenght
b = 5 # blue segments' lenght
n = 12 # number of segments + 1, number of points separating segments

alpha = 40  # angle of bending in degrees
alpha = math.pi * 40/180 # angle in bending in radians

# generate [0, 1, 2, 3, 4, 5, 6, 7]
I = np.arange(0,n)

# generate the x[0,] and x[1,] coordinates of the polygonal line
x = np.empty((n, 2), dtype=float)
I = np.floor(I / 2)*(a+b) + (I % 2)*a
x[:, 0] = I.T
x[:, 1] = 0

# point on x that is coordinate origin
x0 = (x[2,:] + x[3,:])/2
# shift initial polyline:
x = x - x0

# index of bending point
i_bend=5

x_bent = perform_bending(x, angle=alpha, index_bending_point=i_bend) 
x_mids = midpoints_of(x_bent)

# point
p = np.array([x_mids[i_bend-1, 0], 5.1])
    
dist = calc_distances(p, x_mids)

plot_polyline(x, p, equal_axes=True, show_midpoints=False)
plot_polyline(x_bent, p, equal_axes=True, show_midpoints=True)
plot_polyline(x_bent, p, equal_axes=True, show_midpoints=False)

print(dist)

【讨论】:

    【解决方案2】:

    你也可以通过二分搜索来做同样的事情,并找到曲线上离给定点最近的点(通过中点)。

    另外,如果你有曲线的封闭形式,用z = f(x)表示,给定点(x_1, 0, z_1),你可以解决以下优化问题:

    argmin_{x} (x_1 - x)^2 + (z_1 - f(x))^2
    

    上述优化任务的解是曲线上离给定点最近的点。

    为了解决优化任务,您可以根据f的封闭形式使用不同的方法。

    如果您使用的是 python,您可以分别找到许多用于优化任务的有用库。

    【讨论】:

    • 亲爱的天哪!谢谢你的回答,但我正在寻找到中点的距离。 “经典棒”的封闭形式将是一组中点,可以说 midpoints(x) = (-1000,....,0,.....1000,...2000) 和 coord=(700 ,0,510) 然后我可以计算到中点的距离。但是现在我想以 50 度的角度“弯曲”例如 0 的棒 - 然后在右侧的 x 值仍然是 (0,....1000,...2000) 但在左侧他们“更接近”电极,我现在想计算新的中点和 eucl。到它的距离
    • @Math_Man1 很高兴。正如我所解释的,您可以进行二分搜索以找到离给定点最近的中点。
    • 但我不知道你所说的 z=f(x) 的封闭形式是什么意思,因为 f 只是一个点数组
    • @Math_Man1 所以,你可以忽略第二部分。只是为了答案的完整性进行了解释。
    猜你喜欢
    • 2013-03-02
    • 2015-07-15
    • 2014-02-04
    • 1970-01-01
    • 2021-10-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多