#! /usr/bin/env python """ Sierpinski.py is a program which plot the Sierpinski triangle, which is a fractal One can generate the fractal pattern by following the prescription below: [1] Use the three (x,y) vertex points: (-1,0), (0, %3 ), and (1,0) in a plane to form a triangle. [2] Starting from the origin (0,0) plot a point. [3] Randomly select any one of the 3 vertex points. [4] Move half the distance from your current position to the selected vertex. [5] Plot the new current position. [6] Repeat from step 3. This program demonstrates the fractal nature show the repeating pattern at different scales Paul Eugenio Florida State University PHZ4151C 29 Jan 2019 """ from __future__ import division, print_function from math import sqrt from matplotlib.pyplot import scatter, show, figure, xlim, ylim import numpy as np # The plotting point [x,y] starting at 0,0 plotPoint = [0,0] # Create a list of 3 [x,y] points, one for of the triangle's vertices triVertex = [[-1,0],[0,sqrt(3)],[1,0]] # Number of plotting points (many ponts are needed for fractal zooming) N = 200000 # Lists for storing plotting data x = [] y = [] for i in range(N): # Find a random point from {0,1,2} r = np.random.randint(3) # Creating a plotting point halfway between the current # point and a randomly selected vertex plotPoint = [(plotPoint[0] + triVertex[r][0])/2, (plotPoint[1] + triVertex[r][1])/2] x += [plotPoint[0]] y += [plotPoint[1]] # # Create two figures for plotting: [1] the whole fractal, # [2] a zoomed in region exhibiting the fractal nature # fig1 = figure(1) scatter(x,y, s=0.1) fig1.show() fig2 = figure(2) scatter(x,y, s=0.1) # Zoom in to a small corner of the triangle # to observe the repeating fractal nature xlim(0.94, 1) ylim(0, 0.04) fig2.show() # Stop program from automatically ending raw_input("Enter [return] to exit")