You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

class Rule(object):
def __init__(self, rule_id, r = 1, k = 2):
self.r = r
self.k = k
self.a = 2 * r + 1
self.a2 = self.a**2
self.rule_table = k**self.a2 * [0]
self.setRule(rule_id)
def setRule(self, rule_id):
if rule_id < 0:
self.rule_id = 0
elif rule_id >= self.k**self.a2:
self.rule_id = self.k**self.a2 - 1
else:
self.rule_id = rule_id
self.generateRuleTable()
def applyRule(self, matrix_field, shape, pos_y, pos_x):
e = 0
sum = 0
h, w = shape
for i in range(pos_y - self.r, pos_y + self.r + 1):
for j in range(pos_x - self.r, pos_x + self.r + 1):
y = i if i < h else i - h
x = j if j < w else j - w
sum += matrix_field[y][x] * self.k**e
e += 1
return self.rule_table[sum]
def generateRuleTable(self):
id = self.rule_id
i = 0
while id > 0:
self.rule_table[i] = id % self.k
id = id // 2
i = i + 1
#print(self.rule_table)