折线加宽简单思路

import matplotlib.pyplot as plt
import math

# 得出函数平移的dx和dy
def displacement( line, distance ):
    x1, y1, x2, y2 = line
    if x2 - x1 == 0:
        dx = distance
        dy = 0
    if y2 - y1 == 0:
        dx = 0
        dy = distance
    if x2 - x1 != 0 and y2 - y1 != 0:
        l_x_angle_sin = (y2 - y1) / math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
        l_x_angle_cos = (x2 - x1) / math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
        dx = distance*l_x_angle_sin
        dy = distance*l_x_angle_cos
    return dx, dy

# 函数实现直线平移
def translate_line(line, dx, dy):
    x1, y1, x2, y2 = line
    x1 = x1 + dx
    y1 += dy
    x2 += dx
    y2 += dy
    return x1, y1, x2, y2

# 得到交点
def get_intersection(line1, line2):
    x11, y11, x12, y12 = line1
    x21, y21, x22, y22 = line2
    if x12-x11 == 0:
       if x22-x21 == 0:
           return 0
       if x22-x21 != 0:
           k2 = (y22 - y21)/(x22 - x21)
           b2 = (x22 * y21 - x21 * y22)/(x22 - x21)
           return x11, x11 * k2 + b2
    if x12-x11 != 0:
       if  x22-x21 == 0:
           k1 = (y12 - y11) / (x12 - x11)
           b1 = (x12 * y11 - x11 * y12) / (x12 - x11)
           return x22, x22 * k1 + b1
       if  x22-x21 != 0:
           k1 = (y12 - y11) / (x12 - x11)
           b1 = (x12 * y11 - x11 * y12) / (x12 - x11)
           k2 = (y22 - y21) / (x22 - x21)
           b2 = (x22 * y21 - x21 * y22) / (x22 - x21)
           return (b1 - b2) / (k2 - k1), (k2 * b1 - k1 * b2) / (k2 - k1)

if __name__=="__main__":
    # 初始点
    points = [(0, 5), (5, 5), (5, 0), (7, 5)]
    x, y = zip(*points)
    plt.plot(x, y)
    # 加宽倍数
    Widening_factor=2
    # 折线一个方向上增加的宽度
    distance=Widening_factor/2
    # 计算折线平移需要的dx和dy
    # 第一条直线平移所需要的dx和dy
    dx_1, dy_1 = displacement((points[0][0], points[0][1], points[1][0],points[1][1]), distance)
    # 第二条直线平移所需要的dx和dy
    dx_2, dy_2 = displacement((points[1][0], points[1][1], points[2][0],points[2][1]), distance)
    # 第三条直线平移所需要的dx和dy
    dx_3, dy_3 = displacement((points[2][0], points[2][1], points[3][0],points[3][1]), distance)
    # 直线平移
    # 第一条直线向x减小或不变,y增大或不变的方向平移后得到直线
    x1_1, y1_1, x1_2, y1_2 = translate_line((points[0][0], points[0][1], points[1][0], points[1][1]), -dx_1, dy_1)
    # 第一条直线向x增大或不变,y减小或不变的方向平移后得到直线
    x2_1, y2_1, x2_2, y2_2 = translate_line((points[0][0], points[0][1], points[1][0], points[1][1]), dx_1, -dy_1)
    # 第二条直线向x减小或不变,y增大或不变的方向平移后得到直线
    x3_1, y3_1, x3_2, y3_2 = translate_line((points[1][0], points[1][1], points[2][0], points[2][1]), -dx_2, dy_2)
    # 第二条直线向x增大或不变,y减小或不变的方向平移后得到直线
    x4_1, y4_1, x4_2, y4_2 = translate_line((points[1][0], points[1][1], points[2][0], points[2][1]), dx_2, -dy_2)
    # 第三条直线向x减小或不变,y增大或不变的方向平移后得到直线
    x5_1, y5_1, x5_2, y5_2 = translate_line((points[2][0], points[2][1], points[3][0], points[3][1]), -dx_3, dy_3)
    # 第三条直线向x增大或不变,y减小或不变的方向平移后得到直线
    x6_1, y6_1, x6_2, y6_2 = translate_line((points[2][0], points[2][1], points[3][0], points[3][1]), dx_3, -dy_3)
    """
    # 打印折线上的点平移后的坐标
    print((x1_1, y1_1), (x1_2, y1_2))
    print((x2_1, y2_1), (x2_2, y2_2))
    print((x3_1, y3_1), (x3_2, y3_2))
    print((x4_1, y4_1), (x4_2, y4_2))
    print((x5_1, y5_1), (x5_2, y5_2))
    print((x6_1, y6_1), (x6_2, y6_2))
    
    # 绘制
    points_1 = [(x1_1, y1_1), (x1_2, y1_2)]
    points_2 = [(x2_1, y2_1), (x2_2, y2_2)]
    points_3 = [(x3_1, y3_1), (x3_2, y3_2)]
    points_4 = [(x4_1, y4_1), (x4_2, y4_2)]
    points_5 = [(x5_1, y5_1), (x5_2, y5_2)]
    points_6 = [(x6_1, y6_1), (x6_2, y6_2)]
    x_1, y_1 = zip(*points_1)
    x_2, y_2 = zip(*points_2)
    x_3, y_3 = zip(*points_3)
    x_4, y_4 = zip(*points_4)
    x_5, y_5 = zip(*points_5)
    x_6, y_6 = zip(*points_6)
    plt.plot(x_1, y_1)
    plt.plot(x_2, y_2)
    plt.plot(x_3, y_3)
    plt.plot(x_4, y_4)
    plt.plot(x_5, y_5)
    plt.plot(x_6, y_6)
    plt.show()
    """

    # 求交
    x1, y1 = x1_1, y1_1
    x2, y2 = get_intersection((x1_1, y1_1, x1_2, y1_2), (x4_1, y4_1, x4_2, y4_2))
    x3, y3 = get_intersection((x4_1, y4_1, x4_2, y4_2), (x5_1, y5_1, x5_2, y5_2))
    x4, y4 = x5_2, y5_2
    x5, y5 = x6_2, y6_2
    x6, y6 = get_intersection((x6_1, y6_1, x6_2, y6_2), (x3_1, y3_1, x3_2, y3_2))
    x7, y7 = get_intersection((x2_1, y2_1, x2_2, y2_2), (x3_1, y3_1, x3_2, y3_2))
    x8, y8 = x2_1, y2_1
    print(x1, y1)
    print(x2, y2)
    print(x3, y3)
    print(x4, y4)
    print(x5, y5)
    print(x6, y6)
    print(x7, y7)
    print(x8, y8)
    points_all = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5), (x6, y6), (x7, y7), (x8, y8), (x1, y1)]
    x, y = zip(*points_all)
    plt.plot(x, y)
    plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值