-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineSegments.py
159 lines (132 loc) · 4.36 KB
/
lineSegments.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'''
Description: Detecting squares from a 2D matrix.
Working status: Final working code for detecting line segments in 2-D matrix.
'''
# and start_node != (row,col))
import sys
# import pandas as pd
coordinates = []
#pts = []
lines = []
extra, sides = 0,0
matrix = []
checks = []
# filename = sys.argv[1] # matrix.txt
'''
reading matrix from a file.
'''
def readInput():
raw_mat = []
f = open(filename)
for line in f.readlines():
raw_mat.append(line.split())
f.close()
for row in raw_mat:
matrix.append([i for i in row if i.isdigit()])
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j]=int(matrix[i][j])
'''
initialising the checks matrix
'''
def initCheck():
global checks
checks = [[0 for x in range(cols)] for y in range(rows)]
'''
removing duplicates from list and also preserving the order
'''
def removeDuplicates(myList):
newlist = []
for element in myList:
if element not in newlist:
newlist.append(element)
return newlist
'''
Getting number of ones surrounding a point in a matrix.
'''
def numOnes(row,col):
num_ones = 0
if row + 1 < rows and matrix[row + 1][col] == 1:
num_ones = num_ones + 1
if row - 1 >= 0 and matrix[row - 1][col] == 1:
num_ones = num_ones + 1
if col + 1 < cols and matrix[row][col + 1] == 1:
num_ones = num_ones + 1
if col - 1 >= 0 and matrix[row][col - 1] == 1:
num_ones = num_ones + 1
# print num_ones
return num_ones
'''
movement of pointer in a direction at particular coordinate
'''
def motion(next_row,next_col,prev_node,row,col,direction,count,prev_dir,start_node,corner):
global sides
if (extra == 1):
return
count = count+1
next_dir = direction
next_node = (next_row, next_col)
# print pd.DataFrame(checks)
# print "====================="
global coordinates
if next_node != prev_node:
checks[row][col] = checks[row][col] + 1
if (prev_dir != direction or numOnes(row,col)>=3):
sides = sides + 1
coordinates.append((row,col))
prev_corner = corner
corner = (row,col)
# print (prev_corner,corner)
if(prev_corner != corner):
lines.append(tuple((prev_corner,corner)))
# pts.append(tuple((row,col)))
decision(next_row, next_col, next_dir, (row, col), start_node,count,corner)
'''
making decision to go in a direction using various creteria
'''
def decision(row, col, prev_dir, prev_node,start_node,count,corner):
# [row][col] current location
global extra # breaking recursion.
if (start_node == prev_node and count>1):
extra = 1
return extra
if(extra == 1):
return extra
if row + 1 < rows and matrix[row + 1][col] == 1 and checks[row + 1][col]<=(numOnes(row+1,col)-1): # [row+1][col] down
motion(row + 1 , col, prev_node, row, col, 'd',count,prev_dir,start_node,corner)
if row - 1 >= 0 and matrix[row - 1][col] == 1 and checks[row - 1][col]<=(numOnes(row-1,col)-1): # [row-1][col] up
motion(row - 1, col, prev_node, row, col, 'u',count,prev_dir,start_node,corner)
if col + 1 < cols and matrix[row][col + 1] == 1 and checks[row][col+1]<=(numOnes(row,col+1)-1): # [row][col+1] right
motion(row, col+1, prev_node, row, col, 'r',count,prev_dir,start_node,corner)
if col - 1 >= 0 and matrix[row][col - 1] == 1 and checks[row][col-1]<=(numOnes(row,col-1)-1): # [row][col-1] left
motion(row , col-1 , prev_node, row, col, 'l',count,prev_dir,start_node,corner)
'''
Iterating through matrix.
'''
def iterMatrix():
row, col, count = 0,0,0
for row in range(0,rows):
for col in range(0,cols):
if (matrix[row][col] == 0):
continue
elif (matrix[row][col] == 1):
start_node = (row, col)
corner = start_node
decision(row, col, '', (row, col - 1), start_node,0,corner)
break
if (count == 1):
break
# if __name__ == "__main__":
def main(Filename):
global filename
filename = Filename
readInput()
global rows,cols
rows = len(matrix) # number of rows of matrix
cols = len(matrix[0]) # number of columns of matrix
initCheck()
iterMatrix()
# print [line for line in set(lines)]
unique_lines = removeDuplicates(lines)
return [line for line in unique_lines]
# main(sys.argv[1])