Skip to content

Commit

Permalink
Solve spline generation issue (#1)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
erlete authored Dec 23, 2022
1 parent 8853f4c commit 23fd7a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
matplotlib
bidimensional
2 changes: 1 addition & 1 deletion src/coordinate-canvas/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 23fd7a4

Please sign in to comment.