This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_module.py
70 lines (57 loc) · 2.15 KB
/
map_module.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
#!/usr/bin/env python3
"""
Define map operations: create new map with random landmarks,
get a picture of the map, print the map as a matrix.
"""
__author__ = "Gabriel Hishida and Allan Cedric"
import numpy as np
from random import randrange
import cv2 as cv
# MACROS:
EMPTY_SYMBOL = "-"
LANDMARK_SYMBOL = "X"
def random_map(rows, columns, landmarks_count, EMPTY, LANDMARK):
"""Makes a numpy.ndarray of a map with random landmarks"""
# make map matrix
map = [[EMPTY for j in range(columns)] for i in range(rows)]
map = np.array(map)
# insert landmarks
# landmarks are tuples of (ladmark_id, (landmark_coordinate))
landmarks = []
for count in range(landmarks_count):
has_placed = False
while not has_placed:
i = randrange(rows)
j = randrange(columns)
# try to place
if map[i, j] != LANDMARK:
map[i, j] = LANDMARK
has_placed = True
landmarks.append((count, (j, i)))
return map, landmarks
class Map:
"""Map class"""
EMPTY = EMPTY_SYMBOL
LANDMARK = LANDMARK_SYMBOL
def __init__(self, rows, columns, num_landmarks):
"""Get a new random map"""
self.rows, self.columns, self.num_landmarks = rows, columns, num_landmarks
self.matrix, self.landmarks = random_map(self.rows, self.columns, self.num_landmarks,
self.EMPTY, self.LANDMARK)
def __str__(self):
"""Get the matrix as a string"""
str = ""
for row in self.matrix:
for char in row:
str += char
str += "\n"
return str
def get_picture(self, magnitude=8, negated=0):
"""Get a numpy BGR matrix of the map, augmented $magnitude times"""
mask = np.zeros((self.rows*magnitude, self.columns*magnitude, 3), dtype=np.uint8)
mask.fill(255 * negated)
for i in range(self.rows):
for j in range(self.columns):
if self.matrix[i, j] == self.LANDMARK:
mask[i*magnitude:i*magnitude+magnitude-1, j*magnitude:j*magnitude+magnitude-1] = 255 * (not negated)
return mask