terraingen.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:RandTerrainPy 作者: jackromo 项目源码 文件源码
def _update_diamond(self, terrain, x, y, diamond_len):
        """Update the midpoint of a diamond.

        Midpoint becomes average of diamond corners plus a random offset determined by noise.

        Args:
            terrain (Terrain): Terrain to update.
            x (int): X coordinate of center of diamond.
            y (int): Y coordinate of center of diamond.
            diamond_len (int): Length of one corner of diamond to other.

        Returns:
            Terrain: New terrain with updated square center.

        """
        half_len = diamond_len / 2
        # If on edge of terrain, only access 3 neighbours to avoid leaving terrain bounds
        neighbours = []
        if x != 0:
            neighbours.append(terrain[x - half_len, y])
        if y != 0:
            neighbours.append(terrain[x, y - half_len])
        if x != terrain.width - 1:
            neighbours.append(terrain[x + half_len, y])
        if y != terrain.length - 1:
            neighbours.append(terrain[x, y + half_len])
        mean_height = sum(neighbours) / float(len(neighbours))
        frequency = terrain.length / diamond_len
        offset = (random.random() - 0.5) * self.amp_from_freq(frequency)
        if not 0 <= mean_height + offset <= 1:
            if mean_height + offset > 1:
                terrain[x, y] = 1
            else:
                terrain[x, y] = 0
        else:
            terrain[x, y] = mean_height + offset
        return terrain
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号