-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.py
61 lines (48 loc) · 1.26 KB
/
knn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from manim import *
import numpy as np, pygame
pygame.init()
width = 200
height = 200
running = True
# x_1 = 89
x_1 = 20
x_2 = 30
x_3 = 167
y_1 = 25
y_2 = 180
y_3 = 170
# x_4 = 94
# y_4 = 116
x_4 = 130
y_4 = 50
pt_A = np.array([x_1, y_1])
pt_B = np.array([x_2, y_2])
pt_C = np.array([x_3, y_3])
pts = np.array([[x_1, y_1], [x_2, y_2], [x_3, y_3], [x_4, y_4]])
scrn = pygame.display.set_mode((width, height))
scrn.fill(BLACK)
for i in range(height):
for j in range(width):
all_dists = [np.linalg.norm(pts[k] - np.array([j, i])) for k in range(len(pts))]
min_index = np.argmin(all_dists)
# if min_index == 3:
# scrn.set_at((j, i), BLUE)
# else:
# scrn.set_at((j, i), RED)
if min_index == 0:
scrn.set_at((j, i), RED)
elif min_index == 1:
scrn.set_at((j, i), GREEN)
elif min_index == 2:
scrn.set_at((j, i), BLUE)
else:
scrn.set_at((j, i), YELLOW)
print(i)
for i in pts:
pygame.draw.circle(scrn, BLACK, tuple(i), 2)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.quit()