1

5 つのエッジで構成されるポリゴンがあります。各エッジには独自の widthがあり、ここでは説明のために 0.2 から 0.5 の間でランダムに生成されています。

コード

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Path

import math as mt
import random

def buildRectPath(edge, width):
    theta = mt.atan((edge[1][1]-edge[0][1]) / (edge[1][0]-edge[0][0]+0.0000001)) # avoid ZeroDivisionError
    x_left_top = edge[0][0] - mt.sin(theta) * width
    y_left_top = edge[0][1] + mt.cos(theta) * width
    x_left_bottom = edge[0][0]
    y_left_bottom = edge[0][1]
    x_right_top = edge[1][0] - mt.sin(theta) * width
    y_right_top = edge[1][1] + mt.cos(theta) * width
    x_right_bottom = edge[1][0]
    y_right_bottom = edge[1][1]
    verts = [(x_left_bottom, y_left_bottom), # left, bottom
             (x_left_top, y_left_top), # left, top
             (x_right_top, y_right_top), # right, top
             (x_right_bottom, y_right_bottom), # right, bottom
             (x_left_bottom, y_left_bottom),] # ignored
    codes = [Path.MOVETO,
             Path.LINETO,
             Path.LINETO,
             Path.LINETO,
             Path.CLOSEPOLY,]
    path = Path(verts, codes)
    return path

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.axis('equal')
poly = [(1, 1), (2, 4), (4, 6), (6, 6), (8, 3), (1, 1)] # construct a random polygon
# expand each edge of the polygon (to different widths)
edges = [[(1, 1), (2, 4)], [(2, 4), (4, 6)], [(4, 6), (6, 6)], [(6, 6), (8, 3)], [(8, 3), (1, 1)]]
widths = [random.uniform(0.2, 0.5) for cnt in xrange(5)] # generate random widths for the 5 edges
for i, edge in enumerate(edges):
    axes.plot([edge[0][0], edge[1][0]], [edge[0][1], edge[1][1]], color='b')
    rectPath = buildRectPath(edge, widths[i])
    rectPatch = patches.PathPatch(rectPath, facecolor='yellow', alpha=0.5)
    axes.add_patch(rectPatch)

出力

ここに画像の説明を入力

ゴール

形状の滑らかな輪郭を取得したい。基本的に、内側のループ用と外側のループ用の 2 つの曲線を期待しています。

ご了承ください

  • 線の幅が違うので、すべての幅を考慮したフィッティングラインのようなものを求めています。
  • 現在、長方形の間にギャップが存在します。輪郭は、ギャップのない連続曲線である必要があります。
4

0 に答える 0