Recent Posts
C++ | 0 sec ago
None | 5 sec ago
None | 14 sec ago
None | 17 sec ago
None | 21 sec ago
None | 22 sec ago
C++ | 39 sec ago
Lua | 1 min ago
None | 1 min ago
AutoIt | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By phlyingpenguin on the 11th of Sep 2008 05:23:38 PM Download | Raw | Embed | Report
  1. from OpenGL.GL import *
  2. from OpenGL.GLU import *
  3. from OpenGL.GLUT import *
  4. from numpy import *
  5.  
  6. class Turtle:
  7.         def __init__(self,x, y, theta):
  8.                 self.x = x
  9.                 self.y = y
  10.                 self.t = theta
  11.                 self.pen = 0
  12.                 self.trail = []
  13.        
  14.         """ Move forward 'distance' units """
  15.         def forward(self,distance):
  16.                 line = [(self.x,self.y)]
  17.                 self.x += distance * cos(radians(self.t))
  18.                 self.y += distance * sin(radians(self.t))
  19.                 line.append( (self.x, self.y) )
  20.                 if self.pen == 1: self.trail.append(line)
  21.                 glutPostRedisplay()
  22.        
  23.         """ Move right 'angle' degrees. Note that 'angle' is POSITIVE to move right """
  24.         def right(self,angle):
  25.                 self.t -= angle
  26.                 glutPostRedisplay()
  27.        
  28.         """ Turn left 'angle' degrees """
  29.         def left(self,angle):
  30.                 self.t += angle
  31.                 glutPostRedisplay()
  32.        
  33.         """ Set pen on or off (1/0) """
  34.         def pen(self,position):
  35.                 self.pen = position
  36.        
  37.         """ Flip pen bit """
  38.         def flip_pen(self):
  39.                 self.pen = self.pen ^ 1
  40.        
  41.         """ Use data to produce the current image """
  42.         def draw(self):
  43.                 """ shape x points """
  44.                 polys = [[(1.0, -0.99999999999999989),
  45.                                   (1.5000000000000002, -2.5),
  46.                                   (2.0, -1.9999999999999998)],
  47.                                  [(-0.99999999999999989, -1.0),
  48.                                   (-1.4999999999999998, -2.5),
  49.                                   (-1.9999999999999998, -2.0)],
  50.                                  [(-1.0, 0.99999999999999989),
  51.                                   (-1.5000000000000002, 2.5),
  52.                                   (-2.0, 1.9999999999999998)],
  53.                                  [(0.99999999999999989, 1.0),
  54.                                   (1.4999999999999998, 2.5),
  55.                                   (1.9999999999999998, 2.0)]]
  56.                 """ circle x ( [points] x radius) """
  57.                 circles = [[[(0.0, 0.0)], 2], [[(2.2000000000000002, 1.3471114790620887e-16)], 1]]
  58.                 """ trail """
  59.                 glColor3f(0.5, 0.3, 0.1)
  60.                 map(lambda x: self.draw_shape(GL_LINES, x), self.trail)
  61.  
  62.                 glColor3f(0.0, 0.8, 0.0)
  63.  
  64.                 """ Convert points into matricies and multiply """
  65.                 def rotate(x):
  66.                         C = matrix([
  67.                                 [cos(radians(self.t)), -sin(radians(self.t)), 0],
  68.                                 [sin(radians(self.t)), cos(radians(self.t)), 0],
  69.                                 [0,0,1]
  70.                                 ])
  71.                         p = matrix(list(x)+[0])
  72.                         p.shape = (3,1)
  73.                         p = C*p
  74.                         p = p.tolist()
  75.                         return (p[0][0],p[1][0])
  76.  
  77.                 polys = map(lambda x: map(lambda t: rotate(t), x), polys)
  78.                 circles = map(lambda x: [map(lambda y: rotate(y), x[0])] + [x[1]], circles)
  79.  
  80.                 """ Movement: """
  81.                 move = lambda y: map(lambda x:(x[0]+self.x, x[1]+self.y), y)
  82.                 polys = map(lambda t: move(t), polys)
  83.                 circles = map(lambda x: [move(x[0]), x[1]], circles)
  84.  
  85.                 """ Drawing: """
  86.                 map(lambda x: self.draw_shape(GL_POLYGON, x), polys)
  87.                 map(lambda x: self.circle(x[0][0][0], x[0][0][1], x[1]), circles)
  88.        
  89.         """ Abstract shape drawing """
  90.         def draw_shape(self,id,shape):
  91.                 translated = map(lambda x: x, shape)
  92.                 glBegin(id)
  93.                 map(lambda x: glVertex2f(x[0], x[1]), translated)
  94.                 glEnd()
  95.        
  96.         """ Draws a circle at x,y, radius in size. """
  97.         def circle(self, x, y, radius):
  98.                 self.draw_circle(x,y,radius,0.5)
  99.        
  100.         """
  101.         Draws a circle at x,y, radius in size.
  102.  
  103.         Taken from: http://www.allegro.cc/forums/thread/588625
  104.         """
  105.         def draw_circle(self,x,y,radius,accuracy):
  106.                 glDisable(GL_TEXTURE_2D)
  107.                 da = min( (2.0 * math.asin(1.0/radius)/accuracy), 0.5)
  108.                 glBegin(GL_TRIANGLE_FAN)
  109.                 glVertex2d(x,y)
  110.                 t = arange(0,2*pi,da)
  111.                 map(lambda a: glVertex2d(x + cos(a) * radius, y + sin(a) * radius), t)
  112.                 glVertex2d(x + radius, y)
  113.                 glEnd()
  114.  
  115. class TurtleClient:
  116.         def __init__(self):
  117.                 self.main()
  118.        
  119.         def display(self):
  120.                 glClear (GL_COLOR_BUFFER_BIT)
  121.                 glPushMatrix()
  122.  
  123.                 self.turt.draw()
  124.  
  125.                 glPopMatrix()
  126.                 glutSwapBuffers()
  127.  
  128.         def reshape(self, w, h):
  129.                 glViewport (0, 0, w, h)
  130.                 glMatrixMode(GL_PROJECTION)
  131.                 glLoadIdentity()
  132.                 glOrtho(-40.0, 40.0, -40.0, 40.0, -1.0, 1.0)
  133.                 glMatrixMode(GL_MODELVIEW)
  134.                 glLoadIdentity()
  135.  
  136.         def keyboard(self, key, x, y):
  137.                 if (key == 'q' or key == 'Q'): exit(0)
  138.        
  139.         def special_key(self, key, x, y):
  140.                 if key == GLUT_KEY_LEFT: self.turt.left(10)
  141.                 if key == GLUT_KEY_RIGHT: self.turt.right(10)
  142.                 if key == GLUT_KEY_UP: self.turt.forward(2)
  143.                 if key == GLUT_KEY_DOWN: self.turt.flip_pen()
  144.  
  145.         def main(self):
  146.                 self.turt = Turtle(0, 0, 0)
  147.                 glutInit()
  148.                 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB)
  149.                 glutInitWindowSize (500, 500)
  150.                 glutInitWindowPosition (100, 100)
  151.                 glutCreateWindow("o hai!")
  152.                 glClearColor(0.0, 0.0, 0.0, 0.0)
  153.                 glShadeModel(GL_FLAT)
  154.                 glutDisplayFunc(self.display)
  155.                 glutReshapeFunc(self.reshape)
  156.                 glutKeyboardFunc(self.keyboard)
  157.                 glutSpecialFunc(self.special_key)
  158.                 glutMainLoop()
  159.  
  160. tc = TurtleClient()
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: