Sierpinski Triangle

Having recently heard something about chaos theory, I was interested in the sierpinski triangle. Specifically in the random generation. To proof, if it can really be generated using a dice, I wrote this program.

How it works

  • You start with any point inside the triangle (I chose the center)
  • A triangle has three points, let us call them A, B and C
  • You choose randomly between one of those three points, go half the distance and set a pixel right there
  • Repeating this process over and over again (always starting at your last point), you eventually get the sierpinski triangle

Controls

  • Space toggles if the triangle is generated
  • m toggles if the current position is shown (indicated by a blue circle)

Starting with an empty triangle Slowly but surely... Finished sierpinski triangle


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of July, 2015

# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# gets the position on a circle
# circle center                           : '_pos'
# circle radius                           : '_radius'
# angle from center to point on the circle: '_angle'
def getCirclePos(_pos, _radius, _angle):
	return [
				_pos[0] + _radius * math.cos(math.radians(_angle)),
				_pos[1] + _radius * math.sin(math.radians(_angle))
			]

# returns an integer version of given positon
def intpos(_pos):
	return [int(_pos[0]), int(_pos[1])]

# basic vector functions
def vecConvert(p1, p2):
	return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
	return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
	return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
	return [point[0] + vec[0], point[1] + vec[1]]
def vecAdd(vec1, vec2):
	return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# quits the program
def quit():
	sys.exit()	

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
				quit()
		
		# keydown
		if event.type == pygame.KEYDOWN:
			# toggle running
			if event.key == pygame.K_SPACE:
				if main.RUNNING:
					main.RUNNING = False
				else:
					main.RUNNING = True
			
			# toggle showing the current pos
			if event.key == pygame.K_m:
				if main.SHOWPOS:
					main.SHOWPOS = False
				else:
					main.SHOWPOS = True

# render function
def render():
	# calculate and render
	if main.RUNNING:
		# for loop speeds up the drawing of the triangle
		for _ in range(0, 10):
			# consider which point to go to
			r = random.randint(0, 2)
			if r == 0:
				vec = vecConvert(main.POS, main.A)
			elif r == 1:
				vec = vecConvert(main.POS, main.B)
			elif r == 2:
				vec = vecConvert(main.POS, main.C)
			
			# calculate the next point using vectors
			vecl = vecLen(vec)
			if vecl != 0:
				vec0 = vecMultiply(vec, 1./vecl)
				vecn = vecMultiply(vec0, vecl/2.)
				main.LASTPOS = main.POS[:]
				main.POS = vecGetPoint(vecn, main.POS)
			
			# draw actual pixel
			main.SURF.set_at(intpos(main.POS), main.DOTCOLOR)
	
	# blit surface
	main.SCREEN.blit(main.SURF, [0, 0])
	
	# render current position
	if main.SHOWPOS:
		pygame.draw.circle(main.SCREEN, main.POSCOLOR, intpos(main.POS), 10)
	
	# flip
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	main.WIDTH, main.HEIGHT = 720, 720
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.CENTER = [main.WIDTH / 2., main.HEIGHT / 2.]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Sierpinski Triangle"
	main.TICKS = 0
	
	main.BACKCOLOR = (0, 0, 0)
	main.DOTCOLOR = (255, 255, 255)
	main.POSCOLOR = (50, 50, 200)
	
	n = 300
	angle = 0
	main.A = getCirclePos(main.CENTER, n, 360. / 3 * 0 + angle)
	main.B = getCirclePos(main.CENTER, n, 360. / 3 * 1 + angle)
	main.C = getCirclePos(main.CENTER, n, 360. / 3 * 2 + angle)
	
	main.LASTPOS = main.CENTER[:]
	main.POS = main.CENTER[:]
	
	main.RUNNING = False
	main.SHOWPOS = True
	
	# functions
	pygame.display.set_caption(main.CAPTION)
	main.SURF.fill(main.BACKCOLOR)
	
	pygame.draw.line(main.SURF, main.DOTCOLOR, main.A, main.B)
	pygame.draw.line(main.SURF, main.DOTCOLOR, main.B, main.C)
	pygame.draw.line(main.SURF, main.DOTCOLOR, main.C, main.A)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()
Advertisements

One thought on “Sierpinski Triangle

  1. Pingback: Sierpinski TIrangle | J-Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s