You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将重叠矩形分隔的多角度搜索伪代码移植到Python/Blender?

问题描述

我正尝试将某回答中的Mathematica/伪代码移植到Python/Blender(我的场景和原问题几乎完全一致)。该回答包含两个版本的算法,我已经完成第一个版本的移植,但在移植第二个改进版的“多角度搜索”伪代码时遇到了困难。

已完成的第一个版本移植代码

stack = deque()
while any(intersections[r] for r in rectangles):
    rectangles.sort(key=lambda r: intersections[r])
    stack.appendleft(rectangles[0])
    del rectangles[0]

while stack:
    rectangles.insert(0, stack[0])
    stack.popleft()

    rect = rectangles[0]

    g = geometric_centre(points)
    b, d = points[rect]
    m = ((b + d) / 2) - g

    i = 1
    while has_intersections(rect, points):
        x = (-1)**i * i * INCR
        vec = Vector((x, x)) * m
        b += vec
        d += vec
        i += 1

其中points是一个字典,键为矩形,值为两个Blender mathutils.Vector组成的元组,分别代表矩形的右上角和左下角顶点。

移植前后效果对比

调整前矩形存在重叠,调整后重叠问题解决,布局更规整。

待移植的改进版伪代码

原回答中第二个编辑版本的“多角度搜索”伪代码,其核心while循环逻辑如下:

While stack not empty
    find the geometric center G of the chart (each time!)
    find the PREFERRED movement vector M (from G to rectangle center)
    pop rectangle from stack 
    With the rectangle
         While there are intersections (list+rectangle)
              For increasing movement modulus
                 For increasing angle (0, Pi/4)
                    rotate vector M expanding the angle alongside M
                    (* angle, -angle, Pi + angle, Pi-angle*)
                    re-position the rectangle according to M
    Re-insert modified vector into list

我的失败尝试

while stack:
    rectangles.insert(0, stack[0])
    stack.popleft()

    rect = rectangles[0]

    g = geometric_centre(points)
    b, d = points[rect]
    m = ((b + d) / 2) - g
    old_m = m.copy()

    while has_intersections(rect, points):
        for i in range(1, abs(int(m.magnitude))):
            for angle in (15, 30, 36, 45):
                m.rotate(Matrix.Rotation(radians(angle), 2))
                b += old_m - m
                d += old_m - m

请问该如何将这段伪代码正确翻译成Python?


内容的提问来源于stack exchange,提问作者Don Cheadle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:42:46