commit 2fd7381d4f37ec002bf50a1d145aae53424e2e16
parent a40b457e2fa3637ce60a37e91c438cdf13301bfb
Author: bkopf <vetlehaf@stud.ntnu.no>
Date: Sat, 17 Nov 2018 16:10:11 +0100
Make Figure.rectangle() look for rect chains recursively
Diffstat:
M | a2svg.py | | | 77 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
M | ascii.txt | | | 10 | ++++++++++ |
2 files changed, 70 insertions(+), 17 deletions(-)
diff --git a/a2svg.py b/a2svg.py
@@ -4,6 +4,7 @@
class Figure:
def __init__(self, ascii_text):
self._elements = []
+ self._process_queue = []
self._ascii_text = [line.strip('\n') for line in ascii_text]
self._height = len(ascii_text)
self._width = max([len(line) for line in ascii_text])
@@ -41,11 +42,14 @@ class Figure:
def process_char(self, y, x):
symbol = self._figarray[y][x]
if symbol == '+':
- new_element = Joint((y, x))
- rect = self.rectangle(y, x)
- if rect:
- rect.extract_text(self)
- self._elements.append(rect)
+ rects = self.rectangle(y, x)
+ if rects:
+ # TODO: Refactor this text extraction
+ for r in rects:
+ r.extract_text(self)
+ self._elements += rects
+ else:
+ new_element = Joint((y, x))
if symbol == '-':
new_element = Line((y, x))
if symbol == '|':
@@ -64,30 +68,42 @@ class Figure:
"""
- Checks if a joint belongs to a rectangle.
- If it does, the rectangle is added as an element
+ Checks if a joint belongs to a rectangle (and if there are subrectangles)
+ If it does, the rectangles are added as an element, and all associated characters
+ are registered as processed.
"""
def rectangle(self, y_start, x_start):
+ # Temporary list of processed chars. Must dismiss if this is not a rectangle
+ processed = [[y_start, x_start]]
if x_start + 1 >= self._width:
return None
- y = y_start + 1
+
+ # TODO: Refactor this into helper method 'find_y_start'?
# 1. Find the vertical stop of the rectangle, if any
+ y = y_start + 1
y_stop = None
while not y_stop and y < self._height:
cell = self._figarray[y][x_start]
if cell == '|':
+ processed.append([y, x_start])
+ y += 1
+ elif cell == '+':
+ # If '-' to the right, this should be the y_stop
+ if self._figarray[y][x_start+1] == '-':
+ y_stop = y
+ processed.append([y, x_start])
+ break
+ # If not, the '+' might be a connection to a line on the left
+ self._process_queue.append([y, x_start])
y += 1
- continue
- elif cell == '+' and self._figarray[y][x_start+1]:
- y_stop = y
- break
else:
break
if not y_stop:
return None
- # 2. Find the horisontal stop, if any
+ # TODO: Clean up
+ # 2. Find the horizontal stop, if any
x_stop = None
x = x_start + 1
while not x_stop and x < self._width:
@@ -103,19 +119,46 @@ class Figure:
break
if x_stop:
break
- else:
- continue
+ # If x_stop is *not* defined, we probably found an x where both 'y's are connected to lines,
+ # so we need to continue and add them
if upper_cell != '-' and upper_cell != '+':
break
if lower_cell != '-' and lower_cell != '+':
break
+ if upper_cell == '+':
+ self._process_queue.append([y_start, x])
+ if lower_cell == '+':
+ self._process_queue.append([y_stop, x])
x += 1
if not x_stop:
return None
- # By this point, we know we have a rectangle
- return Rectangle((x_start, y_start), (x_stop, y_stop))
+ # By this point, we know we have a rectangle, so we can register the
+ # entire section as processed
+ # TODO : Make readable
+ for y in range(y_start, y_stop):
+ for x in range(x_start, x_stop+1):
+ if [y, x] not in self._process_queue:
+ self._processed[y][x] = True
+
+ # TESTING
+ for row in self._processed:
+ for cell in row:
+ print("{} ".format('p' if cell else ' '), end="")
+ print()
+
+ rect_list = [Rectangle((x_start, y_start), (x_stop, y_stop))]
+ # Check if we're dealing with a rectangle chain
+ sub_rects = self.rectangle(y_stop, x_start)
+ if sub_rects:
+ rect_list += sub_rects
+ # If not, we can register the bottom line as processed
+ else:
+ for x in range(x_start, x_stop+1):
+ self._processed[y_stop][x] = True
+
+ return rect_list
"""Return SVG text of the entire figure"""
diff --git a/ascii.txt b/ascii.txt
@@ -28,3 +28,13 @@
| |
| |
+----------------------+
+
+
+
++-----------------------+
+| SEC 1 |
++-----------------------+
+| SEC 2 |
++-----------------------+
+| SEC 3 |
++-----------------------+