gpt4 book ai didi

多行斜接连接的算法

转载 作者:行者123 更新时间:2023-12-03 16:19:13 25 4
gpt4 key购买 nike

例如,为了绘制道路或墙壁,我们知道如何计算 2 条连接线上的斜接连接,结果如下
Miter join on 2 lines
但是,当 3 条或更多条线连接到同一点时,是否有一种算法可以有效地计算良好连接的点,就像这样
Join on 5 lines
Join on 3 lines
输入是段列表
注意:最终目标是对这个进行三角测量

最佳答案

假设您的线段(红色)加入了笛卡尔坐标系的原点。通过它们与您选择的轴的角度来识别您的线段,假设是 x 轴。
围绕线段绘制墙壁(黑色),假设它们与红色线段的宽度不同,t₁ 和 t₂。
现在的想法是找到相交向量的坐标。如您所见,菱形立即从这个几何图形中出现。做一些向量计算可以让你很容易地得到答案。
Scheme of the situation
Mathematical explanation for formulaes
这是一个使用 MatPlotLib 进行说明的 Python 小脚本:

import matplotlib.pyplot as plt
import math

angles = [1.0, 2.0, 2.5, 3.0, 5.0] # list of angles corresponding to your red segments (middle of your walls)
wall_len = [0.05, 0.1, 0.02, 0.08, 0.1] # list of half-width of your walls
nb_walls = len(angles)

for a in angles:
plt.plot((0, math.cos(a)), (0, math.sin(a)), "r", linewidth=3) # plotting the red segments

for i in range(0, len(angles)):
j = (i + 1)%nb_walls
angle_n = angles[i] # get angle Θ_n
angle_np = angles[j] # get the angle Θ_n+1 (do not forget to loop to the first angle when you get to the last angle in the list)
wall_n = wall_len[i] # get the thickness of the n-th wall t_n
wall_np = wall_len[j] # get the thickness of the n+1-th wall t_n+1
dif_angle = angle_np - angle_n # ΔΘ
t1 = wall_n/math.sin(dif_angle) # width of the rhombus
t2 = wall_np/math.sin(dif_angle) # height of the rhombus

x = t2*math.cos(angle_n) + t1*math.cos(angle_np) # x coordinates of the intersection point
y = t2*math.sin(angle_n) + t1*math.sin(angle_np) # y coordinates of the intersection point

wall_n = [math.cos(angle_n), math.sin(angle_n)] # for plotting n-th wall
wall_np = [math.cos(angle_np), math.sin(angle_np)] # for plotting n+1-th wall

plt.plot((x, wall_n[0] + x), (y, wall_n[1] + y), "k") # plotting the n wall
plt.plot((x, wall_np[0] + x), (y, wall_np[1] + y), "k") # plotting the n+1 wall

plt.show()
Illustration

关于多行斜接连接的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67166198/

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