From e2e3f8d8ef12c82b12a6d5a28c9f2d625f217ef0 Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Sat, 18 Sep 2021 15:17:31 +0430 Subject: [PATCH 1/7] Horizonal route test. --- tests/test_canvas.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 37b3b8d..6de97dc 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -240,18 +240,17 @@ def test_canvas_drawarrowtext(): def test_canvas_route_horizontal(): c = Canvas() - c.route(2, 3, 7, 8) - c.route(2, 4, 7, 8) + c.route(2, 3, 7, 8, direction='h') assert eqdia(str(c), ''' ............ . . . . . . - . -----+ . - . | | . - . | | . - . | | . - . | v . - . +---> . + . --+ . + . | . + . | . + . | . + . | . + . +-> . ............ ''') From fcdee0ddb6b8d7a7a113a4517ee809ce1d405f5d Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Sun, 19 Sep 2021 20:51:09 +0430 Subject: [PATCH 2/7] Horizontal route has been implemented. --- adia/canvas.py | 33 +++++++++++---------------------- tests/test_canvas.py | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/adia/canvas.py b/adia/canvas.py index c92bda6..963bc29 100644 --- a/adia/canvas.py +++ b/adia/canvas.py @@ -1,3 +1,5 @@ +import math + from .mutablestring import MutableString @@ -143,26 +145,13 @@ def draw_bottomarrow(self, col, row, length, **kw): self.draw_vline(col, row, length - 1, **kw) self.set_char(col, row + length - 1, 'v') - def route(self, col1, row1, col2, row2): - overlap = False - - try: - for i in self._backend[row1][col1:col2 + 1]: - if i == '-' or '|': - overlap = True - break - - except IndexError: - overlap = False - - if not overlap: - self.draw_hline(col1, row1, col2 - col1) - self.draw_vline(col2, row1 + 1, row2 - row1 - 2) - self.set_char(col2, row2 - 1, 'v') - self.set_char(col2, row1, '+') - - else: - self.draw_vline(col1, row1, (row2 - row1)) - self.set_char(col1, row2, '+') - self.draw_hline(col1 + 1, row2, col2 - col1 - 1) + def route(self, col1, row1, col2, row2, direction): + if direction == 'horizontal': + firsthalf = (math.ceil((col2 - col1) / 2) - 1) + secondhalf = col2 - (col1 + firsthalf) - 1 + self.draw_hline(col1, row1, firsthalf) + self.set_char(col1 + firsthalf, row1, '+') + self.draw_vline(col1 + firsthalf, row1 + 1, row2 - row1 - 1) + self.set_char(col1 + firsthalf, row2, '+') + self.draw_hline(col1 + firsthalf + 1, row2, secondhalf) self.set_char(col2 - 1, row2, '>') diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 6de97dc..fba180d 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -240,17 +240,17 @@ def test_canvas_drawarrowtext(): def test_canvas_route_horizontal(): c = Canvas() - c.route(2, 3, 7, 8, direction='h') + c.route(2, 3, 7, 8, 'horizontal') assert eqdia(str(c), ''' - ............ - . . - . . - . . - . --+ . - . | . - . | . - . | . - . | . - . +-> . - ............ + ........... + . . + . . + . . + . --+ . + . | . + . | . + . | . + . | . + . +-> . + ........... ''') From 98e8cf848f4c3a3e43141db0150798393da66bad Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Tue, 21 Sep 2021 06:59:29 +0430 Subject: [PATCH 3/7] Horizontal route from bottom to top. --- adia/canvas.py | 5 ++++- tests/test_canvas.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/adia/canvas.py b/adia/canvas.py index 963bc29..50e714b 100644 --- a/adia/canvas.py +++ b/adia/canvas.py @@ -147,11 +147,14 @@ def draw_bottomarrow(self, col, row, length, **kw): def route(self, col1, row1, col2, row2, direction): if direction == 'horizontal': + firsthalf = (math.ceil((col2 - col1) / 2) - 1) secondhalf = col2 - (col1 + firsthalf) - 1 + minrow = min(row1, row2) + self.draw_hline(col1, row1, firsthalf) self.set_char(col1 + firsthalf, row1, '+') - self.draw_vline(col1 + firsthalf, row1 + 1, row2 - row1 - 1) + self.draw_vline(col1 + firsthalf, minrow + 1, abs(row2 - row1) - 1) self.set_char(col1 + firsthalf, row2, '+') self.draw_hline(col1 + firsthalf + 1, row2, secondhalf) self.set_char(col2 - 1, row2, '>') diff --git a/tests/test_canvas.py b/tests/test_canvas.py index fba180d..a6c3479 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -254,3 +254,18 @@ def test_canvas_route_horizontal(): . +-> . ........... ''') + + c = Canvas() + c.route(0, 7, 5, 1, 'horizontal') + assert eqdia(str(c), ''' + ......... + . . + . +-> . + . | . + . | . + . | . + . | . + . | . + . --+ . + ......... + ''') From ef560021166578a825cda35e032e54a846090944 Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Tue, 21 Sep 2021 07:29:23 +0430 Subject: [PATCH 4/7] Straight horizontal route. --- adia/canvas.py | 9 +++++---- tests/test_canvas.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/adia/canvas.py b/adia/canvas.py index 50e714b..bea66e2 100644 --- a/adia/canvas.py +++ b/adia/canvas.py @@ -152,9 +152,10 @@ def route(self, col1, row1, col2, row2, direction): secondhalf = col2 - (col1 + firsthalf) - 1 minrow = min(row1, row2) - self.draw_hline(col1, row1, firsthalf) - self.set_char(col1 + firsthalf, row1, '+') - self.draw_vline(col1 + firsthalf, minrow + 1, abs(row2 - row1) - 1) - self.set_char(col1 + firsthalf, row2, '+') + self.draw_hline(col1, row1, firsthalf + 1) + self.draw_vline(col1 + firsthalf, minrow, abs(row2 - row1)) + if row1 != row2: + self.set_char(col1 + firsthalf, row1, '+') + self.set_char(col1 + firsthalf, row2, '+') self.draw_hline(col1 + firsthalf + 1, row2, secondhalf) self.set_char(col2 - 1, row2, '>') diff --git a/tests/test_canvas.py b/tests/test_canvas.py index a6c3479..00866ce 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -269,3 +269,13 @@ def test_canvas_route_horizontal(): . --+ . ......... ''') + + c = Canvas() + c.route(0, 2, 7, 2, 'horizontal') + assert eqdia(str(c), ''' + ........... + . . + . . + . ------> . + ........... + ''') From 2f5bdf9fe648adee7e8bce1c5e4f7e3fe3779e40 Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Thu, 4 Nov 2021 11:30:29 +0330 Subject: [PATCH 5/7] Basic routing ft pylover. --- adia/canvas.py | 65 ++++++++++++++++++++++++++++++++++---------- tests/test_canvas.py | 29 +++++++++++++++++++- 2 files changed, 79 insertions(+), 15 deletions(-) diff --git a/adia/canvas.py b/adia/canvas.py index bea66e2..1fd624f 100644 --- a/adia/canvas.py +++ b/adia/canvas.py @@ -1,5 +1,3 @@ -import math - from .mutablestring import MutableString @@ -50,6 +48,15 @@ def extendtop(self, addrows): def __str__(self): return '\n'.join(str(line) for line in self._backend) + '\n' + def get_char(self, col, row): + if col >= self.cols: + self.extendright((col + 1) - self.cols) + + if row >= self.rows: + self.extendbottom((row + 1) - self.rows) + + return self._backend[row][col] + def set_char(self, col, row, char): if col >= self.cols: self.extendright((col + 1) - self.cols) @@ -145,17 +152,47 @@ def draw_bottomarrow(self, col, row, length, **kw): self.draw_vline(col, row, length - 1, **kw) self.set_char(col, row + length - 1, 'v') - def route(self, col1, row1, col2, row2, direction): - if direction == 'horizontal': + def route(self, startcol, startrow, endcol, endrow): + path = [(startcol, startrow)] + + def can(col, row): + return self.get_char(col, row) == ' ' + + def try_(col, row): + if can(col, row): + path.append((col, row)) + return True + + return False + + def walk(): + col, row = path[-1] + + if col == endcol and row == endrow: + return + + if col == endcol: + intrested_hdir = 0 + else: + intrested_hdir = 1 if col < endcol else -1 + + if row == endrow: + intrested_vdir = 0 + else: + intrested_vdir = 1 if row < endrow else -1 + + try: + # Try horizontal + if intrested_hdir and try_(col + intrested_hdir, row): + return + + # Try vertical + if intrested_vdir and try_(col, row + intrested_vdir): + return - firsthalf = (math.ceil((col2 - col1) / 2) - 1) - secondhalf = col2 - (col1 + firsthalf) - 1 - minrow = min(row1, row2) + finally: + walk() - self.draw_hline(col1, row1, firsthalf + 1) - self.draw_vline(col1 + firsthalf, minrow, abs(row2 - row1)) - if row1 != row2: - self.set_char(col1 + firsthalf, row1, '+') - self.set_char(col1 + firsthalf, row2, '+') - self.draw_hline(col1 + firsthalf + 1, row2, secondhalf) - self.set_char(col2 - 1, row2, '>') + walk() + for col, row in path: + self.set_char(col, row, '*') diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 00866ce..3badbca 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -238,9 +238,17 @@ def test_canvas_drawarrowtext(): ''') +def test_canvas_get_char(): + c = Canvas() + assert c.get_char(0, 0) == ' ' + assert c.get_char(1, 1) == ' ' + c.set_char(1, 1, 'x') + assert c.get_char(1, 1) == 'x' + + def test_canvas_route_horizontal(): c = Canvas() - c.route(2, 3, 7, 8, 'horizontal') + c.route(2, 3, 7, 8) assert eqdia(str(c), ''' ........... . . @@ -255,6 +263,7 @@ def test_canvas_route_horizontal(): ........... ''') + """ c = Canvas() c.route(0, 7, 5, 1, 'horizontal') assert eqdia(str(c), ''' @@ -279,3 +288,21 @@ def test_canvas_route_horizontal(): . ------> . ........... ''') + + c = Canvas() + c.route(2, 3, 7, 8, 'horizontal') + c.route(0, 5, 10, 7, 'horizontal') + assert eqdia(str(c), ''' + ............ + . . + . . + . +---+ . + . |--+| . + . | || . + . -+ || . + . || . + . |+-> . + . +-> . + ............ + ''') + """ From 53911d71925fcfd6b5b93737825f4ec04d74c654 Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Thu, 4 Nov 2021 13:01:15 +0330 Subject: [PATCH 6/7] Damn, everything is fucked up. --- adia/canvas.py | 31 +++++++++++++++++++----- tests/test_canvas.py | 57 +++++++------------------------------------- 2 files changed, 33 insertions(+), 55 deletions(-) diff --git a/adia/canvas.py b/adia/canvas.py index 1fd624f..c36a04b 100644 --- a/adia/canvas.py +++ b/adia/canvas.py @@ -156,7 +156,8 @@ def route(self, startcol, startrow, endcol, endrow): path = [(startcol, startrow)] def can(col, row): - return self.get_char(col, row) == ' ' + return self.get_char(col, row) == ' ' and \ + (col, row) not in path def try_(col, row): if can(col, row): @@ -167,6 +168,7 @@ def try_(col, row): def walk(): col, row = path[-1] + done = False if col == endcol and row == endrow: return @@ -182,16 +184,33 @@ def walk(): intrested_vdir = 1 if row < endrow else -1 try: + # from pudb import set_trace; col > 16 and set_trace() # Try horizontal - if intrested_hdir and try_(col + intrested_hdir, row): - return + if intrested_hdir: + if try_(col + intrested_hdir, row): + return + elif try_(col, row + intrested_vdir): + return + elif try_(col, row - 1): + return + elif try_(col, row + 1): + return # Try vertical - if intrested_vdir and try_(col, row + intrested_vdir): - return + elif intrested_vdir: + if try_(col, row + intrested_vdir): + return + elif try_(col - 1, row): + return + elif try_(col + 1, row): + return + + else: + done = True finally: - walk() + if not done: + walk() walk() for col, row in path: diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 3badbca..66e789e 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -248,61 +248,20 @@ def test_canvas_get_char(): def test_canvas_route_horizontal(): c = Canvas() - c.route(2, 3, 7, 8) + c.draw_box(2, 2, 5, 5) + c.draw_box(10, 2, 5, 5) + c.draw_box(18, 2, 5, 5) + c.route(6, 4, 18, 4) assert eqdia(str(c), ''' ........... . . . . . . - . --+ . - . | . - . | . - . | . - . | . - . +-> . - ........... - ''') - - """ - c = Canvas() - c.route(0, 7, 5, 1, 'horizontal') - assert eqdia(str(c), ''' - ......... - . . - . +-> . - . | . - . | . - . | . - . | . - . | . - . --+ . - ......... - ''') - - c = Canvas() - c.route(0, 2, 7, 2, 'horizontal') - assert eqdia(str(c), ''' - ........... . . . . - . ------> . + . . + . . + . . + . . ........... ''') - - c = Canvas() - c.route(2, 3, 7, 8, 'horizontal') - c.route(0, 5, 10, 7, 'horizontal') - assert eqdia(str(c), ''' - ............ - . . - . . - . +---+ . - . |--+| . - . | || . - . -+ || . - . || . - . |+-> . - . +-> . - ............ - ''') - """ From 418bef2c9731542cd0a6e8101a8fc9cb5d950df7 Mon Sep 17 00:00:00 2001 From: Saeed Mahmoodi Date: Sun, 7 Nov 2021 11:16:32 +0330 Subject: [PATCH 7/7] Basic routing without overlap with single char. --- adia/canvas.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adia/canvas.py b/adia/canvas.py index c36a04b..8d31f7f 100644 --- a/adia/canvas.py +++ b/adia/canvas.py @@ -170,10 +170,10 @@ def walk(): col, row = path[-1] done = False - if col == endcol and row == endrow: + if col == endcol - 1 and row == endrow: return - if col == endcol: + if col == endcol - 1: intrested_hdir = 0 else: intrested_hdir = 1 if col < endcol else -1 @@ -213,5 +213,6 @@ def walk(): walk() walk() + path.append((endcol, endrow)) for col, row in path: self.set_char(col, row, '*')