为了账号安全,请及时绑定邮箱和手机立即绑定

使用 PyGame 显示 Sci Py voronoi 边缘会产生奇怪的“星星”效果

使用 PyGame 显示 Sci Py voronoi 边缘会产生奇怪的“星星”效果

江户川乱折腾 2021-12-09 10:44:29
我正在尝试使用 SciPy 和 PyGame在我随机生成的世界地图上创建和显示Voronoi 图。我遇到的问题是,总有一个点有奇怪的线条,它们会忽略其他任何东西,并像星星或其他东西一样散布在地图上。从左上角和左下角可以看出,它们不会无限远。我怎样才能摆脱它?显示内容:我的代码:import numpyimport randomimport pygamefrom scipy.spatial import Voronoidef __generate_voronoi():    """    Randomly chooses various points within the x and y dimensions of the map.    Then, uses SciPy to generate a voronoi diagram with them, and returns it.    :return: SciPy voronoi diagram    """    point_arr = numpy.zeros([900, 2], numpy.uint16)    for i in range(900):        point_arr[i][0] = numpy.uint16(random.randint(0, 1600))        point_arr[i][1] = numpy.uint16(random.randint(0, 900))    return Voronoi(point_arr)def draw_voronoi(pygame_surface):    # generate voronoi diagram    vor = __generate_voronoi()    # draw all the edges    for indx_pair in vor.ridge_vertices:        start_pos = vor.vertices[indx_pair[0]]        end_pos = vor.vertices[indx_pair[1]]        pygame.draw.line(pygame_surface, (0, 0, 0), start_pos, end_pos)
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

感谢这里的耐心评论者,我了解到vor.vertices对于无限大的点的第一个索引将返回 -1。这会产生一个问题,因为 python 将 -1 视为列表或数组最后一个元素的索引。


我的问题的解决方案不是从vor.vertices.


我通过用draw_voronoi()以下代码替换函数来实现:


def draw_voronoi(pygame_surface):


    # generate voronoi diagram

    vor = __generate_voronoi()


    # draw all the edges

    for indx_pair in vor.ridge_vertices:


        if -1 not in indx_pair:


            start_pos = vor.vertices[indx_pair[0]]

            end_pos = vor.vertices[indx_pair[1]]


            pygame.draw.line(pygame_surface, (0, 0, 0), start_pos, end_pos)

这产生了这个图像:

//img1.sycdn.imooc.com//61b16dd70001e0d706550382.jpg

查看完整回答
反对 回复 2021-12-09
  • 1 回答
  • 0 关注
  • 188 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信