2024-10-23 21:31:03 +02:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import numpy as np
|
2024-10-26 23:41:41 +02:00
|
|
|
import sys
|
2024-10-23 21:31:03 +02:00
|
|
|
|
|
|
|
class Plot:
|
2024-10-26 23:41:41 +02:00
|
|
|
def __init__(self, n):
|
2024-10-23 21:31:03 +02:00
|
|
|
self.fig, self.ax = plt.subplots()
|
|
|
|
|
2024-10-27 17:13:32 +01:00
|
|
|
# Obsługa kliknięć myszą
|
2024-10-23 21:31:03 +02:00
|
|
|
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_release = self.fig.canvas.mpl_connect('button_release_event', self.on_release)
|
|
|
|
|
2024-10-27 17:13:32 +01:00
|
|
|
# Ustawienia wykresu
|
2024-10-23 21:31:03 +02:00
|
|
|
plt.xlim(0, 800)
|
|
|
|
plt.ylim(0, 600)
|
|
|
|
plt.grid()
|
2024-10-27 17:13:32 +01:00
|
|
|
self.line = [] # linia rysowana przez użytkownika
|
|
|
|
self.controlPoints = [] # Punkty kontrolne krzywej Béziera
|
|
|
|
self.curvePoints = []
|
|
|
|
self.isDrawing = False # Flaga rysowania
|
|
|
|
self.inputMode = 0 # Tryb wejścia: 0 - rysowanie linii, 1 - dodawanie punktów kontrolnych
|
2024-10-26 23:41:41 +02:00
|
|
|
self.n = n
|
2024-10-23 21:31:03 +02:00
|
|
|
|
|
|
|
def onclick(self, event):
|
2024-10-27 17:13:32 +01:00
|
|
|
# Tryb rysowania linii
|
2024-10-23 21:31:03 +02:00
|
|
|
if self.inputMode == 0:
|
|
|
|
self.isDrawing = True
|
2024-10-27 17:13:32 +01:00
|
|
|
# Tryb dodawania punktów kontrolnych
|
|
|
|
elif self.inputMode == 1 and len(self.controlPoints) < self.n:
|
2024-10-23 21:31:03 +02:00
|
|
|
self.controlPoints.append((event.xdata, event.ydata))
|
|
|
|
self.drawPlot()
|
|
|
|
|
|
|
|
def on_mouse_move(self, event):
|
|
|
|
if self.isDrawing:
|
|
|
|
self.line.append((event.xdata, event.ydata))
|
|
|
|
self.drawPlot()
|
|
|
|
|
|
|
|
def on_release(self, event):
|
2024-10-27 17:13:32 +01:00
|
|
|
# Zakończ rysowanie linii
|
2024-10-23 21:31:03 +02:00
|
|
|
if self.inputMode == 0:
|
|
|
|
self.isDrawing = False
|
|
|
|
self.inputMode = 1
|
|
|
|
|
|
|
|
def drawPlot(self):
|
|
|
|
self.ax.clear()
|
2024-10-27 17:13:32 +01:00
|
|
|
|
|
|
|
# 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
|
2024-10-23 21:31:03 +02:00
|
|
|
if len(self.controlPoints) > 0:
|
|
|
|
self.ax.scatter(*zip(*self.controlPoints), color='black', label='Punkty kontrolne')
|
2024-10-27 17:13:32 +01:00
|
|
|
|
|
|
|
# Rysowanie krzywej Béziera
|
2024-10-23 21:31:03 +02:00
|
|
|
if len(self.controlPoints) >= self.n:
|
|
|
|
t_values = np.linspace(0, 1, num=100)
|
|
|
|
bezier_points = np.array([self.de_casteljau(t) for t in t_values])
|
|
|
|
self.ax.plot(bezier_points[:, 0], bezier_points[:, 1], color='blue', label='Krzywa Béziera')
|
|
|
|
|
|
|
|
self.ax.legend()
|
|
|
|
self.ax.set_xlim(0, 800)
|
|
|
|
self.ax.set_ylim(0, 600)
|
|
|
|
self.ax.grid()
|
|
|
|
plt.draw()
|
|
|
|
|
|
|
|
def de_casteljau(self, t):
|
2024-10-27 17:13:32 +01:00
|
|
|
"""Oblicza punkt na krzywej Béziera za pomocą algorytmu de Casteljau dla danego t."""
|
2024-10-23 21:31:03 +02:00
|
|
|
points = np.array(self.controlPoints)
|
|
|
|
n = len(points)
|
|
|
|
for r in range(1, n):
|
|
|
|
for i in range(n - r):
|
|
|
|
points[i] = (1 - t) * points[i] + t * points[i + 1]
|
|
|
|
return points[0]
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
plt.show()
|
|
|
|
|
2024-10-26 23:41:41 +02:00
|
|
|
n = 10
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
n = int(sys.argv[1])
|
2024-10-23 21:31:03 +02:00
|
|
|
if __name__ == "__main__":
|
2024-10-27 17:00:18 +01:00
|
|
|
plot = Plot(n + 1)
|
2024-10-23 21:31:03 +02:00
|
|
|
plot.run()
|
|
|
|
|