与顶点建立连接

2024-10-04 05:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个生成球体顶点的函数,但我不知道如何生成球体的边/连接。有人知道我怎样才能做到这一点吗

以下是生成球体顶点的方法:

def uv_sphere_vertices(position, radius, stackcount : int, sectorcount : int):
    verts = []
    sectorstep = 2 * math.pi / sectorcount
    stackstep = math.pi / stackcount
    for num in range(stackcount):
        stackangle = math.pi / 2 - num * stackstep
        for num2 in range(sectorcount):
            sectorangle = num2 * sectorstep
            x = radius * math.cos(stackangle) * math.cos(sectorangle)
            y = radius * math.cos(stackangle) * math.sin(sectorangle)
            z = radius * math.sin(stackangle)
            verts.append([x,y,z])

    return verts

这就是我目前拥有的: Points

我需要连接作为顶点列表中顶点的索引。这是连接所需的示例:

enter image description here

我试过他们在http://www.songho.ca/opengl/gl_sphere.html做这件事的方式,但没有真正起作用。我将那里的内容转换为:

    for num in range(stackcount):
        k1 = num * (sectorcount + 1)
        k2 = k1 + sectorcount + 1
        for num2 in range(sectorcount):
            if num != 0:
                edges.append([k1,k2,k1+1])
            if num != stackcount-1:
                edges.append([k1+1,k2,k2+1])

            k1 += 1
            k2 += 1

得到了这个非常滞后的结果,完全不是期望的结果: enter image description here


Tags: inforpirangek2k1mathnum
1条回答
网友
1楼 · 发布于 2024-10-04 05:23:07

我想出了一个解决办法。结果是,我需要在范围值中添加一个。这是我的最终代码:

def uv_sphere_vertices(position, radius, stackcount : int, sectorcount : int):
    verts = []
    edges = []
    sectorstep = 2 * math.pi / sectorcount
    stackstep = math.pi / stackcount
    for num in range(stackcount+1):
        stackangle = math.pi / 2 - num * stackstep
        for num2 in range(sectorcount+1):
            sectorangle = num2 * sectorstep
            x = radius * math.cos(stackangle) * math.cos(sectorangle)
            y = radius * math.cos(stackangle) * math.sin(sectorangle)
            z = radius * math.sin(stackangle)
            verts.append([x,y,z])

    for num in range(stackcount):
        cstack = num * (sectorcount + 1)
        nstack = cstack + sectorcount + 1
        for num2 in range(sectorcount):
            if num != 0:
                edges.append([cstack, nstack, cstack + 1])
            if num != stackcount - 1:
                edges.append([cstack + 1, nstack, nstack + 1])

            cstack += 1
            nstack += 1

    return verts,edges

相关问题 更多 >