π Generator

Using Python and Pygame I created a program which approximates π. On the square screen points will randomly appear. Based on the distance to the center the points get a different color. Divided into two groups (the red points and the gray points) the program can roughly estimate π.

How it calculates

The red points represent the circle area.
A' = \pi \cdot r^2
The gray points represent the square area minus the circle area. Thus the gray points and the red points the square area.
A'' = \big(2 \cdot r\big)^2 = 4 \cdot r^2
We get \frac{\pi}{4}, when we divide one by another.
\frac{A'}{A''} = \frac{\pi \cdot r^2}{4 \cdot r^2} = \frac{\pi}{4}
Lastly, we need to multiply by four to get \pi.
\frac{A'}{A''} \cdot 4 = \pi

Main Display Terminal Output


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 29th of March, 2015

# gets the distance between two points
def getDistance(p1, p2):
	return int(math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2))

# tick function
def tick():
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()

# render function
def render():
	# for loop to create more points at a time
	for _ in range(0, 100):
		# creates a random point
		p = [
			random.randint(0, main.WIDTH),
			random.randint(0, main.HEIGHT)
		]
		
		# calculates distance between 'p' and the middle ('main.MIDDLE')
		dist = getDistance(main.MIDDLE, p)
		
		# if distance is equal to radius, it gets a bright red color
		if dist == main.RADIUS:
			pygame.draw.circle(main.SURF, (100, 0, 0), p, 2)
			main.REDS += 1
		
		# if distance is smaller than radius, the point lays in the circle
		# it gets a dark red color
		elif dist = 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

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

# init
import pygame, sys, time, math, os, random

main = dummy()

main.TICKS = 0

main.RADIUS = 300
main.WIDTH = main.RADIUS * 2
main.HEIGHT = main.RADIUS * 2
main.MIDDLE = [main.WIDTH / 2, main.HEIGHT / 2]
main.SURF = pygame.Surface([main.WIDTH, main.HEIGHT])

main.SCALE = 1
main.SCALEDSIZE = [main.WIDTH * main.SCALE, main.HEIGHT * main.SCALE]
main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Pi Generator")

main.GREYS = 0
main.REDS = 0

# start program
run()
Advertisements

One thought on “π Generator

  1. Pingback: Pi Day MMXVII – 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