-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
76 lines (64 loc) · 3.11 KB
/
grid.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import pygame
from scipy import ndimage
import joblib
class Grid:
"""Grid array operaton class"""
def __init__(self, width=1920, height=1080, scale=20, offset=1):
self.width = width
self.height = height
self.scale = scale
self.offset = offset
self.window_offset = 100
# self.rows = self.height//self.scale
self.rows = 28
# self.columns = self.width//self.scale
self.columns = 28
self.grid_array = np.zeros((self.rows, self.columns))
# self.grid_array = np.random.randint(0, 2, (self.rows, self.columns))
def render(self, screen=None, filter=False, sigma=1):
"""Renders the grid array"""
for x in np.arange(self.columns):
for y in np.arange(self.rows):
rect = pygame.Rect(self.window_offset + x*self.scale, self.window_offset + 100 \
+ y*self.scale, self.scale-self.offset, self.scale-self.offset)
if filter == False:
if self.grid_array[y,x] == 0:
pygame.draw.rect(screen, (0,0,0), rect)
elif self.grid_array[y,x] > 0:
pygame.draw.rect(screen, (11,11,80), rect)
else:
array = self.get_gaussianBlurerd_array(sigma=sigma)
color = (array[y,x]%255, array[y,x]%255, array[y,x]%255)
pygame.draw.rect(screen, color, rect)
def update_array(self, x, y):
"""takes mouse position (x,y) and updates the grid"""
m_pos = pygame.mouse.get_pos()
j = (x - self.window_offset) // self.scale
i = (y - self.window_offset) // self.scale
if (m_pos[0] >= self.window_offset and m_pos[0] <= self.columns*self.scale + self.window_offset) \
and (m_pos[1] >= self.window_offset and m_pos[1] <= self.rows*self.scale + self.window_offset):
try:
if self.grid_array[i,j] != None:
self.grid_array[i,j] = 255 #center cell ie clicked cell
# neighbor cells
self.grid_array[i-1,j-1] = 255
self.grid_array[i-1,j] = 255
self.grid_array[i-1,j+1] = 255
self.grid_array[i+1,j-1] = 255
self.grid_array[i+1,j] = 255
self.grid_array[i+1,j+1] = 255
self.grid_array[i,j-1] = 255
self.grid_array[i,j+1] = 255
except IndexError:
pass
# joblib.dump(self.grid_array, "array_dump.pkl")
def reset_array(self):
"""reset the array to zeros"""
self.grid_array = np.zeros((self.rows, self.columns))
def get_gaussianBlurerd_array(self, sigma=1.0):
"""returns the gaussian blurred array"""
return ndimage.gaussian_filter(self.grid_array, sigma=sigma)
# Image Processing Stuffs
def gaussian_blur(self):
self.grid_array = ndimage.gaussian_filter(self.grid_array, sigma=4)