-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid2D.py
81 lines (58 loc) · 1.88 KB
/
Grid2D.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
77
78
79
80
81
import numpy as np
class Grid2D_Fixed:
def __init__(self, w, h):
self.m_Width = w
self.m_Height = h
self.m_buff0 = np.zeros((self.m_Height, self.m_Width), dtype=np.int32) #np.matrix((self.m_Height, self.m_Width), dtype=np.int8)
self.m_buff1 = np.zeros((self.m_Height, self.m_Width), dtype=np.int32)
self.m_const = 0
#In initial condition, both buffers should be set with the same state
#in order to guarantee the correct state render
def initCond(self, l, c, s):
self.m_buff1[l][c] = s
self.m_buff0[l][c] = s
def setConstant(self, c):
self.m_const = c
def setState(self, l, c, s):
self.m_buff1[l][c] = s
def getState(self, l, c):
if l >= 0 and l < self.m_Height and c >= 0 and c < self.m_Width:
return self.m_buff0[l][c]
else:
return self.m_const
def getWidth (self):
return self.m_Width
def getHeight (self):
return self.m_Height
def swap(self):
aux = self.m_buff1
self.m_buff1 = self.m_buff0
self.m_buff0 = aux
class Grid2D_Periodic(Grid2D_Fixed):
pass
def getState(self, l, c):
l1 = l
c1 = c
if l1 < 0:
l1 = self.m_Height + l1
if l1 >= self.m_Height:
l1 = l1 % self.m_Height
if c1 < 0:
c1 = self.m_Width + c1
if c1 >= self.m_Width:
c1 = c1 % self.m_Width
return self.m_buff0[l1][c1]
class Grid2D_Reflective(Grid2D_Fixed):
pass
def getState(self, l, c):
l1 = l
c1 = c
if l1 < 0:
l1 = (l1 * -1)
if l1 >= self.m_Height:
l1 = self.m_Height - (l1 % self.m_Height) - 1
if c1 < 0:
c1 = (c1 * -1)
if c1 == self.m_Width:
c1 = self.m_Width - (c1 % self.m_Width) - 1
return self.m_buff0[l1][c1]