+/GENN
genetically evolving neural networks
git clone https://git.julia.blue/+/GENN
ae5ad7f
v0.2
1import sys 2import pygame 3import math 4 5 6class Vector (): 7x = 0 8y = 0 9 10def __init__ (self ,pos = (0 ,0 )): 11self .x = float (pos [0 ]) 12self .y = float (pos [1 ]) 13 14def magnitude (self ): 15return math .sqrt (self .x ** 2 + self .y ** 2 ) 16 17def norm (self ): 18m = self .magnitude 19return Vector ((self .x / m ,self .y / m )) 20 21def dot (self ,other ): 22return self .x * other .x + self .y * other .y 23 24def rotate (self ,angle ): 25return Vector ((self .x * math .cos (angle )- self .y * math .sin (angle ),self .x * math .sin (angle )+ self .y * math .cos (angle ))) 26 27def __add__ (self ,other ): 28return Vector ((self .x + other .x ,self .y + other .y )) 29 30def __sub__ (self ,other ): 31return Vector ((self .x - other .x ,self .y - other .y )) 32 33def __mul__ (self ,other ): 34return Vector ((self .x * other .x ,self .y * other .y )) 35 36def __div__ (self ,other ): 37return Vector ((self .x / other .x ,self .y / other .y )) 38 39def __eq__ (self ,other ): 40return self .x == other .x and self .y == other .y 41 42def toTup (self ): 43return (self .x ,self .y ) 44 45def toTupInt (self ): 46return (int (self .x ),int (self .y )) 47 48 49def main (): 50pygame .init () 51screenSize = screenWidth ,screenHeight = 640 ,480 52screen = pygame .display .set_mode (screenSize ) 53 54a = Vector ((100 ,300 )) 55b = Vector ((578 ,400 )) 56red = (255 ,0 ,0 ) 57w = 5 58h = 25 59 60while 1 : 61screen .fill ((100 ,100 ,100 )) 62for event in pygame .event .get (): 63if event .type == pygame .QUIT : 64sys .exit () 65mouse = Vector ((float (pygame .mouse .get_pos ()[0 ]),float ( 66pygame .mouse .get_pos ()[1 ]))) 67ab = b - a 68ac = mouse - a 69d = ab .dot (ac )/ ab .dot (ab ) 70final = a + ab * Vector ((d ,d )) 71 72p = Vector ((0 ,mouse .y )) 73 74# drawing 75pygame .draw .line (screen ,red ,a .toTupInt (),b .toTupInt ()) 76pygame .draw .line (screen ,red ,final .toTupInt (),mouse .toTupInt ()) 77pygame .draw .rect (screen , (255 ,255 ,255 ), (p .x ,p .y ,w ,h )) 78pygame .display .flip () 79 80if __name__ == "__main__" : 81main ()