From 889f7eaef6b51d4c56c43feef0a8711127b84286 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Tue, 3 Dec 2019 19:10:15 -0500
Subject: [PATCH] Update node coord logic to be even less random, for directed
 type graphs

---
 resources/graphs/basic_graph/graph.py | 100 +++++++++++++++++++++++---
 1 file changed, 90 insertions(+), 10 deletions(-)

diff --git a/resources/graphs/basic_graph/graph.py b/resources/graphs/basic_graph/graph.py
index fb6473e..f3e10d1 100644
--- a/resources/graphs/basic_graph/graph.py
+++ b/resources/graphs/basic_graph/graph.py
@@ -230,18 +230,16 @@ class BasicGraphDisplay():
 
         # If start or end node lists exist, we want to limit coordinate positioning slightly.
         if len(start_node_list) != 0 or len(end_node_list) != 0:
-            logger.info('start or end node lists is greater than length 0.')
 
             # Create list of "possible_positions". Should exclude far left or far right coordinates.
             for x_index in range(int(max_direction)):
                 # We want to limit x-coords, preventing random assignment to x-coords less than 10 or greater than 90.
-                if x_index > 10 and x_index < 90:
+                if x_index >= 10 and x_index <= 90:
                     # Given x-coord is between values we want. Proceed.
                     for y_index in range(int(max_direction)):
                         possible_positions.append((x_index, y_index))
 
         else:
-            logger.info('Start or end node lists is length 0.')
             # Create list of "possible_positions". Should be a list of every possible (x,y) coordinate pairing.
             for x_index in range(int(max_direction)):
                 for y_index in range(int(max_direction)):
@@ -262,7 +260,9 @@ class BasicGraphDisplay():
         if end_node_count > 0:
             state_index = 1
             grid_length = max_direction / end_node_count
-            for end_state in end_node_list:
+
+            # Set coords of all nodes in "end" list.
+            for end_node in end_node_list:
                 # Get grid values.
                 current_grid_end = grid_length * state_index
                 current_grid_start = current_grid_end - grid_length
@@ -271,18 +271,57 @@ class BasicGraphDisplay():
                 # Assign node to calculated grid.
                 x_coord = 95
                 y_coord = int((current_grid_start + current_grid_end) / 2)
-                self._parent._nodes[end_state.get_name()].x_coord = x_coord
-                self._parent._nodes[end_state.get_name()].y_coord = y_coord
+                self._parent._nodes[end_node.get_name()].x_coord = x_coord
+                self._parent._nodes[end_node.get_name()].y_coord = y_coord
 
                 # Record extreme positions.
                 if x_coord > farthest_x[0]:
-                    farthest_x = (x_coord, end_state)
+                    farthest_x = (x_coord, end_node)
                 if x_coord < shortest_x[0]:
-                    shortest_x = (x_coord, end_state)
+                    shortest_x = (x_coord, end_node)
                 if y_coord > farthest_y[0]:
-                    farthest_y = (y_coord, end_state)
+                    farthest_y = (y_coord, end_node)
                 if y_coord < shortest_y[0]:
-                    shortest_y = (y_coord, end_state)
+                    shortest_y = (y_coord, end_node)
+
+                # Assign all directly connected nodes to current "end" state node.
+                for connected_node in end_node.get_connected_nodes().values():
+
+                    # Make sure connected node is not in "start" node list.
+                    if connected_node not in start_node_list:
+                        x_coord = 90
+                        search_for_coord = True
+
+                        while search_for_coord:
+                            try:
+                                y_coord = randint(0, 100)
+                                possible_positions.remove((x_coord, y_coord))
+                                search_for_coord = False
+                            except ValueError:
+                                # Coord was already taken by another node. Try again.
+                                pass
+
+                        connected_node.x_coord = x_coord
+                        connected_node.y_coord = y_coord
+
+                        # Record extreme positions.
+                        if x_coord > farthest_x[0]:
+                            farthest_x = (x_coord, connected_node)
+                        if x_coord < shortest_x[0]:
+                            shortest_x = (x_coord, connected_node)
+                        if y_coord > farthest_y[0]:
+                            farthest_y = (y_coord, connected_node)
+                        if y_coord < shortest_y[0]:
+                            shortest_y = (y_coord, connected_node)
+
+            # Since we found "end" nodes, remove some possible coord positions.
+            for x_index in range(87, 90):
+                for y_index in range(100):
+                    try:
+                        # Remove given coordinates from list of possible positions.
+                        possible_positions.remove((x_index, y_index))
+                    except ValueError:
+                        pass    # Position was already removed from list. This is fine.
 
         # Loop through all initial states, assigning y-coords based on a divided grid setup.
         # Grids are from top to bottom, to evenly distribute space to all start nodes.
@@ -290,6 +329,8 @@ class BasicGraphDisplay():
         if start_node_count > 0:
             state_index = 1
             grid_length = max_direction / start_node_count
+
+            # Set coords of all nodes in "start" list.
             for start_node in start_node_list:
                 # Get grid values.
                 current_grid_end = grid_length * state_index
@@ -312,6 +353,45 @@ class BasicGraphDisplay():
                 if y_coord < shortest_y[0]:
                     shortest_y = (y_coord, start_node)
 
+                # Assign all directly connected nodes to current "start" state node.
+                for connected_node in start_node.get_connected_nodes().values():
+
+                    # Make sure connected node is not in "end" node list.
+                    if connected_node not in end_node_list:
+                        x_coord = 10
+                        search_for_coord = True
+
+                        while search_for_coord:
+                            try:
+                                y_coord = randint(0, 100)
+                                possible_positions.remove((x_coord, y_coord))
+                                search_for_coord = False
+                            except ValueError:
+                                # Coord was already taken by another node. Try again.
+                                pass
+
+                        connected_node.x_coord = x_coord
+                        connected_node.y_coord = y_coord
+
+                        # Record extreme positions.
+                        if x_coord > farthest_x[0]:
+                            farthest_x = (x_coord, connected_node)
+                        if x_coord < shortest_x[0]:
+                            shortest_x = (x_coord, connected_node)
+                        if y_coord > farthest_y[0]:
+                            farthest_y = (y_coord, connected_node)
+                        if y_coord < shortest_y[0]:
+                            shortest_y = (y_coord, connected_node)
+
+            # Since we found "start" nodes, remove some possible coord positions.
+            for x_index in range(10, 13):
+                for y_index in range(100):
+                    try:
+                        # Remove given coordinates from list of possible positions.
+                        possible_positions.remove((x_index, y_index))
+                    except ValueError:
+                        pass  # Position was already removed from list. This is fine.
+
         # Loop through all currently unassigned nodes and assign coordinates.
         for node in self._parent.nodes.all().values():
             # Make sure node is not already assigned coordinates.
-- 
GitLab