Advertisement
Geometrian

L-System

Mar 17th, 2020
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. import copy
  2. import os, sys
  3. import traceback
  4.  
  5. from math import *
  6.  
  7. with open(os.devnull, "w") as f:
  8.     oldstdout=sys.stdout; sys.stdout=f; import pygame; sys.stdout=oldstdout
  9. from pygame.locals import *
  10.  
  11. from OpenGL.GL import *
  12. from OpenGL.GLU import *
  13.  
  14.  
  15. if sys.platform in ["win32","win64"]: os.environ["SDL_VIDEO_CENTERED"]="1"
  16.  
  17. pygame.display.init()
  18. pygame.font.init()
  19.  
  20.  
  21. screen_size = [1024,1024]
  22.  
  23. icon = pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
  24. pygame.display.set_caption("[Program] - [Author] - [Version] - [Date]")
  25.  
  26. multisample = 16
  27. if multisample:
  28.     pygame.display.gl_set_attribute(GL_MULTISAMPLEBUFFERS,1)
  29.     pygame.display.gl_set_attribute(GL_MULTISAMPLESAMPLES,multisample)
  30.    
  31. pygame.display.set_mode(screen_size,OPENGL|DOUBLEBUF)
  32.  
  33.  
  34. glEnable(GL_BLEND)
  35. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
  36.  
  37. glEnable(GL_TEXTURE_2D)
  38. glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE)
  39. glTexEnvi(GL_POINT_SPRITE,GL_COORD_REPLACE,GL_TRUE)
  40.  
  41. glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)
  42. glEnable(GL_DEPTH_TEST)
  43.  
  44.  
  45. def lerp(x,y,t):
  46.     return x*(1.0-t) + y*t
  47.  
  48.  
  49. data =  [ (1/2-sqrt(2)/4,0), ((0.5,0),(3/4,1/4)), (0.5,0), ((0.5,1/8),(1/2,1/4)), (3/8,1/4),
  50.           (1/4,1/8), (3/8,0), ((3/8,-1/8),(3/8,-1/4)), (5/8,-1/4), (5/8,-1/8), (1/2,-1/8),
  51.           ((5/8,-1/8),(3/4,-1/8)), (1,0) ]
  52. ##data = [ (1/4,0), ((1/2,0),(1/2,1/4)), (1/2,-1/4), ((1/2,0),(3/4,0)), (1,0) ]
  53. ##data = [ (1/4,0), ((1/2,0),(1/2,1/4)), (3/4,0), (1/2,0), ((3/4,0),(1,-0.001)) ]
  54. ##data = [ (1/2-sqrt(2)/4,0), ((0.5,0),(3/4,1/4)), (1/2,1/8), (1/2,-1/4), (1,0) ]
  55. class Line(object):
  56.     def __init__(self,p0,p1):
  57.         self.p0 = p0
  58.         self.p1 = p1
  59.         vec = ( self.p1[0]-self.p0[0], self.p1[1]-self.p0[1] )
  60.         self.angle = degrees(atan2(vec[1],vec[0]))
  61.         self.length = hypot(vec[1],vec[0])
  62.         self.children = None
  63.     def apply_rules(self,axiom):
  64.         if self.children == None:
  65.             self.children = copy.deepcopy(axiom)
  66.         else:
  67.             for child in self.children:
  68.                 child.apply_rules(axiom)
  69.     def push(self):
  70.         glPushMatrix()
  71.         glTranslatef(self.p0[0],self.p0[1],0.0)
  72.         glRotatef(self.angle,0,0,1)
  73.         glScalef(*((self.length,)*3))
  74.     def pop(self):
  75.         glPopMatrix()
  76.     def draw(self):
  77.         if self.children == None:
  78.             glColor3f(1,1,0)
  79.             glBegin(GL_LINES)
  80.             glVertex2f(*self.p0)
  81.             glVertex2f(*self.p1)
  82.             glEnd()
  83.         else:
  84.             self.push()
  85.             for child in self.children: child.draw()
  86.             self.pop()
  87. class Arc(object):
  88.     def __init__(self,p0,center,p1):
  89.         self.center = center
  90.         vec0 = (p0[0]-center[0],p0[1]-center[1])
  91.         vec1 = (p1[0]-center[0],p1[1]-center[1])
  92.         self.radius = hypot(vec0[0],vec0[1])
  93.         angle0 = degrees(atan2(vec0[1],vec0[0]))
  94.         angle1 = degrees(atan2(vec1[1],vec1[0]))
  95.         while angle0<0.0: angle0+=360.0
  96.         while angle1<0.0: angle1+=360.0
  97.         if   angle1>angle0 and angle1-angle0>180: angle1-=360.0
  98.         elif angle1<angle0 and angle0-angle1>180: angle1+=360.0
  99.         self.angle0 = radians(angle0)
  100.         self.angle1 = radians(angle1)
  101.     def apply_rules(self,axiom):
  102.         pass
  103.     def draw(self):
  104.         glColor3f(1,0,0)
  105.         glBegin(GL_LINE_STRIP)
  106.         for i in range(100):
  107.             angle = lerp(self.angle0,self.angle1,i/99.0)
  108.             glVertex2f(self.center[0]+self.radius*cos(angle),self.center[1]+self.radius*sin(angle))
  109.         glEnd()
  110. axiom = []
  111. pos = (0,0)
  112. for elem in data:
  113.     if type(elem[0]) == tuple:
  114.         #arc
  115.         axiom.append(Arc( pos, elem[0], elem[1] ))
  116.         pos = elem[1]
  117.     else:
  118.         #line
  119.         axiom.append(Line( pos, elem ))
  120.         pos = elem
  121.  
  122. def apply_rule(system):
  123.     for elem in system:
  124.         elem.apply_rules(axiom)
  125.  
  126. systems = [copy.deepcopy(axiom)]
  127. for i in range(3):
  128.     system = copy.deepcopy(systems[-1])
  129.     apply_rule(system)
  130.     systems.append(system)
  131.  
  132. dllists = [ glGenLists(1) for i in range(len(systems)) ]
  133. for i in range(len(systems)):
  134.     glNewList(dllists[i],GL_COMPILE)
  135.     for elem in systems[i]:
  136.         elem.draw()
  137.     glEndList()
  138.  
  139.  
  140. idisplay = 3
  141. def get_input():
  142.     global idisplay
  143.     for event in pygame.event.get():
  144.         if   event.type == QUIT: return False
  145.         elif event.type == KEYDOWN:
  146.             if   event.key == K_ESCAPE: return False
  147.             elif event.key==K_0: idisplay=0
  148.             elif event.key==K_1: idisplay=1
  149.             elif event.key==K_2: idisplay=2
  150.             elif event.key==K_3: idisplay=3
  151.             elif event.key==K_4: idisplay=4
  152.             elif event.key==K_5: idisplay=5
  153.             elif event.key==K_6: idisplay=6
  154.             elif event.key==K_7: idisplay=7
  155.             elif event.key==K_8: idisplay=8
  156.             elif event.key==K_9: idisplay=9
  157.     return True
  158.  
  159. def draw():
  160.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  161.    
  162.     glViewport(0,0,screen_size[0],screen_size[1])
  163.    
  164.     glMatrixMode(GL_PROJECTION)
  165.     glLoadIdentity()
  166.     glOrtho( -0.1,1.1, -0.6,0.6, -1.0,1.0 )
  167.    
  168.     glMatrixMode(GL_MODELVIEW)
  169.     glLoadIdentity()
  170.  
  171.     glCallList(dllists[idisplay])
  172.    
  173.     pygame.display.flip()
  174.  
  175. def main():
  176.     clock = pygame.time.Clock()
  177.     while True:
  178.         if not get_input(): break
  179.         draw()
  180.         clock.tick(60)
  181.     pygame.quit()
  182.  
  183. if __name__ == "__main__":
  184.     try:
  185.         main()
  186.     except:
  187.         traceback.print_exc()
  188.         pygame.quit()
  189.         input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement