多行斜接连接的算法

ing*_*ham 5 algorithm geometry trigonometry shapes

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

斜接 2 行

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

加入 5 行 加入 3 行

输入是段列表

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

小智 5

假设您的线段(红色)加入了笛卡尔坐标系的原点。通过它们与您选择的轴的角度来识别您的线段,假设是 x 轴。围绕线段绘制墙壁(黑色),假设它们与红色线段的宽度不同,t? 和T?。

现在的想法是找到相交向量的坐标。如您所见,菱形立即从这个几何图形中出现。做一些向量计算可以让你很容易地得到答案。

情况图

公式的数学解释

这是一个使用 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()
Run Code Online (Sandbox Code Playgroud)

插图