Here is the function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A node is defined as a junction of 3 or more branches. | |
def node_checking(x_mat, x_list, row, col, x_row, x_col): | |
if ((row==0 and col==0) or (row==x_row-1 and col==x_col-1) or \ | |
(row==0 and col==x_col-1) or (row==x_row-1 and col==0)): | |
# If its a corner point it can't be a node. | |
# This prevents array index going out of range. | |
pass | |
# The next cases, can't be outer edges or corner points. | |
else: | |
if (row==0): | |
# If it is the first row, | |
# check if the element in the next and | |
# previous columns and same row are connected. | |
if not (x_mat[row][col+1]=='' or x_mat[row][col-1]==''): | |
# Then check if the element in next row and | |
# same column is connected. Look for a T junction. | |
if not (x_mat[row+1][col]==''): | |
x_list.append([row, col]) | |
if (row==x_row-1): | |
# If it is the last row, | |
# check if the elements in the next and | |
# previous columns and same row are connected. | |
if not (x_mat[row][col+1]=='' or x_mat[row][col-1]==''): | |
if not (x_mat[row-1][col]==''): | |
# Then check if element in the previous row and | |
# same column is connected. Look for a T junction. | |
x_list.append([row, col]) | |
if (col==0): | |
# If it is the first column, | |
# check if the element in the next column and | |
# same row is connected. | |
if not (x_mat[row][col+1]==''): | |
# Then check if the elements in next and | |
# previous row and same column are connected. | |
# Look for a T junction. | |
if not (x_mat[row+1][col]=='' or x_mat[row-1][col]==''): | |
x_list.append([row, col]) | |
if (col==x_col-1): | |
# If it is the last column, | |
# check if the element in the previous column and | |
# same row is connected. | |
if not (x_mat[row][col-1]==''): | |
# Then check if the elements in next and | |
# previous row and same column are connected. | |
# Look for a T junction. | |
if not (x_mat[row+1][col]=='' or x_mat[row-1][col]==''): | |
x_list.append([row, col]) | |
if (row>0 and row<x_row-1 and col>0 and col<x_col-1): | |
# If the element is not on the outer boundary | |
if (x_mat[row][col+1]!='' and x_mat[row][col-1]!=''): | |
# Check if the elements in next and | |
# previous columns and same row are connected | |
if (x_mat[row+1][col]!='' or x_mat[row-1][col]!=''): | |
# Then check if elements in either the next and | |
# previous row and same column are connected | |
x_list.append([row, col]) | |
elif (x_mat[row+1][col]!='' and x_mat[row-1][col]!=''): | |
# Check if the elements in next and | |
# previous rows and same column are connected | |
if (x_mat[row][col+1]!='' or x_mat[row][col-1]!=''): | |
# Then check if elements in either the next and | |
# previous column and same row are connected | |
x_list.append([row, col]) |
The difference is caused by the last block:
if (row>0 and row<x_row-1 and col>0 and col<x_col-1):
# If the element is not on the outer boundary
if (x_mat[row][col+1]!='' and x_mat[row][col-1]!=''):
# Check if the elements in next and
# previous columns and same row are connected
if (x_mat[row+1][col]!='' or x_mat[row-1][col]!=''):
# Then check if elements in either the next and
# previous row and same column are connected
x_list.append([row, col])
elif (x_mat[row+1][col]!='' and x_mat[row-1][col]!=''):
# Check if the elements in next and
# previous rows and same column are connected
if (x_mat[row][col+1]!='' or x_mat[row][col-1]!=''):
# Then check if elements in either the next and
# previous column and same row are connected
x_list.append([row, col])
The second elif statement had an extra indent!
No comments:
Post a Comment