多行斜接连接的算法

Algorithm for miter joins on multiple lines

例如,为了绘制道路或墙壁,我们知道如何计算 2 条连接线上的斜接,结果如下所示

但是当 3 条或更多条线连接到同一点时,是否有一种算法可以有效地计算点以实现良好的连接,就像这样

输入是一个段列表

注意:最终目标是对这个进行三角测量

假设您的线段(红色)加入笛卡尔坐标系的原点。通过与您选择的轴的角度来识别您的线段,比方说 x 轴。 在线段周围绘制墙壁(黑色),假设它们的宽度与红色线段不同,t₁ 和 t2。

现在的想法是找到交点向量的坐标。如您所见,菱形立即从该几何图形中出现。做一些矢量计算可以让您轻松找到答案。

这里有一个小 Python 脚本用于说明,使用 MatPlotLib:

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()