<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , unicode_literals <EOL> from pyglet import font <EOL> from pyglet import clock <EOL> from pyglet import window <EOL> from pyglet import image <EOL> from pyglet . gl import * <EOL> from pyglet . window import mouse <EOL> from pyglet . window import event <EOL> from pyglet . window import key <EOL> class Base ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , x , y , z = <NUM_LIT:0> , color = ( <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> ) , stroke = <NUM_LIT:0> , rotation = <NUM_LIT:0> ) : <EOL> try : <EOL> self . rect <EOL> except AttributeError : <EOL> self . rect = Rect ( x , y , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> self . visible = <NUM_LIT:1> <EOL> self . z = z <EOL> self . rotation = rotation <EOL> self . stroke = stroke <EOL> self . color = color <EOL> self . q = gluNewQuadric ( ) <EOL> def setLoc ( self , p ) : self . rect . loc = p <EOL> def getLoc ( self ) : return self . rect . loc <EOL> def setX ( self , x ) : self . rect . x = x <EOL> def getX ( self ) : return self . rect . x <EOL> def setY ( self , y ) : self . rect . y = y <EOL> def getY ( self ) : return self . rect . y <EOL> loc = property ( getLoc , setLoc ) <EOL> x = property ( getX , setX ) <EOL> y = property ( getY , setY ) <EOL> def setWidth ( self , w ) : self . rect . width = w <EOL> def getWidth ( self ) : return self . rect . width <EOL> def setHeight ( self , h ) : self . rect . height = h <EOL> def getHeight ( self ) : return self . rect . height <EOL> width = property ( getWidth , setWidth ) <EOL> height = property ( getHeight , setHeight ) <EOL> class Pixel ( Base ) : <EOL> """<STR_LIT>""" <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> glColor4f ( * self . color ) <EOL> glPushMatrix ( ) <EOL> glTranslatef ( self . x , self . y , - self . z ) <EOL> glBegin ( GL_POINTS ) <EOL> glVertex3f ( <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ) <EOL> glEnd ( ) <EOL> glPopMatrix ( ) <EOL> def intersects ( self , x , y ) : <EOL> if x == self . x and y == self . y : return True <EOL> class Circle ( Base ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , x = <NUM_LIT:10> , y = <NUM_LIT:10> , z = <NUM_LIT:0> , width = <NUM_LIT:2> , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , stroke = <NUM_LIT:0> , rotation = <NUM_LIT:0.0> , style = GLU_FILL ) : <EOL> self . radius = width * <NUM_LIT:0.5> <EOL> self . rect = Rect ( x , y , width , width ) <EOL> self . style = style <EOL> self . circleresolution = <NUM_LIT> <EOL> Base . __init__ ( self , x , y , z , color , stroke , rotation ) <EOL> def setWidth ( self , w ) : <EOL> self . radius = w * <NUM_LIT:0.5> <EOL> self . rect . width = w <EOL> width = property ( Base . getWidth , setWidth ) <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> glColor4f ( * self . color ) <EOL> glPushMatrix ( ) <EOL> glTranslatef ( self . x , self . y , - self . z ) <EOL> glRotatef ( self . rotation , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.1> ) <EOL> if self . radius < <NUM_LIT:1> : self . radius = <NUM_LIT:1> <EOL> if self . stroke : <EOL> inner = self . radius - self . stroke <EOL> if inner < <NUM_LIT:0> : inner = <NUM_LIT:0> <EOL> else : <EOL> inner = <NUM_LIT:0> <EOL> gluQuadricDrawStyle ( self . q , self . style ) <EOL> gluDisk ( self . q , inner , self . radius , self . circleresolution , <NUM_LIT:1> ) <EOL> glPopMatrix ( ) <EOL> class Arc ( Base ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , x = <NUM_LIT:10> , y = <NUM_LIT:10> , z = <NUM_LIT:0> , radius = <NUM_LIT:1> , start = <NUM_LIT:0> , sweep = <NUM_LIT:1> , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , stroke = <NUM_LIT:0> , <EOL> rotation = <NUM_LIT:0.0> , style = GLU_FILL ) : <EOL> Base . __init__ ( self , x , y , z , color , stroke , rotation ) <EOL> self . radius = radius <EOL> self . start = start <EOL> self . sweep = sweep <EOL> self . style = style <EOL> self . circleresolution = <NUM_LIT> <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> glColor4f ( * self . color ) <EOL> glPushMatrix ( ) <EOL> glTranslatef ( self . x , self . y , - self . z ) <EOL> glRotatef ( self . rotation , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.1> ) <EOL> if self . stroke : <EOL> inner = self . radius - self . stroke <EOL> if inner < <NUM_LIT:0> : inner = <NUM_LIT:0> <EOL> else : <EOL> inner = <NUM_LIT:0> <EOL> self . start -= <NUM_LIT> <EOL> gluQuadricDrawStyle ( self . q , self . style ) <EOL> gluPartialDisk ( self . q , inner , self . radius , self . circleresolution , <NUM_LIT:1> , self . start , self . sweep ) <EOL> glPopMatrix ( ) <EOL> class Polygon ( Base ) : <EOL> def __init__ ( self , v , z = <NUM_LIT:0> , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , stroke = <NUM_LIT:0> , rotation = <NUM_LIT:0.0> , style = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . v = v <EOL> l , t , r , b = calcPolygonRect ( v ) <EOL> self . rect = Rect ( l + ( r - l ) * <NUM_LIT:0.5> , t + ( b - t ) * <NUM_LIT:0.5> , r - l , b - t ) <EOL> self . v2 = [ ( i [ <NUM_LIT:0> ] - self . rect . x , i [ <NUM_LIT:1> ] - self . rect . y ) for i in v ] <EOL> self . style = style <EOL> Base . __init__ ( self , self . rect . x , self . rect . y , z , color , stroke , rotation ) <EOL> def updateV ( self ) : <EOL> self . v = [ ( self . rect . x + n [ <NUM_LIT:0> ] , self . rect . y + n [ <NUM_LIT:1> ] ) for n in self . v2 ] <EOL> def setLoc ( self , p ) : <EOL> self . rect . loc = p ; self . updateV ( ) <EOL> def setX ( self , x ) : <EOL> self . rect . x = x ; self . updateV ( ) <EOL> def setY ( self , y ) : <EOL> self . rect . y = y ; self . updateV ( ) <EOL> x = property ( Base . getX , setX ) <EOL> y = property ( Base . getY , setY ) <EOL> loc = property ( Base . getLoc , setLoc ) <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> l , t , r , b = calcPolygonRect ( self . v ) <EOL> x , y = calcRectCenter ( l , t , r , b ) <EOL> self . drawVertex ( x , y , self . z , [ ( i [ <NUM_LIT:0> ] - x , i [ <NUM_LIT:1> ] - y ) for i in self . v ] , self . color , self . stroke , self . rotation , self . style ) <EOL> def drawVertex ( self , x , y , z = <NUM_LIT:0> , v = ( ) , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , stroke = <NUM_LIT:0> , rotation = <NUM_LIT:0.0> , style = <NUM_LIT:0> ) : <EOL> glColor4f ( * self . color ) <EOL> glPushMatrix ( ) <EOL> glTranslatef ( x , y , - z ) <EOL> glRotatef ( self . rotation , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.1> ) <EOL> if self . style : <EOL> glEnable ( GL_LINE_STIPPLE ) <EOL> glLineStipple ( <NUM_LIT:1> , style ) <EOL> if self . stroke : <EOL> glLineWidth ( self . stroke ) <EOL> glBegin ( GL_LINE_LOOP ) <EOL> else : <EOL> if len ( v ) == <NUM_LIT:4> : glBegin ( GL_QUADS ) <EOL> elif len ( v ) > <NUM_LIT:4> : glBegin ( GL_POLYGON ) <EOL> else : glBegin ( GL_TRIANGLES ) <EOL> for p in v : <EOL> glVertex3f ( p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> glEnd ( ) <EOL> if self . style : glDisable ( GL_LINE_STIPPLE ) <EOL> glPopMatrix ( ) <EOL> class LineRel ( Base ) : <EOL> def __init__ ( self , x , y , a = ( <NUM_LIT:0> , <NUM_LIT:0> ) , b = ( <NUM_LIT:0> , <NUM_LIT:0> ) , z = <NUM_LIT:0> , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , stroke = <NUM_LIT:0> , rotation = <NUM_LIT:0.0> , style = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> w = ( b [ <NUM_LIT:0> ] - a [ <NUM_LIT:0> ] ) <EOL> h = ( b [ <NUM_LIT:1> ] - a [ <NUM_LIT:1> ] ) <EOL> x = abs ( a [ <NUM_LIT:0> ] + w * <NUM_LIT:0.5> ) <EOL> y = abs ( a [ <NUM_LIT:1> ] + h * <NUM_LIT:0.5> ) <EOL> self . a2 = abs ( a [ <NUM_LIT:0> ] ) - x , abs ( a [ <NUM_LIT:1> ] ) - y <EOL> self . b2 = abs ( b [ <NUM_LIT:0> ] ) - x , abs ( b [ <NUM_LIT:1> ] ) - y <EOL> self . a = x - w * <NUM_LIT:0.5> , y - w * <NUM_LIT:0.5> <EOL> self . b = x + w * <NUM_LIT:0.5> , y + w * <NUM_LIT:0.5> <EOL> self . rect = Rect ( x , y , w , h ) <EOL> self . style = style <EOL> Base . __init__ ( self , x , y , z , color , stroke , rotation ) <EOL> def render ( self ) : <EOL> """<STR_LIT>""" <EOL> p1 = self . a2 <EOL> p2 = self . b2 <EOL> glColor4f ( * self . color ) <EOL> color = ( GLfloat * <NUM_LIT:4> ) ( * self . color ) <EOL> glPushMatrix ( ) <EOL> glTranslatef ( self . x , self . y , - self . z ) <EOL> glRotatef ( self . rotation , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.1> ) <EOL> if self . style : <EOL> glEnable ( GL_LINE_STIPPLE ) <EOL> glLineStipple ( <NUM_LIT:1> , self . style ) <EOL> if self . stroke <= <NUM_LIT:0> : <EOL> self . stroke = <NUM_LIT:1> <EOL> glLineWidth ( self . stroke ) <EOL> glBegin ( GL_LINES ) <EOL> glVertex2i ( int ( p1 [ <NUM_LIT:0> ] ) , int ( p1 [ <NUM_LIT:1> ] ) ) <EOL> glVertex2i ( int ( p2 [ <NUM_LIT:0> ] ) , int ( p2 [ <NUM_LIT:1> ] ) ) <EOL> glEnd ( ) <EOL> if self . style : <EOL> glDisable ( GL_LINE_STIPPLE ) <EOL> glPopMatrix ( ) <EOL> def updateAB ( self ) : <EOL> self . a = self . x + self . a [ <NUM_LIT:0> ] , self . y + self . a [ <NUM_LIT:0> ] <EOL> self . b = self . x + self . b [ <NUM_LIT:0> ] , self . y + self . b [ <NUM_LIT:0> ] <EOL> def setLoc ( self , p ) : <EOL> self . rect . loc = p ; self . updateAB ( ) <EOL> def setX ( self , x ) : <EOL> self . rect . x = x ; self . updateAB ( ) <EOL> def setY ( self , y ) : <EOL> self . rect . y = y ; self . updateAB ( ) <EOL> x = property ( Base . getX , setX ) <EOL> y = property ( Base . getY , setY ) <EOL> loc = property ( Base . getLoc , setLoc ) <EOL> class Line ( LineRel ) : <EOL> def __init__ ( self , a = ( <NUM_LIT:0> , <NUM_LIT:0> ) , b = ( <NUM_LIT:0> , <NUM_LIT:0> ) , z = <NUM_LIT:0> , color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , stroke = <NUM_LIT:0> , rotation = <NUM_LIT:0.0> , style = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> w = ( b [ <NUM_LIT:0> ] - a [ <NUM_LIT:0> ] ) <EOL> h = ( b [ <NUM_LIT:1> ] - a [ <NUM_LIT:1> ] ) <EOL> x = abs ( a [ <NUM_LIT:0> ] + w * <NUM_LIT:0.5> ) <EOL> y = abs ( a [ <NUM_LIT:1> ] + h * <NUM_LIT:0.5> ) <EOL> a = x - w * <NUM_LIT:0.5> , y - h * <NUM_LIT:0.5> <EOL> b = x + w * <NUM_LIT:0.5> , y + h * <NUM_LIT:0.5> <EOL> LineRel . __init__ ( self , x , y , a , b , z , color , stroke , rotation , style ) <EOL> class Rect ( object ) : <EOL> def __init__ ( self , x = <NUM_LIT:0> , y = <NUM_LIT:0> , w = <NUM_LIT:0> , h = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . rect = x , y , w , h <EOL> def setRect ( self , r ) : <EOL> self . __x = r [ <NUM_LIT:0> ] <EOL> self . __y = r [ <NUM_LIT:1> ] <EOL> self . __width = r [ <NUM_LIT:2> ] <EOL> self . __height = r [ <NUM_LIT:3> ] <EOL> w = r [ <NUM_LIT:2> ] * <NUM_LIT:0.5> ; h = r [ <NUM_LIT:3> ] * <NUM_LIT:0.5> <EOL> self . __rect = r [ <NUM_LIT:0> ] - w , r [ <NUM_LIT:1> ] - h , r [ <NUM_LIT:0> ] + w , r [ <NUM_LIT:1> ] + h <EOL> def getRect ( self ) : <EOL> return self . __rect <EOL> rect = property ( getRect , setRect ) <EOL> def setQuad ( self , q ) : <EOL> self . rect = q [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] + ( q [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] - q [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) * <NUM_LIT:0.5> , q [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] + ( q [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] - q [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) * <NUM_LIT:0.5> , q [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] - q [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , q [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] - q [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def getQuad ( self ) : <EOL> return [ ( self . rect [ <NUM_LIT:0> ] , self . rect [ <NUM_LIT:1> ] ) , ( self . rect [ <NUM_LIT:2> ] , self . rect [ <NUM_LIT:1> ] ) , ( self . rect [ <NUM_LIT:2> ] , self . rect [ <NUM_LIT:3> ] ) , ( self . rect [ <NUM_LIT:0> ] , self . rect [ <NUM_LIT:3> ] ) ] <EOL> quad = property ( getQuad , setQuad ) <EOL> def setX ( self , x ) : <EOL> self . rect = x , self . y , self . width , self . height <EOL> def getX ( self ) : return self . __x <EOL> x = property ( getX , setX ) <EOL> def setY ( self , y ) : <EOL> self . rect = self . x , y , self . width , self . height <EOL> def getY ( self ) : return self . __y <EOL> y = property ( getY , setY ) <EOL> def setLoc ( self , p ) : <EOL> self . rect = p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] , self . width , self . height <EOL> def getLoc ( self ) : return self . __x , self . __y <EOL> loc = property ( getLoc , setLoc ) <EOL> def setWidth ( self , w ) : <EOL> self . rect = self . x , self . y , w , self . height <EOL> def getWidth ( self ) : return self . __width <EOL> width = property ( getWidth , setWidth ) <EOL> def setHeight ( self , h ) : <EOL> self . rect = self . x , self . y , self . width , h <EOL> def getHeight ( self ) : return self . __height <EOL> height = property ( getHeight , setHeight ) <EOL> def setLeft ( self , x ) : <EOL> self . rect = x + self . width * <NUM_LIT:0.5> , self . y , self . width , self . height <EOL> def getLeft ( self ) : return self . rect [ <NUM_LIT:0> ] <EOL> left = property ( getLeft , setLeft ) <EOL> def setTop ( self , y ) : <EOL> self . rect = self . x , y + self . width * <NUM_LIT:0.5> , self . width , self . height <EOL> def getTop ( self ) : return self . rect [ <NUM_LIT:1> ] <EOL> top = property ( getTop , setTop ) <EOL> def setRight ( self , x ) : <EOL> self . rect = x - self . width * <NUM_LIT:0.5> , self . y , self . width , self . height <EOL> def getRight ( self ) : return self . rect [ <NUM_LIT:2> ] <EOL> right = property ( getRight , setRight ) <EOL> def setBottom ( self , x ) : <EOL> self . rect = self . x , y - self . width * <NUM_LIT:0.5> , self . width , self . height <EOL> def getBottom ( self ) : return self . rect [ <NUM_LIT:3> ] <EOL> bottom = property ( getBottom , setBottom ) <EOL> def calcPolygonRect ( pointArray ) : <EOL> """<STR_LIT>""" <EOL> l , t , r , b = <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , - <NUM_LIT> <EOL> for n in pointArray : <EOL> if n [ <NUM_LIT:0> ] < l : l = n [ <NUM_LIT:0> ] <EOL> if n [ <NUM_LIT:0> ] > r : r = n [ <NUM_LIT:0> ] <EOL> if n [ <NUM_LIT:1> ] < t : t = n [ <NUM_LIT:1> ] <EOL> if n [ <NUM_LIT:1> ] > b : b = n [ <NUM_LIT:1> ] <EOL> return l , t , r , b <EOL> def calcRectCenter ( l , t , r , b ) : <EOL> """<STR_LIT>""" <EOL> return l + ( ( r - l ) * <NUM_LIT:0.5> ) , t + ( ( b - t ) * <NUM_LIT:0.5> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import random <EOL> win = window . Window ( ) <EOL> glClearColor ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> ) <EOL> glEnable ( GL_BLEND ) <EOL> glBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ) <EOL> p = Pixel ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> c = Circle ( <NUM_LIT:200> , <NUM_LIT:100> , width = <NUM_LIT:100> , color = ( <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT:0.> , <NUM_LIT:1.> ) ) <EOL> a = Arc ( <NUM_LIT> , <NUM_LIT> , radius = <NUM_LIT:100> , color = ( <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:1.> ) , sweep = <NUM_LIT> , style = GLU_FILL ) <EOL> P = Polygon ( [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:50> , <NUM_LIT:200> ) , ( <NUM_LIT> , <NUM_LIT:200> ) , ( <NUM_LIT> , <NUM_LIT:100> ) , ( <NUM_LIT:100> , <NUM_LIT:5> ) ] , color = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> ) ) <EOL> l = Line ( ( <NUM_LIT:10> , <NUM_LIT> ) , ( <NUM_LIT:100> , <NUM_LIT> ) , stroke = <NUM_LIT:2> , color = ( <NUM_LIT:0> , <NUM_LIT:0.> , <NUM_LIT:1.> , <NUM_LIT:1.> ) ) <EOL> while not win . has_exit : <EOL> win . dispatch_events ( ) <EOL> col = [ random . random ( ) for i in range ( <NUM_LIT:3> ) ] + [ <NUM_LIT:1> ] <EOL> glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) <EOL> c . color = col <EOL> c . render ( ) <EOL> p . render ( ) <EOL> a . render ( ) <EOL> a . rotation += <NUM_LIT:1> <EOL> P . render ( ) <EOL> l . render ( ) <EOL> win . flip ( ) </s>
