Skip to content

Instantly share code, notes, and snippets.

@VitorRamos
Created December 11, 2018 07:34
Show Gist options
  • Save VitorRamos/603f277947fee669a34336e9512695b4 to your computer and use it in GitHub Desktop.
Save VitorRamos/603f277947fee669a34336e9512695b4 to your computer and use it in GitHub Desktop.
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
from collections import defaultdict
class vec2:
def __init__(self,x,y):
self.x=x
self.y=y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class Toothpick:
horizontal= 0
vertical= 1
sz= 0.125*2
def __init__(self, center, direction):
self.center= center
self.direction= direction
def end_points(self):
if self.direction == Toothpick.horizontal:
return [vec2(self.center.x-Toothpick.sz/2, self.center.y),
vec2(self.center.x+Toothpick.sz/2, self.center.y)]
else:
return [vec2(self.center.x, self.center.y-Toothpick.sz/2),
vec2(self.center.x, self.center.y+Toothpick.sz/2)]
def draw_edge(self):
glPushMatrix()
glPointSize(2)
glColor3f(0,0,1)
glBegin(GL_POINTS)
if self.direction == Toothpick.horizontal:
glVertex2f(self.center.x-Toothpick.sz/2, self.center.y)
glVertex2f(self.center.x+Toothpick.sz/2, self.center.y)
else:
glVertex2f(self.center.x, self.center.y-Toothpick.sz/2)
glVertex2f(self.center.x, self.center.y+Toothpick.sz/2)
glEnd()
glPopMatrix()
def draw(self):
glPushMatrix()
glColor3f(1,0,0)
glBegin(GL_LINES)
if self.direction == Toothpick.horizontal:
glVertex2f(self.center.x-Toothpick.sz/2, self.center.y)
glVertex2f(self.center.x+Toothpick.sz/2, self.center.y)
else:
glVertex2f(self.center.x, self.center.y-Toothpick.sz/2)
glVertex2f(self.center.x, self.center.y+Toothpick.sz/2)
glEnd()
glPopMatrix()
class World:
def __init__(self):
self.all_ends= []
self.tpicks= [Toothpick(vec2(0,0),Toothpick.vertical)]
self.new_tpicks= [Toothpick(vec2(0,0),Toothpick.vertical)]
self.counter= defaultdict(lambda:0)
def draw(self):
for tp in self.new_tpicks:
tp.draw_edge()
for tp in self.tpicks:
tp.draw()
def update(self):
aux= []
for tp in self.new_tpicks:
if self.counter[ (tp.end_points()[0].x, tp.end_points()[0].y) ] <= 1:
aux.append(Toothpick(tp.end_points()[0], not tp.direction))
self.counter[ (aux[-1].end_points()[0].x, aux[-1].end_points()[0].y) ]+=1
self.counter[ (aux[-1].end_points()[1].x, aux[-1].end_points()[1].y) ]+=1
if self.counter[ (tp.end_points()[1].x, tp.end_points()[1].y) ] <= 1:
aux.append(Toothpick(tp.end_points()[1], not tp.direction))
self.counter[ (aux[-1].end_points()[0].x, aux[-1].end_points()[0].y) ]+=1
self.counter[ (aux[-1].end_points()[1].x, aux[-1].end_points()[1].y) ]+=1
self.new_tpicks= aux
self.tpicks+=aux
w= World()
scale= 2
def reshape(w,h):
ar= w/h
glViewport(0,0,w,h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(-scale*ar,scale*ar,-scale,scale)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def display():
glClear(GL_COLOR_BUFFER_BIT)
w.draw()
glFlush()
def keyboard(key,x,y):
global w, scale
if key == b'r':
w= World()
elif key == b'+':
scale+=1
reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT))
elif key == b'-':
scale= scale-1 if scale > 1 else scale
reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT))
else:
w.update()
glutPostRedisplay()
def time(t):
w.update()
glutPostRedisplay()
glutTimerFunc(100, time, t)
glutInit()
glutInitWindowSize(640,480)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutCreateWindow("Test")
glutReshapeFunc(reshape)
glutDisplayFunc(display)
glutTimerFunc(30, time, 0)
glutKeyboardFunc(keyboard)
glClearColor(1,1,1,1)
glutMainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment