ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[python]通过一个矩形切割另外一个矩形

2022-05-25 12:01:10  阅读:209  来源: 互联网

标签:return 切割 sep python list line 矩形 RingEdgeMap rect


单例:

应用:

#!/bin/env python
# -*- coding:utf-8

from typing import *
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from random import randint as ri
import random


def getColor():
    color: int
    color1 = ri(16, 255)
    color2 = ri(16, 255)
    color3 = ri(16, 255)
    color1 = hex(color1)
    color2 = hex(color2)
    color3 = hex(color3)
    ans = "#" + color1[2:] + color2[2:] + color3[2:]
    return ans


def randomcolor():
    colorArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    color = ""
    for i in range(6):
        color += colorArr[random.randint(0, 14)]
    return "#" + color


def cut_rect_by_line(rect: tuple, line: tuple):
    """

    :param rect:
    :param line:
    :return:
    """
    rect_l = []
    rect_r = []
    rect_t = []
    rect_b = []

    if line[0] == 'x':
        if rect[0] < line[1] < rect[2]:
            rect_l = (rect[0], rect[1], line[1], rect[3])
            rect_r = (line[1], rect[1], rect[2], rect[3])
        elif line[1] <= rect[0]:
            rect_r = rect
        elif line[1] >= rect[2]:
            rect_l = rect

    elif line[0] == 'y':
        if rect[1] < line[1] < rect[3]:
            rect_t = (rect[0], line[1], rect[2], rect[3])
            rect_b = (rect[0], rect[1], rect[2], line[1])
        elif line[1] >= rect[3]:
            rect_b = rect
        elif line[1] <= rect[1]:
            rect_t = rect

    ret = {
        't': rect_t,
        'b': rect_b,
        'l': rect_l,
        'r': rect_r
    }
    return ret


def cut_rect_by_rect(rect: tuple, sep_rect) -> Dict:
    line_map = {'t': ('y', sep_rect[3]),
                'b': ('y', sep_rect[1]),
                'l': ('x', sep_rect[0]),
                'r': ('x', sep_rect[2])}
    frontier = [rect]
    ret_list = []
    while frontier:
        rect_local = frontier.pop()
        temp_rect_lst = []
        for k, v in line_map.items():
            ret = cut_rect_by_line(rect_local, v)
            ret = [i for i in ret.values() if i and list(i) != list(rect_local)]
            temp_rect_lst.extend(ret)
        if not temp_rect_lst:
            ret_list.append(rect_local)
        frontier.extend(temp_rect_lst)
        frontier = list(set(frontier))

    return list(set(ret_list))


class RingEdgeMap:
    OL = 'ol'
    OR = 'or'
    OT = 'ot'
    OB = 'ob'
    OLT = 'olt'
    ORT = 'ort'
    OLB = 'olb'
    ORB = 'orb'
    IL = 'il'
    IR = 'ir'
    IT = 'it'
    IB = 'ib'
    ILT = 'ilt'
    IRT = 'irt'
    ILB = 'ilb'
    IRB = 'irb'
    C = 'c'


def get_rect_section(rect, sep_rect):
    rect_center = (rect[2] + rect[0]) / 2, (rect[3] + rect[1]) / 2
    sep_rect_center = (sep_rect[2] + sep_rect[0]) / 2, (sep_rect[3] + sep_rect[1]) / 2
    top = rect_center[1] > sep_rect[3]
    bottom = rect_center[1] < sep_rect[1]
    left = rect_center[0] < sep_rect[0]
    right = rect_center[0] > sep_rect[2]
    if left and top:
        return RingEdgeMap.OLT
    if left and bottom:
        return RingEdgeMap.OLB
    if right and top:
        return RingEdgeMap.ORT
    if right and bottom:
        return RingEdgeMap.ORB
    if left:
        return RingEdgeMap.OL
    if right:
        return RingEdgeMap.OR
    if top:
        return RingEdgeMap.OT
    if bottom:
        return RingEdgeMap.OB

    return RingEdgeMap.C


if __name__ == '__main__':

    fig, ax = plt.subplots()

    ax.plot([10, 10], [10, 10])
    rect = (0, 0, 100, 100)
    sep_rect = (30, 40, 90, 90)
    ax.add_patch(Rectangle((sep_rect[0], sep_rect[1]), sep_rect[2] - sep_rect[0], sep_rect[3] - sep_rect[1], fill=False, linewidth=1, color="red"))
    ax.add_patch(Rectangle((rect[0], rect[1]), rect[2] - rect[0], rect[3] - rect[1], fill=False, linewidth=1, color='blue'))

    rect_list = cut_rect_by_rect(rect, sep_rect=sep_rect)
    rect_list = list(set(rect_list))
    i = 0
    for rect in rect_list:
        i += 1
        c = randomcolor()
        section_name = get_rect_section(rect, sep_rect)
        ax.add_patch(Rectangle((rect[0], rect[1]), rect[2] - rect[0], rect[3] - rect[1], fill=True, linewidth=1, color=c, alpha=0.1))
        plt.text(x=(rect[2] + rect[0]) // 2, y=(rect[3] + rect[1]) // 2, color=c, s=section_name)

    plt.show()

标签:return,切割,sep,python,list,line,矩形,RingEdgeMap,rect
来源: https://www.cnblogs.com/zxingwork/p/16308841.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有