-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.py
74 lines (57 loc) · 2.36 KB
/
mapper.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
from coordinates import Coordinate
def map_individual(individual):
# first step always straight (1)
coord_back_2 = Coordinate(0, 0)
coord_back_1 = Coordinate(1, 0)
coordinates = [coord_back_2, coord_back_1]
for i in range(2, len(individual) + 2):
step = individual[i - 2]
# find out current orientation
# east
if coord_back_2.x < coord_back_1.x and coord_back_2.y == coord_back_1.y:
# and go straight
if step == 1:
new_coord = Coordinate(coord_back_1.x + 1, coord_back_1.y)
# and go left
if step == 0:
new_coord = Coordinate(coord_back_1.x, coord_back_1.y + 1)
# and go right
if step == 2:
new_coord = Coordinate(coord_back_1.x, coord_back_1.y - 1)
# north
if coord_back_2.x == coord_back_1.x and coord_back_2.y < coord_back_1.y:
# and go straight
if step == 1:
new_coord = Coordinate(coord_back_1.x, coord_back_1.y + 1)
# and go left
if step == 0:
new_coord = Coordinate(coord_back_1.x - 1, coord_back_1.y)
# and go right
if step == 2:
new_coord = Coordinate(coord_back_1.x + 1, coord_back_1.y)
# west
if coord_back_2.x > coord_back_1.x and coord_back_2.y == coord_back_1.y:
# and go straight
if step == 1:
new_coord = Coordinate(coord_back_1.x - 1, coord_back_1.y)
# and go left
if step == 0:
new_coord = Coordinate(coord_back_1.x, coord_back_1.y - 1)
# and go right
if step == 2:
new_coord = Coordinate(coord_back_1.x, coord_back_1.y + 1)
# south
if coord_back_2.x == coord_back_1.x and coord_back_2.y > coord_back_1.y:
# and go straight
if step == 1:
new_coord = Coordinate(coord_back_1.x, coord_back_1.y - 1)
# and go left
if step == 0:
new_coord = Coordinate(coord_back_1.x + 1, coord_back_1.y)
# and go right
if step == 2:
new_coord = Coordinate(coord_back_1.x - 1, coord_back_1.y)
coordinates.append(new_coord)
coord_back_2 = coord_back_1
coord_back_1 = new_coord
return coordinates