Graph Sim

Thinking of graphs – especially bar graphs – as a pretty piece of art I wanted to create something related. But due to me not having any data I could graph, I decided to just use randomly generated data. It has no real purpose rather than look good.
The First Example The Second Example The Third Example

Some math

  • All of these calculations are based on random.randint() being perfectly random. Due to it being pseudo-random this will just be an approximation. To get further information, click on the equation to view them in Wolfram Alpha.
  • The chance of two adjacent bars having the same color
    • \Big(\big(\frac{1}{256}\big)^3\Big)^{2-1} = 5.96 \cdot 10^{-8} = \text{basically }0
  • The chance of two adjacent bars having the same color and size
  • (\text{max. size} = \text{window height} = 720)
    • \Big(\big(\frac{1}{256}\big)^3 \cdot \frac{1}{720}\Big)^{2-1} = 8.28 \cdot 10^{-11} = \text{basically }0
  • The chance of the screen not showing anything
  • (\text{which means every bar has the background color})
  • (\text{max. quantity of bars on screen} = \frac{\text{window height}}{\text{bar width}} = \frac{720}{4} = 180)
    • \Big(\big(\frac{1}{256}\big)^3 \Big)^{180} = 3.55 \cdot 10^{-1301} = \text{basically }0.0
  • Although these probabilities are very unlikely, they – theoretically – could occur.

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 13th of May, 2015

# gets the scaled mouse position
def getMousePos():
	p = pygame.mouse.get_pos()
	return [
				p[0] / main.SCALE, p[1] / main.SCALE
			]

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

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
	newColor = math.fabs(_color)
	n = _max - _min
	if newColor > n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

# 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 [
				int(_pos[0] + _radius * math.cos(math.radians(_angle))),
				int(_pos[1] + _radius * math.sin(math.radians(_angle)))
			]

# swaps a boolean
def boolSwap(_bool):
	if _bool:
		return False
	else:
		return True


# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
				sys.exit()
		
		# keyup
		if event.type == pygame.KEYUP:
			# handle 'main.KEYSDOWN'
			if event.key in main.KEYSDOWN:
				main.KEYSDOWN.remove(event.key)
		
		# keydown
		if event.type == pygame.KEYDOWN:
			# handle 'main.KEYSDOWN'
			if event.key not in main.KEYSDOWN:
				main.KEYSDOWN.append(event.key)
	
	# add a new bar
	if main.TICKS % 1 == 0:
		main.GRAPH.append(
			[random.randint(0, main.HEIGHT), [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]]
		)
	
	# cap off max number of bars (for better performance)
	while len(main.GRAPH) > main.WIDTH / main.GRAPHWIDTH + 1:
		main.GRAPH.pop(0)

# render function
def render():
	# fill
	main.SCREEN.fill(main.BACKGROUNDCOLOR)
	if main.FILL:
		main.SURF.fill(main.COLOR)
	
	# render bars
	for _ in range(0, len(main.GRAPH)):
		p = [
			_*main.GRAPHWIDTH,
			main.GRAPH[_][0],
			
			main.GRAPHWIDTH,
			main.HEIGHT
		]
		pygame.draw.rect(main.SURF, main.GRAPH[_][1], p)
	
	# blit and flip
	main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
	pygame.display.flip()

# 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

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

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

# main dummy
main = dummy()
main.TICKS = 0

main.RES = 1
main.WIDTH = 1080 / main.RES
main.HEIGHT = 720 / main.RES
main.SIZE = [main.WIDTH, main.HEIGHT]
main.CENTER = [main.WIDTH / 2, main.HEIGHT / 2]
main.CENTERFLOAT = [main.WIDTH / 2.0, main.HEIGHT / 2.0]

main.SCALE = 1 * main.RES
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]
main.SCALEDSIZEFLOAT = [main.WIDTH * main.SCALE, main.HEIGHT * main.SCALE]

main.SURF = pygame.Surface(main.SIZE)

main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Graph Sim")

main.BACKGROUNDCOLOR = [0, 0, 0]
main.COLOR = [150, 0, 0]
main.FILL = True

main.KEYSDOWN = []

main.GRAPH = []
main.GRAPHWIDTH = 4

# start program
run()
Advertisements

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