<s> import pygame <EOL> from pygame . locals import * <EOL> from py2d . Bezier import * <EOL> from py2d . Math import * <EOL> SELECTION_DISTANCE = <NUM_LIT:20> <EOL> from examples import Example <EOL> class Cubic ( Example ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , runner ) : <EOL> self . runner = runner <EOL> self . title = "<STR_LIT>" <EOL> self . p1 = Vector ( <NUM_LIT:200> , <NUM_LIT> ) <EOL> self . p2 = Vector ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . c1 = Vector ( <NUM_LIT> , <NUM_LIT:50> ) <EOL> self . c2 = Vector ( <NUM_LIT:100> , <NUM_LIT:50> ) <EOL> self . points = ( ( '<STR_LIT>' , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , self . p1 ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , self . p2 ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , self . c1 ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , self . c2 ) ) <EOL> self . sel_point = None <EOL> def update ( self , time_elapsed ) : <EOL> pass <EOL> def render ( self ) : <EOL> pygame . draw . line ( self . runner . screen , <NUM_LIT> , self . p1 . as_tuple ( ) , self . c1 . as_tuple ( ) ) <EOL> pygame . draw . line ( self . runner . screen , <NUM_LIT> , self . p2 . as_tuple ( ) , self . c2 . as_tuple ( ) ) <EOL> for label , color , pos in self . points : <EOL> self . draw_point ( pos , color , label ) <EOL> bezier = [ self . p1 ] + flatten_cubic_bezier ( self . p1 , self . p2 , self . c1 , self . c2 ) + [ self . p2 ] <EOL> pygame . draw . lines ( self . runner . screen , <NUM_LIT> , False , [ p . as_tuple ( ) for p in bezier ] , <NUM_LIT:2> ) <EOL> if self . sel_point : <EOL> pygame . draw . ellipse ( self . runner . screen , <NUM_LIT> , pygame . Rect ( ( self . sel_point . x - <NUM_LIT:4> , self . sel_point . y - <NUM_LIT:4> ) , ( <NUM_LIT:8> , <NUM_LIT:8> ) ) , <NUM_LIT:1> ) <EOL> def draw_point ( self , p , color , label = None ) : <EOL> pygame . draw . ellipse ( self . runner . screen , color , pygame . Rect ( p . as_tuple ( ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> if label : <EOL> self . runner . screen . blit ( self . runner . font . render ( label , False , color ) , p . as_tuple ( ) ) <EOL> def mouse_down ( self , pos , button ) : <EOL> if button == <NUM_LIT:1> : <EOL> mouse = Vector ( * pos ) <EOL> nearest = min ( self . points , key = lambda p : ( p [ <NUM_LIT:2> ] - mouse ) . length ) <EOL> if ( nearest [ <NUM_LIT:2> ] - mouse ) . length_squared <= SELECTION_DISTANCE : <EOL> self . sel_point = nearest [ <NUM_LIT:2> ] <EOL> else : <EOL> self . sel_point = None <EOL> def mouse_move ( self , pos , rel , buttons ) : <EOL> if buttons [ <NUM_LIT:0> ] and self . sel_point : <EOL> self . sel_point . x , self . sel_point . y = pos <EOL> class Quadratic ( Example ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , runner ) : <EOL> self . runner = runner <EOL> self . title = "<STR_LIT>" <EOL> self . p1 = Vector ( <NUM_LIT:200> , <NUM_LIT> ) <EOL> self . p2 = Vector ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . c = Vector ( <NUM_LIT> , <NUM_LIT:50> ) <EOL> self . points = ( ( '<STR_LIT>' , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , self . p1 ) , <EOL> ( '<STR_LIT>' , ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , self . p2 ) , <EOL> ( '<STR_LIT:C>' , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) , self . c ) ) <EOL> self . sel_point = None <EOL> def update ( self , time_elapsed ) : <EOL> pass <EOL> def render ( self ) : <EOL> pygame . draw . line ( self . runner . screen , <NUM_LIT> , self . p1 . as_tuple ( ) , self . c . as_tuple ( ) ) <EOL> pygame . draw . line ( self . runner . screen , <NUM_LIT> , self . p2 . as_tuple ( ) , self . c . as_tuple ( ) ) <EOL> for label , color , pos in self . points : <EOL> self . draw_point ( pos , color , label ) <EOL> bezier = [ self . p1 ] + flatten_quadratic_bezier ( self . p1 , self . p2 , self . c ) + [ self . p2 ] <EOL> pygame . draw . lines ( self . runner . screen , <NUM_LIT> , False , [ p . as_tuple ( ) for p in bezier ] , <NUM_LIT:2> ) <EOL> if self . sel_point : <EOL> pygame . draw . ellipse ( self . runner . screen , <NUM_LIT> , pygame . Rect ( ( self . sel_point . x - <NUM_LIT:4> , self . sel_point . y - <NUM_LIT:4> ) , ( <NUM_LIT:8> , <NUM_LIT:8> ) ) , <NUM_LIT:1> ) <EOL> def draw_point ( self , p , color , label = None ) : <EOL> pygame . draw . ellipse ( self . runner . screen , color , pygame . Rect ( p . as_tuple ( ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> if label : <EOL> self . runner . screen . blit ( self . runner . font . render ( label , False , color ) , p . as_tuple ( ) ) <EOL> def mouse_down ( self , pos , button ) : <EOL> if button == <NUM_LIT:1> : <EOL> mouse = Vector ( * pos ) <EOL> nearest = min ( self . points , key = lambda p : ( p [ <NUM_LIT:2> ] - mouse ) . length ) <EOL> if ( nearest [ <NUM_LIT:2> ] - mouse ) . length_squared <= SELECTION_DISTANCE : <EOL> self . sel_point = nearest [ <NUM_LIT:2> ] <EOL> else : <EOL> self . sel_point = None <EOL> def mouse_move ( self , pos , rel , buttons ) : <EOL> if buttons [ <NUM_LIT:0> ] and self . sel_point : <EOL> self . sel_point . x , self . sel_point . y = pos </s>
