第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

我對 Bridson 算法 Poisson-Disk Sampling 的實現(xiàn)似乎陷入了無限循環(huán)

我對 Bridson 算法 Poisson-Disk Sampling 的實現(xiàn)似乎陷入了無限循環(huán)

Sebastion Lague 的一段視頻很好地解釋了 Bridson 的算法。過于簡單化,創(chuàng)建具有半徑/sqrt(2) 邊的單元格網(wǎng)格。放置初始點(diǎn)并將列表作為生成點(diǎn)。將點(diǎn)放入網(wǎng)格中的單元格中。對于任何生成點(diǎn),生成半徑和 2*radius 之間的點(diǎn)。查看距離新點(diǎn)單元格 2 個單位的單元格。如果包含其他點(diǎn),則比較距離。如果任何點(diǎn)比半徑更接近新點(diǎn),則新點(diǎn)無效。如果新點(diǎn)有效,則將新點(diǎn)列為生成點(diǎn)并放入網(wǎng)格中的單元格中。如果 spawnpoint 生成了太多無效點(diǎn),則 spawnpoint 將被刪除并變成點(diǎn)。重復(fù)直到不再存在生成點(diǎn)。返回積分。我在 Python 3.7.2 和 pygame 1.7~ 中基本上寫了同樣的東西,但正如標(biāo)題中所說,我陷入了遞歸煉獄。我為這個算法使用了一個 Point() 類,考慮到 pygame.Vector2() 存在,這似乎是多余的,但我需要一些元素用于需要這個類才能工作的單獨(dú)算法(具有無限頂點(diǎn)的 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 算法相關(guān)的代碼是: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貢獻(xiàn)1811條經(jīng)驗 獲得超6個贊

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


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

我建議將這一點(diǎn)添加到cellGrid是否有效:


if isValid:


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


    points.append(newPoint)

    spawnpoints.append(newPoint)

    spawned = True

    break

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


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

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

    continue


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

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

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


此外,控制 2 個嵌套for循環(huán)的范圍從x-2toy+2而不是 from x-2tox+2分別從y-2to運(yùn)行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

查看結(jié)果,尺寸為 300 x 300,半徑為 15:

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

spawnpoints如果始終使用第一個點(diǎn)而不是隨機(jī)點(diǎn),則可以獲得更好的結(jié)果:


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

spawnIndex = 0 # 0 rather than random

spawnpoint = spawnpoints[spawnIndex] 

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

查看完整回答
反對 回復(fù) 2022-06-14
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號