komentarze i zmiany Bartka

This commit is contained in:
tafit0902 2024-10-27 17:13:32 +01:00
parent 646da5f1a9
commit 73f4a1d470

27
main.py
View File

@ -6,24 +6,28 @@ class Plot:
def __init__(self, n): def __init__(self, n):
self.fig, self.ax = plt.subplots() self.fig, self.ax = plt.subplots()
# Obsługa kliknięć myszą
self.cid_click = self.fig.canvas.mpl_connect('button_press_event', self.onclick) self.cid_click = self.fig.canvas.mpl_connect('button_press_event', self.onclick)
self.cid_motion = self.fig.canvas.mpl_connect('motion_notify_event', self.on_mouse_move) self.cid_motion = self.fig.canvas.mpl_connect('motion_notify_event', self.on_mouse_move)
self.cid_release = self.fig.canvas.mpl_connect('button_release_event', self.on_release) self.cid_release = self.fig.canvas.mpl_connect('button_release_event', self.on_release)
# Ustawienia wykresu
plt.xlim(0, 800) plt.xlim(0, 800)
plt.ylim(0, 600) plt.ylim(0, 600)
plt.grid() plt.grid()
self.line = [] self.line = [] # linia rysowana przez użytkownika
self.controlPoints = [] self.controlPoints = [] # Punkty kontrolne krzywej Béziera
self.curvePoints = [] self.curvePoints = []
self.isDrawing = False self.isDrawing = False # Flaga rysowania
self.inputMode = 0 self.inputMode = 0 # Tryb wejścia: 0 - rysowanie linii, 1 - dodawanie punktów kontrolnych
self.n = n self.n = n
def onclick(self, event): def onclick(self, event):
# Tryb rysowania linii
if self.inputMode == 0: if self.inputMode == 0:
self.isDrawing = True self.isDrawing = True
if self.inputMode == 1 and len(self.controlPoints) < self.n: # Tryb dodawania punktów kontrolnych
elif self.inputMode == 1 and len(self.controlPoints) < self.n:
self.controlPoints.append((event.xdata, event.ydata)) self.controlPoints.append((event.xdata, event.ydata))
self.drawPlot() self.drawPlot()
@ -33,15 +37,23 @@ class Plot:
self.drawPlot() self.drawPlot()
def on_release(self, event): def on_release(self, event):
# Zakończ rysowanie linii
if self.inputMode == 0: if self.inputMode == 0:
self.isDrawing = False self.isDrawing = False
self.inputMode = 1 self.inputMode = 1
def drawPlot(self): def drawPlot(self):
self.ax.clear() self.ax.clear()
self.ax.plot(*zip(*self.line), color='red', label='linia')
# Rysowanie linii narysowanej przez użytkownika
if len(self.line) > 1:
self.ax.plot(*zip(*self.line), color='red', label='linia')
# Rysowanie punktów kontrolnych
if len(self.controlPoints) > 0: if len(self.controlPoints) > 0:
self.ax.scatter(*zip(*self.controlPoints), color='black', label='Punkty kontrolne') self.ax.scatter(*zip(*self.controlPoints), color='black', label='Punkty kontrolne')
# Rysowanie krzywej Béziera
if len(self.controlPoints) >= self.n: if len(self.controlPoints) >= self.n:
t_values = np.linspace(0, 1, num=100) t_values = np.linspace(0, 1, num=100)
bezier_points = np.array([self.de_casteljau(t) for t in t_values]) bezier_points = np.array([self.de_casteljau(t) for t in t_values])
@ -54,6 +66,7 @@ class Plot:
plt.draw() plt.draw()
def de_casteljau(self, t): def de_casteljau(self, t):
"""Oblicza punkt na krzywej Béziera za pomocą algorytmu de Casteljau dla danego t."""
points = np.array(self.controlPoints) points = np.array(self.controlPoints)
n = len(points) n = len(points)
for r in range(1, n): for r in range(1, n):