Polygons

Using my ‘getCirclePos()’ function I thought of not only creating a circle. Dividing 360° by n, you get x°. Now x° is a n-th of a circle. If I connect n points with each other, always adding x° and starting from 0°, I get a n-gon. ‘Polygons’ does exactly this. The minimum n-gon-count is 3, the maximum 1000.

Usage

  • ‘w’ increases the polygon point count
  • ‘s’ decreases up the polygon point count
  • ‘up’ does what ‘w’ does, but you can hold it
  • ‘down’ does what ‘s’ does, but you can hold it
  • ‘r’ starts the random movement (polygon count changes randomly)

5-gon6-gon255-gon


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 26th of March, 2015
#         edited 31st of March, 2015

# 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)))
			]

# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
				sys.exit()
		
		# get 'main.KEYSDOWN'
		if event.type == pygame.KEYUP:
			if event.key in main.KEYSDOWN:
				main.KEYSDOWN.remove(event.key)
		
		if event.type == pygame.KEYDOWN:
			if event.key not in main.KEYSDOWN:
				main.KEYSDOWN.append(event.key)
			
			# key 'w' and key 's' control n-gon
			if event.key == pygame.K_w:
				main.COUNT += 1
			if event.key == pygame.K_s:
				main.COUNT -= 1
			
			# random movement toggle
			if event.key == pygame.K_r:
				if main.RAND:
					main.RAND = False
				else:
					main.RAND = True
	
	# key 'UP' and key 'DOWN' control n-gon
	# (here you can hold the keys)
	if pygame.K_UP in main.KEYSDOWN:
		main.COUNT += 1
	if pygame.K_DOWN in main.KEYSDOWN:
		main.COUNT -= 1
	
	# random movement
	if main.RAND:
		main.COUNT += getAOne()
	
	# n-gon bounds
	if main.COUNT  1000:
		main.COUNT = 1000

# render function
def render():
	# fill
	main.SCREEN.fill([0, 0, 0])
	main.SURF.fill([50, 50, 100])
	
	# define colors used
	cols = [[255, 255, 255], [0, 0, 0]]
	#cols = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [0, 255, 255], [255, 0, 255], [255, 255, 255], [0, 0, 0]]
	cCol = 0
	
	# draw the lines for the n-gon
	lastPos = None
	firstPos = None
	for _ in range(0, main.COUNT):
		pos = getCirclePos(main.MIDDLE, (main.WIDTH + main.HEIGHT) / 2 / 3, 360. / main.COUNT * _ + (360. / main.COUNT/2)),
		if firstPos == None:
			firstPos = pos
		if lastPos != None:
			pygame.draw.line(main.SURF, cols[cCol], lastPos, pos, 2)
			# draw line to the center
			#pygame.draw.line(main.SURF, cols[cCol], pos, main.MIDDLE)
			cCol += 1
			if cCol > len(cols) - 1:
				cCol = 0
		
		lastPos = pos
	
	pygame.draw.line(main.SURF, cols[cCol], pos, firstPos, 2)
	# draw line to the center
	#pygame.draw.line(main.SURF, cols[cCol], firstPos, main.MIDDLE)
	
	# display the n in n-gon
	text = main.FONT.render(str(main.COUNT) + "-gon", 1, cols[0])
	rect = text.get_rect()
	pos = [
		0,
		main.HEIGHT - rect.bottom
	]
	main.SURF.blit(text, pos)
	
	# scale
	main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
	# flip
	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

# init
import pygame, sys, time, math, os, random
pygame.font.init()

main = dummy()
main.TICKS = 0

main.RES = 1
main.WIDTH = 1080 / main.RES
main.HEIGHT = 720 / main.RES
main.MIDDLE = [main.WIDTH / 2, main.HEIGHT / 2]
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SURF = pygame.Surface(main.SIZE)

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

main.KEYSDOWN = []
main.FONT = pygame.font.Font(None, int(20 / main.RES))
main.COUNT = 2
main.RAND = False


run()
Advertisements

One thought on “Polygons

  1. Pingback: Circle Walk II | 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