如何使用 Processing 绘制平滑的四边形网格?

How to draw smooth quad mesh with Processing?

我正在尝试在 Processing 中使用样条线绘制一个由几个正方形组成的网格,到目前为止我已经尝试过了

mesh = (
    [100, 100],[150, 100],[200, 100],[250, 100],[300, 100],[350, 100],[400, 100],[450, 100],[500, 100],
    [100, 150],[150, 150],[200, 150],[250, 150],[300, 150],[350, 150],[400, 150],[450, 150],[500, 150],
    [100, 200],[150, 200],[200, 200],[250, 200],[300, 200],[350, 200],[400, 200],[450, 200],[500, 200],
    [100, 250],[150, 250],[200, 250],[250, 250],[300, 250],[350, 250],[400, 250],[450, 250],[500, 250],
    [100, 300],[150, 300],[200, 300],[250, 300],[300, 300],[350, 300],[400, 300],[450, 300],[500, 300],
    [100, 350],[150, 350],[200, 350],[250, 350],[300, 350],[350, 350],[400, 350],[450, 350],[500, 350],
)

def draw():
    clear()
    background(255)
    stroke(0)
    strokeWeight(1.2)
    beginShape()
    for p in mesh:
        curveVertex(*p)
    endShape()
    stroke(*POINT_COLOR)
    strokeWeight(POINT_RADIUS)
    for p in mesh:
        point(*p)

其中mesh是所有顶点的矩阵。我想绘制所有正方形的所有 4 条边,如何使用样条曲线绘制?稍后我将允许用户拖动顶点来改变网格形状,我希望该形状是平滑的。最终结果将如下所示,但在 2D 平面上:

这是我的建议(免责声明:我没有在 python 中使用处理,所以我无法测试 运行 这段代码,可能会有错误):

mesh = (
    [[100, 100],[150, 100],[200, 100],[250, 100],[300, 100],[350, 100],[400, 100],[450, 100],[500, 100]],
    [[100, 150],[150, 150],[200, 150],[250, 150],[300, 150],[350, 150],[400, 150],[450, 150],[500, 150]],
    [[100, 200],[150, 200],[200, 200],[250, 200],[300, 200],[350, 200],[400, 200],[450, 200],[500, 200]],
    [[100, 250],[150, 250],[200, 250],[250, 250],[300, 250],[350, 250],[400, 250],[450, 250],[500, 250]],
    [[100, 300],[150, 300],[200, 300],[250, 300],[300, 300],[350, 300],[400, 300],[450, 300],[500, 300]],
    [[100, 350],[150, 350],[200, 350],[250, 350],[300, 350],[350, 350],[400, 350],[450, 350],[500, 350]]
)

def draw():
    background(255)
    stroke(0)
    for i in range(len(mesh)):
        beginShape()
        curveVertex(mesh[i][0][0], mesh[i][0][0])
        for j in range(len(mesh[i])):
            curveVertex(mesh[i][j][0], mesh[i][j][0])
        curveVertex(mesh[i][len(mesh[i]) - 1][0], mesh[i][len(mesh[i]) - 1][0])
        endShape()
    for i in range(len(mesh[0])):
        beginShape()
        curveVertex(mesh[0][i][0], mesh[0][i][1])
        for j in range(len(mesh)):
            curveVertex(mesh[j][i][0], mesh[j][i][0])
        curveVertex(mesh[len(mesh) - 1][i][0], mesh[len(mesh) - 1][i][1])
        endShape()

这会在每一行画一条线,然后在每一列画一条线(至少,这是我的意图)。请注意,我让每一行都有自己的列表,而不是将所有坐标对放在一个元组中。这使我能够以更有条理的方式遍历这些点。