gpt4 book ai didi

matlab - 到 "curved"结构的欧氏距离

转载 作者:行者123 更新时间:2023-12-04 03:36:11 26 4
gpt4 key购买 nike

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

picture 1

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

平方(总和(((中点坐标))^2)

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

pic2

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

最佳答案

我现在无法访问 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)

关于matlab - 到 "curved"结构的欧氏距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66865638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com