+/GENN

genetically evolving neural networks

git clone https://git.julia.blue/+/GENN

Joseph RyanAdd legacy projectae5ad7f

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