亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我對 Bridson 算法 Poisson-Disk Sampling 的實現似乎陷入了無限循環

我對 Bridson 算法 Poisson-Disk Sampling 的實現似乎陷入了無限循環

料青山看我應如是 2022-06-14 09:57:57
Sebastion Lague 的一段視頻很好地解釋了 Bridson 的算法。過于簡單化,創建具有半徑/sqrt(2) 邊的單元格網格。放置初始點并將列表作為生成點。將點放入網格中的單元格中。對于任何生成點,生成半徑和 2*radius 之間的點。查看距離新點單元格 2 個單位的單元格。如果包含其他點,則比較距離。如果任何點比半徑更接近新點,則新點無效。如果新點有效,則將新點列為生成點并放入網格中的單元格中。如果 spawnpoint 生成了太多無效點,則 spawnpoint 將被刪除并變成點。重復直到不再存在生成點。返回積分。我在 Python 3.7.2 和 pygame 1.7~ 中基本上寫了同樣的東西,但正如標題中所說,我陷入了遞歸煉獄。我為這個算法使用了一個 Point() 類,考慮到 pygame.Vector2() 存在,這似乎是多余的,但我需要一些元素用于需要這個類才能工作的單獨算法(具有無限頂點的 Delaunay)。為了簡單起見,我將刪除所有特定于 Delaunay 的元素,并展示該算法所需的此類的基本框架:class Point:    def __init__(self, x, y):        self.x = x        self.y = y    def DistanceToSquared(self,other):        return (self.x-other.x)**2 + (self.y-other.y)**2與 Bridson 算法相關的代碼是:def PoissonDiskSampling(width, height, radius, startPos = None, spawnAttempts = 10):    if startPos == None:        startPos = [width//2,height//2]    cellSize = radius / math.sqrt(2)    cellNumberX = int(width // cellSize + 1)  # Initialise a cells grid for optimisation    cellNumberY = int(height // cellSize + 1)    cellGrid = [[None for x in range(cellNumberX)] for y in range(cellNumberY)]    startingPoint = Point(startPos[0],startPos[1]) # Add an iniial point for spawning purposes    cellGrid[startingPoint.x//radius][startingPoint.y//radius] = startingPoint    points = [startingPoint] # Initialise 2 lists tracking all points and active points    spawnpoints = [startingPoint]    while len(spawnpoints) > 0:        spawnIndex = random.randint(0,len(spawnpoints)-1)        spawnpoint = spawnpoints[spawnIndex]        spawned = False        for i in range(spawnAttempts):            r = random.uniform(radius,2*radius)            radian = random.uniform(0,2*math.pi)            newPoint = Point(spawnpoint.x + r*math.cos(radian),                            spawnpoint.y + r*math.sin(radian))            if 0 <= newPoint.x <= width and 0 <= newPoint.y <= height:                isValid = True            else:                continue
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

您的代碼中可能缺少最重要的步驟:


如果新點有效,則將新點列為 spawnpoint 并放入 grid 中的單元格中。

我建議將這一點添加到cellGrid是否有效:


if isValid:


    cellGrid[newPointIndex[0]][newPointIndex[1]] = newPoint


    points.append(newPoint)

    spawnpoints.append(newPoint)

    spawned = True

    break

newPointIndex此外,在可以添加點之前,您必須驗證具有索引的單元格是否尚未被占用:


newPointIndex = [int(newPoint.x/cellSize), int(newPoint.y/cellSize)]

if cellGrid[newPointIndex[0]][newPointIndex[1]] != None:

    continue


neighbours = FindNeighbours(cellNumberX,cellNumberY,newPointIndex,cellGrid)

最后,函數存在問題FindNeighbours。range(start, stop)為 x in 創建一個范圍start <= x < stop。

所以停止必須是index[0]+3而不是index[0]+2。


此外,控制 2 個嵌套for循環的范圍從x-2toy+2而不是 from x-2tox+2分別從y-2to運行y+2:


for cellX in range(max(0,(index[0]-2)), min(cellNumberX,(index[1]+2))):

   for cellY in range(max(0,(index[0]-2)), min(cellNumberY,(index[1]+2)))

固定功能必須是:


def FindNeighbours(cellNumberX, cellNumberY, index, cellGrid):

    neighbours = []

    for cellX in range(max(0, index[0]-2), min(cellNumberX, index[0]+3)):

        for cellY in range(max(0, index[1]-2), min(cellNumberY, index[1]+3)):

            if cellGrid[cellX][cellY] != None:

                neighbours.append(cellGrid[cellX][cellY])

    return neighbours

查看結果,尺寸為 300 x 300,半徑為 15:

http://img1.sycdn.imooc.com//62a7eb560001a58803290328.jpg

spawnpoints如果始終使用第一個點而不是隨機點,則可以獲得更好的結果:


# spawnIndex = random.randint(0,len(spawnpoints)-1)

spawnIndex = 0 # 0 rather than random

spawnpoint = spawnpoints[spawnIndex] 

http://img1.sycdn.imooc.com//62a7eb6a00010c2e03290328.jpg

查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號