多条线段的斜接算法

6
为了绘制例如道路或墙壁,我们知道如何计算两条连接线的斜接连接,得到类似于这样的结果。

Miter join on 2 lines

但是是否有一种算法可以高效地计算出一个漂亮的连接点的点数,当3条或更多条线连接到同一个点时,就像这样

Join on 5 lines Join on 3 lines

输入是一系列线段的列表。
注意:最终目标是对其进行三角剖分。
1个回答

8
假设你的线段(红色)在笛卡尔坐标系原点相交。通过它们与你选择的轴(比如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


非常棒的回答,文档和解释都非常清晰!这真的很有帮助! - ingham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接