Цвет пути между двумя прямоугольниками, используя python палочка - PullRequest
0 голосов
/ 24 января 2020

Я записал функцию, используя python -wand как:

def rectangle_with_path(length, width, path_width):
    draw = Drawing()
    draw.fill_color = Color("transparent")
    draw.stroke_color = Color("black")
    draw.stroke_width = 1.5
    epsilon = 50
    if length > width:
        l, w = 300, 240
    else:
        l, w = 240, 300
    draw.stroke_color = Color("blue")
    draw.rectangle(left=(500-l)//2, top=(500-w)//2, right=(500+l)//2, bottom=(500+w)//2)
    draw.stroke_color = Color("green")
    draw.rectangle(left=(500-l)//2 - epsilon, top=(500-w)//2 - epsilon, right=(500+l)//2 + epsilon,
                   bottom=(500+w)//2 + epsilon)
    draw.stroke_color = Color("black")
    _double_arrowed_line(end_points=(((500 - l)//2 - epsilon + 10, 250), ((500 - l)//2 - 10, 250)),
                         angle=180, draw=draw)
    _double_arrowed_line(end_points=(((500 + l)//2 + 10, 250), ((500 + l)//2 + epsilon - 10, 250)),
                         angle=180, draw=draw)
    _double_arrowed_line(end_points=((250, (500 - w)//2 - epsilon + 10), (250, (500 - w)//2 - 10)),
                         angle=90, draw=draw)
    _double_arrowed_line(end_points=((250, (500 + w)//2 + 10), (250, (500 + w)//2 + epsilon - 10)),
                         angle=90, draw=draw)
    draw.stroke_width = 1
    # width label
    draw.text(int((500 - l)//2) + 10, 250 + 50, "%s m" % width)
    # length label
    draw.text(250 + 50, (500-w)//2 + 20, "%s m" % length)

    draw.text(int((500 - l)//2 - epsilon//2 - 5), 250 - 20, "%s m" % path_width)
    draw.text(int((500 + l)//2 + epsilon//2 - 5), 250 - 20, "%s m" % path_width)
    draw.text(250 + 10, int((500 - w)//2 - epsilon//2 - 5), "%s m" % path_width)
    draw.text(250 + 10, int((500 + w)//2 + epsilon//2 - 5), "%s m" % path_width)
    with Image(width=500, height=500, background=background) as image:
        draw(image)
        image.format = 'png'
        png_bin = image.make_blob()
        return png_bin


def _double_arrowed_line(l=None, end_points=None, angle=None, draw=None,
                         fig_width=500, fig_height=500, double1=True, double2=True):
    """
    l : length of line on "both sides"

    end_points : tuple of size 2 of coordinates
        has the start and end point coordinates with it

    angle : angle with the horizontal in degrees (math convention)
        we don't follow ImageMagick's convention
    """
    O = (fig_width/2, fig_height/2)
    angle_d = angle
    angle = radian_angle(angle)
    if end_points is None:
        if l is None:
            l = 175
        start = (O[0] + l*cos(-angle), O[1] + l*sin(-angle))
        end = (O[0] + l*cos(pi - angle), O[1] + l*sin(pi - angle))
    else:
        start, end = end_points
    draw.line(start, end)
    if double1:
        draw.polyline([(start[0] + 4*cos(-pi/2 - angle), start[1] + 4*sin(-pi/2 - angle)),
                       (start[0] + 8*cos(-angle), start[1] + 8*sin(-angle)),
                       (start[0] + 4*cos(pi/2 - angle), start[1] + 4*sin(pi/2 - angle))])
    if double2:
        draw.polyline([(end[0] + 4*cos(pi/2 - angle), end[1] + 4*sin(pi/2 - angle)),
                       (end[0] + 8*cos(pi - angle), end[1] + 8*sin(pi - angle)),
                       (end[0] + 4*cos(-pi/2 - angle), end[1] + 4*sin(-pi/2 - angle))])

Используя вышеуказанную функцию как rectangle_with_path(length=200, width=100, path_width=4), я получаю следующее изображение enter image description here. Как мне закрасить путь (например, красным, например, fill_color) между прямоугольниками, нарисованными зеленым и синим цветом?

Редактировать : _double_arrowed_line просто создает двойные стрелки линия, указывающая конечные точки и угол наклона в градусах.

...