-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawgraph2.py
129 lines (105 loc) · 3.44 KB
/
drawgraph2.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import logging
import random
from typing import List
# not all imports are currently used, but they might be in the future and it shows all available functionalities
from socha import (
Accelerate,
AccelerationProblem,
Advance,
AdvanceInfo,
AdvanceProblem,
Board,
CartesianCoordinate,
CubeCoordinates,
CubeDirection,
Field,
FieldType,
GameState,
Move,
Passenger,
Push,
PushProblem,
Segment,
Ship,
TeamEnum,
TeamPoints,
Turn,
TurnProblem,
)
from socha.api.networking.game_client import IClientHandler
from socha.starter import Starter
import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np
start: CubeDirection = CubeDirection.Right
directionList: List[CubeDirection] = [ # Right -> Clockwise
CubeDirection.Right,
CubeDirection.DownRight,
CubeDirection.DownLeft,
CubeDirection.Left,
CubeDirection.UpLeft,
CubeDirection.UpRight
]
def draw(G: nx.DiGraph):
colors = []
hcoord = []
vcoord = []
for n in G.nodes:
coords3 = n.split(';')
cube = CubeCoordinates(int(coords3[0]), int(coords3[1]))
if G.nodes[n]['fieldType'] == FieldType.Water:
if not G.nodes[n]['hasStream']:
colors.append(["blue"])
else:
colors.append(["lightseagreen"])
elif G.nodes[n]['fieldType'] == FieldType.Goal:
colors.append(["gold"])
elif G.nodes[n]['fieldType'] == FieldType.Island:
colors.append(["green"])
elif G.nodes[n]['fieldType'] == FieldType.Passenger:
colors.append(["red"])
q = int(coords3[0])
r = int(coords3[1])
s = int(coords3[2])
hcoord.append(2. * np.sin(np.radians(60)) * (q-s) /3.)
vcoord.append(r)
fig, ax = plt.subplots(dpi=70)
ax.set_aspect('equal')
for x, y, c, n in zip(hcoord, vcoord, colors, G.nodes.data()):
node = n[0]
nodeData = n[1]
color = c[0]
alpha = 0.3
if nodeData['segment'] % 2 == 1:
alpha += 0.2
hex = RegularPolygon((x, y), numVertices=6, radius=2. / 3.,
orientation=np.radians(0),
facecolor=color, alpha=alpha, edgecolor='k')
ax.add_patch(hex)
starts = ['next', 'start', 'me']
counter = 0 # distances
for s in starts:
counter += 1
spaces = ' '
for i in range(counter * 5):
spaces += ' '
if nodeData['meDirection'] != None: # direction
ax.text(x, y, spaces + '>', ha='center', va='center', size=10, rotation = (6 - directionList.index(nodeData[s + 'Direction'])) * 60, color='indigo')
ax.text(x, y-0.3, node, ha='center', va='center', size=7) # coord
counter = 0 # distances
for s in starts:
counter += 1
d = nodeData[s + 'Distance']
if d >= 999999999999:
d = 'inf'
ax.text(x, y + counter / 7, s[0] + ' ' + str(d), ha='center', va='center', size=7)
# Also add scatter points in hexagon centres
ax.scatter(hcoord, vcoord, c=[c[0] for c in colors], alpha=0.5)
plt.gca().invert_yaxis()
plt.gca().axes.get_xaxis().set_visible(False)
plt.gca().axes.get_yaxis().set_visible(False)
plt.show()
G = nx.DiGraph()
G.add_nodes_from()
draw(G)