From 23fd7a4d7383e6e7d44976f3df8396aaf7ca87de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20S=C3=A1nchez?= Date: Fri, 23 Dec 2022 23:25:54 +0100 Subject: [PATCH] Solve spline generation issue (#1) * Rename module * Update requirements.txt * Remove debugging statement * Solve repeated coordinate issue Solved an issue that caused spline generation to fail when two consecutive equal coordinates were drawn. * Update warning message * Add comments --- requirements.txt | 1 + src/coordinate-canvas/__main__.py | 2 +- .../{utils.py => line_builder.py} | 55 ++++++++++++------- 3 files changed, 37 insertions(+), 21 deletions(-) rename src/coordinate-canvas/{utils.py => line_builder.py} (59%) diff --git a/requirements.txt b/requirements.txt index 6ccafc3..bcb16b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ matplotlib +bidimensional diff --git a/src/coordinate-canvas/__main__.py b/src/coordinate-canvas/__main__.py index 2bd01e6..6460a32 100644 --- a/src/coordinate-canvas/__main__.py +++ b/src/coordinate-canvas/__main__.py @@ -22,7 +22,7 @@ from bidimensional.functions import Spline from .config import CONFIG -from .utils import LineBuilder +from .line_builder import LineBuilder def validate_input(message: str) -> float: diff --git a/src/coordinate-canvas/utils.py b/src/coordinate-canvas/line_builder.py similarity index 59% rename from src/coordinate-canvas/utils.py rename to src/coordinate-canvas/line_builder.py index c169b1f..9c1190d 100644 --- a/src/coordinate-canvas/utils.py +++ b/src/coordinate-canvas/line_builder.py @@ -56,38 +56,53 @@ def __call__(self, event: matplotlib.backend_bases.MouseEvent) -> None: event (matplotlib.backend_bases.MouseEvent): The click event. """ - print(type(event)) + # Correct axes validation: + if event.inaxes != self.line.axes: return - self.x.append(event.xdata) - self.y.append(event.ydata) + if len(self.x) > 0: # Prevents single-coordinate spline error. - if len(self.x) > 1: - sp = Spline([ - Coordinate(x_, y_) - for x_, y_ in zip(self.x, self.y) - ], gen_step=min(self.dimensions) / 100) + if event.xdata != self.x[-1] or \ + event.ydata != self.y[-1]: # Ignores repeated coordinates. - x = [x_ for x_, _ in sp.positions] - y = [y_ for _, y_ in sp.positions] + self.x.append(event.xdata) + self.y.append(event.ydata) - sp.plot_input( - CONFIG.get("input").get("shape"), - ms=CONFIG.get("input").get("size"), - alpha=CONFIG.get("input").get("alpha"), - color=f"dark{self.color}", - ) + if len(self.x) > 1: + sp = Spline([ + Coordinate(x_, y_) + for x_, y_ in zip(self.x, self.y) + ], gen_step=min(self.dimensions) / 100) + + x = [x_ for x_, _ in sp.positions] + y = [y_ for _, y_ in sp.positions] + + sp.plot_input( + CONFIG.get("input").get("shape"), + ms=CONFIG.get("input").get("size"), + alpha=CONFIG.get("input").get("alpha"), + color=f"dark{self.color}", + ) + + self.line.set_data(x, y) + self.line.figure.canvas.draw() + + else: + print(f"WARNING: Repetated coordinate {event.xdata}, " + + f"{event.ydata}. Skipping...") else: - x, y = self.x, self.y self.ax.plot( - x, y, + event.xdata, event.ydata, CONFIG.get("input").get("shape"), lw=CONFIG.get("input").get("size"), alpha=CONFIG.get("input").get("alpha"), color=f"dark{self.color}" ) - self.line.set_data(x, y) - self.line.figure.canvas.draw() + self.x.append(event.xdata) + self.y.append(event.ydata) + + self.line.set_data(self.x, self.y) + self.line.figure.canvas.draw()