Friday, January 11, 2013

Pygame: Sierpinski triangle

    The Sierpinski triangle is a fractal described by Sierpiński in 1915 and appearing in Italian art from the 13th century. It is also called the Sierpiński gasket. The Sierpinski triangle is a  geometric pattern formed by connecting the  midpoints of the sides of a triangle. It is at the is most  interesting one and   simplest one in fractals.


The Sierpinski triangle is given by Pascal's triangle (mod 2), giving the sequence 1; 1, 1; 1, 0, 1; 1, 1, 1, 1; 1, 0, 0, 0, 1; ... . In other words, coloring all odd numbers black and even numbers white in Pascal's triangle produces a Sierpiński triangle


Construction
An algorithm for obtaining arbitrarily close approximations to the Sierpinski triangle is as follows:

Pygame: Koch Snowflake

     A factral, also known as the Koch island, which was first described by Helge von Koch in 1904. It is built by starting with an equilateral triangle, removing the inner third of each side, building another equilateral triangle at the location where the side was removed, and then repeating the process indefinitely.
The Koch snowflake (also known as the Koch star and Koch island) is a mathematical curve and one of the earliest fractal curves to have been described.
Construction
The Koch snow flake can be constructed by starting with an equilateral triangle, then recursively altering each line segment as follows:

1. divide the line segment into three segments of equal length.
2. draw an equilateral triangle that has the middle segment from step 1 as its base and points outward.
3.remove the line segment that is the base of the triangle from step 2.
After one iteration of this process, the resulting shape is the outline of a hexagram.
The Koch snowflake is the limit approached as the above steps are followed over and over again. The Koch curve originally described by Koch is constructed with only one of the three sides of the original triangle. In other words, three Koch curves make a Koch snowflake.


   Start with an equilateral triangle T. Scale T by a factor of 1/3 and place 3 copies along each of the three sides of T as illustrated in the diagram below to form a new image S(1). Next scale T by a factor of 1/9 = (1/3)^2 and place 12=4*3 copies along the sides of T(1) as illustrated to form the image S(2). For the next iteration, take 48=4*12 copies of Tscaled by a factor of 1/27=(1/3)^3 and place them around the sides of S(2) to form the image S(3). Continue this construction. The Koch Snowflake is the limiting image of the construction.

Wednesday, January 9, 2013

Pygame: Game Of Life

Pygame:

    Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. It is built over the Simple DirectMedia Layer (SDL) library. This is based on the assumption that the most expensive functions inside games (mainly the graphics part) can be abstracted from the game logic, making it possible to use a high-level programming language, such as Python, to structure the game.
    Pygame was originally written by Pete Shinners and is released under the open source free software GNU Lesser General Public License.

Game of life:

    The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and obseving how it evolves.

Rules:
    The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.
         I find the best way to understand the Pygame library is to jump straight into an example. In the early days of Pygame, I created The "Conway's game of life". Let's take a look at the code of Conway's game of life. This tutorial will go through the code block by block. Explaining how the code works.
Import modules:
This is the code that imports all the needed modules into your program. It also checks for the availability of some of the optional pygame modules.
import pygame,sys
from pygame.locals import *