<s> """<STR_LIT>""" <EOL> import time <EOL> import numpy as np <EOL> from scipy . spatial import cKDTree <EOL> from vispy import gloo <EOL> from vispy import app <EOL> VERT_SHADER = """<STR_LIT>""" <EOL> FRAG_SHADER = """<STR_LIT>""" <EOL> class Canvas ( app . Canvas ) : <EOL> def __init__ ( self ) : <EOL> app . Canvas . __init__ ( self , keys = '<STR_LIT>' ) <EOL> ps = self . pixel_scale <EOL> n = <NUM_LIT:1000> <EOL> self . particles = np . zeros ( <NUM_LIT:2> + n , [ ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:4> ) , <EOL> ( '<STR_LIT:size>' , '<STR_LIT>' , <NUM_LIT:1> * ps ) ] ) <EOL> self . boids = self . particles [ <NUM_LIT:2> : ] <EOL> self . target = self . particles [ <NUM_LIT:0> ] <EOL> self . predator = self . particles [ <NUM_LIT:1> ] <EOL> self . boids [ '<STR_LIT>' ] = np . random . uniform ( - <NUM_LIT> , + <NUM_LIT> , ( n , <NUM_LIT:3> ) ) <EOL> self . boids [ '<STR_LIT>' ] = np . random . uniform ( - <NUM_LIT> , + <NUM_LIT> , ( n , <NUM_LIT:3> ) ) <EOL> self . boids [ '<STR_LIT:size>' ] = <NUM_LIT:4> * ps <EOL> self . boids [ '<STR_LIT>' ] = <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> <EOL> self . target [ '<STR_LIT:size>' ] = <NUM_LIT:16> * ps <EOL> self . target [ '<STR_LIT>' ] [ : ] = <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> <EOL> self . predator [ '<STR_LIT:size>' ] = <NUM_LIT:16> * ps <EOL> self . predator [ '<STR_LIT>' ] [ : ] = <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> <EOL> self . target [ '<STR_LIT>' ] [ : ] = <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT:0> <EOL> self . _t = time . time ( ) <EOL> self . _pos = <NUM_LIT:0.0> , <NUM_LIT:0.0> <EOL> self . _button = None <EOL> width , height = self . physical_size <EOL> gloo . set_viewport ( <NUM_LIT:0> , <NUM_LIT:0> , width , height ) <EOL> self . program = gloo . Program ( VERT_SHADER , FRAG_SHADER ) <EOL> self . vbo_position = gloo . VertexBuffer ( self . particles [ '<STR_LIT>' ] <EOL> . copy ( ) ) <EOL> self . vbo_color = gloo . VertexBuffer ( self . particles [ '<STR_LIT>' ] . copy ( ) ) <EOL> self . vbo_size = gloo . VertexBuffer ( self . particles [ '<STR_LIT:size>' ] . copy ( ) ) <EOL> self . program [ '<STR_LIT>' ] = self . vbo_color <EOL> self . program [ '<STR_LIT:size>' ] = self . vbo_size <EOL> self . program [ '<STR_LIT>' ] = self . vbo_position <EOL> gloo . set_state ( clear_color = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , blend = True , <EOL> blend_func = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . _timer = app . Timer ( '<STR_LIT>' , connect = self . update , start = True ) <EOL> self . show ( ) <EOL> def on_resize ( self , event ) : <EOL> width , height = event . physical_size <EOL> gloo . set_viewport ( <NUM_LIT:0> , <NUM_LIT:0> , width , height ) <EOL> def on_mouse_press ( self , event ) : <EOL> self . _button = event . button <EOL> self . on_mouse_move ( event ) <EOL> def on_mouse_release ( self , event ) : <EOL> self . _button = None <EOL> self . on_mouse_move ( event ) <EOL> def on_mouse_move ( self , event ) : <EOL> if not self . _button : <EOL> return <EOL> w , h = self . size <EOL> x , y = event . pos <EOL> sx = <NUM_LIT:2> * x / float ( w ) - <NUM_LIT:1.0> <EOL> sy = - ( <NUM_LIT:2> * y / float ( h ) - <NUM_LIT:1.0> ) <EOL> if self . _button == <NUM_LIT:1> : <EOL> self . target [ '<STR_LIT>' ] [ : ] = sx , sy , <NUM_LIT:0> <EOL> elif self . _button == <NUM_LIT:2> : <EOL> self . predator [ '<STR_LIT>' ] [ : ] = sx , sy , <NUM_LIT:0> <EOL> def on_draw ( self , event ) : <EOL> gloo . clear ( ) <EOL> self . program . draw ( '<STR_LIT>' ) <EOL> self . _t = self . iteration ( time . time ( ) - self . _t ) <EOL> def iteration ( self , dt ) : <EOL> t = self . _t <EOL> t += <NUM_LIT:0.5> * dt <EOL> t += <NUM_LIT:0.5> * dt <EOL> self . boids [ '<STR_LIT>' ] = self . boids [ '<STR_LIT>' ] <EOL> self . boids [ '<STR_LIT>' ] = self . boids [ '<STR_LIT>' ] <EOL> n = len ( self . boids ) <EOL> P = self . boids [ '<STR_LIT>' ] <EOL> V = self . boids [ '<STR_LIT>' ] <EOL> C = - ( P - P . sum ( axis = <NUM_LIT:0> ) / n ) <EOL> A = - ( V - V . sum ( axis = <NUM_LIT:0> ) / n ) <EOL> D , I = cKDTree ( P ) . query ( P , <NUM_LIT:5> ) <EOL> M = np . repeat ( D < <NUM_LIT> , <NUM_LIT:3> , axis = <NUM_LIT:1> ) . reshape ( n , <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> Z = np . repeat ( P , <NUM_LIT:5> , axis = <NUM_LIT:0> ) . reshape ( n , <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> R = - ( ( P [ I ] - Z ) * M ) . sum ( axis = <NUM_LIT:1> ) <EOL> T = self . target [ '<STR_LIT>' ] - P <EOL> dP = P - self . predator [ '<STR_LIT>' ] <EOL> D = np . maximum ( <NUM_LIT:0> , <NUM_LIT> - <EOL> np . sqrt ( dP [ : , <NUM_LIT:0> ] ** <NUM_LIT:2> + <EOL> dP [ : , <NUM_LIT:1> ] ** <NUM_LIT:2> + <EOL> dP [ : , <NUM_LIT:2> ] ** <NUM_LIT:2> ) ) <EOL> D = np . repeat ( D , <NUM_LIT:3> , axis = <NUM_LIT:0> ) . reshape ( n , <NUM_LIT:3> ) <EOL> dP *= D <EOL> self . boids [ '<STR_LIT>' ] += <NUM_LIT> * C + <NUM_LIT> * A + <NUM_LIT> * R + <NUM_LIT> * T + <NUM_LIT> * dP <EOL> self . boids [ '<STR_LIT>' ] += self . boids [ '<STR_LIT>' ] <EOL> self . vbo_position . set_data ( self . particles [ '<STR_LIT>' ] . copy ( ) ) <EOL> return t <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> c = Canvas ( ) <EOL> app . run ( ) </s>
