我怎么能随意放置一些非冲突rects?

我怎么能随意放置一些非冲突rects?,第1张

我怎么能随意放置一些非冲突rects?

我已经改变了我的回答一点,以解决有关是否可以进行修改,而不是随机生成非冲突的后续问题 的正方形
,而不是任意矩形。我在我可以将工作的最简单的方法,这是后处理我原来的答复的矩形输出,把它的内容变成方形子区这样做。我还更新了可选的可视化代码,以显示两种输出。显然,这种过滤可以扩展到做其他事情一样insetting每个矩形或方形略,以防止它们彼此接触。

我的回答避免做很多的答案已经发布做 - 这是随机产生的矩形,同时拒绝任何碰撞与任何已创建的 -
因为它听起来固有的缓慢性和计算浪费。我的方法,而不是集中于只产生那些不重叠摆在首位。

这使得有什么需要做的将其变成可以非常快速地进行一个简单的区域划分的问题相对简单。下面是一个实现的如何可以做。它始于限定其中它分成四个较小的非重叠矩形外边界的矩形。这是通过选择一个半随机的内部点并将其与外部矩形的四个现有角点一起使用以形成四个子区域来实现的。

大部分的动作发生在

quadsect()
函数。内部点的选择是决定什么输出看起来象是至关重要的。你可以约束它,你希望的任何方式,比如只选择一个,将导致至少在一定的最小宽度或高度或不超过一定量较大的子矩形。在我的回答样本代码,它的定义为中心点±
1 / 3的宽度和外矩形的高度的,但基本上任何内部点会工作到一定程度。

由于这种算法生成子矩形非常迅速,这是确定的,花一些计算时间确定内部分割点。

为了显示此方法的结果,有一些非必要的代码的结尾,它使用

PIL
(Python图像库)模块来创建一个图像文件显示在一些测试运行我做产生的矩形。

无论如何,这是代码和输出示例的最新版本:

import randomfrom random import randintrandom.seed()NUM_RECTS = 20REGION = Rect(0, 0, 640, 480)class Point(object):    def __init__(self, x, y):        self.x, self.y = x, y    @staticmethod    def from_point(other):        return Point(other.x, other.y)class Rect(object):    def __init__(self, x1, y1, x2, y2):        minx, maxx = (x1,x2) if x1 < x2 else (x2,x1)        miny, maxy = (y1,y2) if y1 < y2 else (y2,y1)        self.min, self.max = Point(minx, miny), Point(maxx, maxy)    @staticmethod    def from_points(p1, p2):        return Rect(p1.x, p1.y, p2.x, p2.y)    width  = property(lambda self: self.max.x - self.min.x)    height = property(lambda self: self.max.y - self.min.y)plus_or_minus = lambda v: v * [-1, 1][(randint(0, 100) % 2)]  # equal chance +/-1def quadsect(rect, factor):    """ Subdivide given rectangle into four non-overlapping rectangles.        'factor' is an integer representing the proportion of the width or        height the deviatation from the center of the rectangle allowed.    """    # pick a point in the interior of given rectangle    w, h = rect.width, rect.height  # cache properties    center = Point(rect.min.x + (w // 2), rect.min.y + (h // 2))    delta_x = plus_or_minus(randint(0, w // factor))    delta_y = plus_or_minus(randint(0, h // factor))    interior = Point(center.x + delta_x, center.y + delta_y)    # create rectangles from the interior point and the corners of the outer one    return [Rect(interior.x, interior.y, rect.min.x, rect.min.y), Rect(interior.x, interior.y, rect.max.x, rect.min.y), Rect(interior.x, interior.y, rect.max.x, rect.max.y), Rect(interior.x, interior.y, rect.min.x, rect.max.y)]def square_subregion(rect):    """ Return a square rectangle centered within the given rectangle """    w, h = rect.width, rect.height  # cache properties    if w < h:        offset = (h - w) // 2        return Rect(rect.min.x, rect.min.y+offset,         rect.max.x, rect.min.y+offset+w)    else:        offset = (w - h) // 2        return Rect(rect.min.x+offset, rect.min.y,         rect.min.x+offset+h, rect.max.y)# call quadsect() until at least the number of rects wanted has been generatedrects = [REGION]   # seed output listwhile len(rects) <= NUM_RECTS:    rects = [subrect for rect in rects  for subrect in quadsect(rect, 3)]random.shuffle(rects)  # mix them upsample = random.sample(rects, NUM_RECTS)  # select the desired numberprint '%d out of the %d rectangles selected' % (NUM_RECTS, len(rects))################################################## extra credit - create an image file showing resultsfrom PIL import Image, ImageDrawdef gray(v): return tuple(int(v*255) for _ in range(3))BLACK, DARK_GRAY, GRAY = gray(0), gray(.25), gray(.5)LIGHT_GRAY, WHITE = gray(.75), gray(1)RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)CYAN, MAGENTA, YELLOW = (0, 255, 255), (255, 0, 255), (255, 255, 0)BACKGR, SQUARE_COLOR, RECT_COLOR = (245, 245, 87), (255, 73, 73), (37, 182, 249)imgx, imgy = REGION.max.x + 1, REGION.max.y + 1image = Image.new("RGB", (imgx, imgy), BACKGR)  # create color imagedraw = ImageDraw.Draw(image)def draw_rect(rect, fill=None, outline=WHITE):    draw.rectangle([(rect.min.x, rect.min.y), (rect.max.x, rect.max.y)],        fill=fill, outline=outline)# first draw outlines of all the non-overlapping rectanges generatedfor rect in rects:    draw_rect(rect, outline=LIGHT_GRAY)# then draw the random sample of them selectedfor rect in sample:    draw_rect(rect, fill=RECT_COLOR, outline=WHITE)# and lastly convert those into squares and re-draw them in another colorfor rect in sample:    draw_rect(square_subregion(rect), fill=SQUARE_COLOR, outline=WHITE)filename = 'square_quadsections.png'image.save(filename, "PNG")print repr(filename), 'output image saved'

输出采样1

输出采样2



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5650852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存