# This program populates a 2 dimensional matrix randomly with 1s and 0s, and uses it to # paint a grid of cells in a 'wxPython' window. # NOTE: For your assignment, the simplest change to this program you can do is to write your # logic to populate the 'cell_states' matrix and then use it to paint the Cellular Automaton (CA) space-time grid. # Each row of the matrix would correspond to the state of the CA at the corresponding time step. # wxPython NOTE: Every program using wxPython must necessarily create an 'App' and a 'Frame' object. # In this program, we further create a 'Panel' object. wxPython is an event-driven platform. In this program, # we bind the 'EVT_PAINT' event with the 'OnPaint' function where we paint our CA space-time grid. # Every time a window is created it is redrawn thereby generating the EVT_PAINT event and consequently # the OnPaint method which reads the 'cell_states' matrix. So, before creating the window, populate the # cell_states matrix. import wx import numpy as np # Grid specifications GRID_B = 1000 GRID_H = 700 # Cell specifications NUM_HORIZ_CELLS = 100 NUM_VERT_CELLS = 100 CELL_WIDTH = float(GRID_B) / NUM_HORIZ_CELLS CELL_HEIGHT = float(GRID_H) / NUM_VERT_CELLS # NOTE: Write logic to populate 'cell_states' # Create a 2D 'cell_states' array with randomly chosen 0's and 1's cell_states = np.random.randint(2, size=(NUM_HORIZ_CELLS, NUM_VERT_CELLS)) app = wx.App() frame = wx.Frame(None, title="CA space-time diagram", size = (GRID_B, GRID_H)) panel = wx.Panel(frame) panel.SetBackgroundColour("black") def OnPaint(event): panel.dc = wx.PaintDC(panel) # painting needs to be directed to a "device context" or DC (panel in this case) for i in range(0, NUM_HORIZ_CELLS): for j in range(0, NUM_VERT_CELLS): if cell_states[i, j] == 1: color = "green" else: color = "red" panel.dc.SetBrush(wx.Brush(color, wx.SOLID)) panel.dc.DrawRectangle(i * CELL_WIDTH, j * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT) # rectangle parameters specify top left x and y coordinates and its width and height, in that order. panel.Bind(wx.EVT_PAINT, OnPaint) frame.Center() frame.Show(True) # A mainloop is an endless cycle that catches up all events coming up to your application. # It is an integral part of any windows GUI application. app.MainLoop()