terraingen.py 文件源码

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

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

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

        Args:
            terrain (Terrain): Terrain to update.
            x (int): X coordinate of center of square.
            y (int): Y coordinate of center of square.
            square_len (int): Length of one side of square.

        Returns:
            Terrain: New terrain with updated square center.

        """
        half_len = square_len / 2
        # Impossible to attempt to access neighbours out of terrain bounds
        mean_height = sum([terrain[x - half_len, y - half_len],
                           terrain[x - half_len, y + half_len],
                           terrain[x + half_len, y - half_len],
                           terrain[x + half_len, y + half_len]]) / 4.0
        frequency = terrain.length / square_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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号