六角网格中的渗透,概率为0.5

时间:2016-09-14 10:02:02

标签: python hex

我试图绘制一个(10,000 x 10,000)六角形格子,它是随机的半黑色和半白色。我不知道如何将这个格子的六边形随机地填充到黑白([这是一个样本]我真正想要的是这段代码,但我无法做到。])1。这是我的代码(用python语言编写):

from __future__ import division, print_function
import math
import pygame
import random

pygame.init()

window_size = window_width, window_height = 1360, 768
rows, columns = 100, 100
fps = 10000
black = (0, 0, 0)
white = (255, 255, 255)

std_color = white
background_color =white
edge_color = black
color = black

class Hexagon(object):
     """
         row: int - the row of the hexagon in the grid
         col: int - the column of the hexagon in the grid
    """
    def __init__(self, row, col, rows, cols, width=8):
        """
        Constructs a new hexagon.
        Arguments:
            row: int - the row of the hexagon in the grid
            col: int - the column of the hexagon in the grid
        """
        self.row, self.col = row, col

        self.width = width

        alpha = 2*math.pi/3
        self.a = width / (2*math.cos(alpha/2) + 1)
        self.h = math.cos(alpha/2)*self.a
        self.b = math.sin(alpha/2)*self.a*2

        if row % 2 == 0:
            self.x = self.a + self.h
        else:
            self.x = 0.0
            self.x += self.col*(self.a + width)
            self.y = (self.row + 1)*self.b/2
            self.index = (self.row, self.col)

        self.points = [
                (self.x, self.y),
                (self.x + self.h, self.y - self.b/2),
                (self.x + self.h + self.a, self.y - self.b/2),
                (self.x + width, self.y),
                (self.x + self.h + self.a, self.y + self.b/2),
                (self.x + self.h, self.y + self.b/2)
        ]

        rel_indices = [
                (1,0),
                (2,0),
                (-1,0),
                (-2,0),
        ] 
        if self.row % 2 == 0:
            rel_indices += [(1, 1), (-1, 1)]
        else:
            rel_indices += [(-1, -1), (1, -1)]
        self.neighbor_indices = [((self.row + drow) % rows, (self.col + dcol) % cols)
                                 for drow, dcol in rel_indices]


    def draw(self, window, color):       
        pygame.draw.lines(window, edge_color, True, self.points)

grid=[Hexagon(row, col, rows, columns) for row in range(rows) for col in                                range(columns)]

window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Hexgrid")
clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    window.fill(background_color)

    for hexagon in grid:
        hexagon.draw(window, std_color)

    pygame.display.flip()
    clock.tick(fps)

我该怎么做?

0 个答案:

没有答案