Compare commits
No commits in common. "3f5fc7ffe8bbdc264510118849c4e9140ac2eeb3" and "5166a32ad306afc00a8993fb42b0e7bfe6a41fb5" have entirely different histories.
3f5fc7ffe8
...
5166a32ad3
3
.idea/.gitignore
vendored
3
.idea/.gitignore
vendored
@ -1,3 +0,0 @@
|
|||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
@ -1,6 +0,0 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
|
||||||
<settings>
|
|
||||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
|
||||||
<version value="1.0" />
|
|
||||||
</settings>
|
|
||||||
</component>
|
|
@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
|
|
||||||
</project>
|
|
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/zadanie1Analiza.iml" filepath="$PROJECT_DIR$/.idea/zadanie1Analiza.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module type="PYTHON_MODULE" version="4">
|
|
||||||
<component name="NewModuleRootManager">
|
|
||||||
<content url="file://$MODULE_DIR$" />
|
|
||||||
<orderEntry type="inheritedJdk" />
|
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
|
||||||
</component>
|
|
||||||
</module>
|
|
65
main.py
65
main.py
@ -5,39 +5,25 @@ class Plot:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
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)
|
||||||
self.cid_key = self.fig.canvas.mpl_connect('key_press_event', self.on_key_press) # Obsługa klawiatury
|
|
||||||
|
|
||||||
# Ustawienia wykresu
|
|
||||||
plt.xlim(0, 800)
|
plt.xlim(0, 800)
|
||||||
plt.ylim(0, 600)
|
plt.ylim(0, 600)
|
||||||
plt.grid()
|
plt.grid()
|
||||||
self.line = [] # Linia rysowana przez użytkownika
|
self.line = []
|
||||||
self.controlPoints = [] # Punkty kontrolne krzywej Béziera
|
self.controlPoints = []
|
||||||
self.approximation_points = [] # Punkty aproksymacji krzywej
|
self.curvePoints = []
|
||||||
self.isDrawing = False # Flaga rysowania
|
self.isDrawing = False
|
||||||
self.inputMode = 0 # Tryb wejścia: 0 - rysowanie linii, 1 - dodawanie punktów kontrolnych
|
self.inputMode = 0
|
||||||
|
self.n = 15
|
||||||
def on_key_press(self, event):
|
|
||||||
"""Przełączanie trybu rysowania i dodawania punktów kontrolnych przy użyciu klawisza 'm'."""
|
|
||||||
if event.key == 'm':
|
|
||||||
self.inputMode = (self.inputMode + 1) % 2
|
|
||||||
mode = "Rysowanie linii" if self.inputMode == 0 else "Dodawanie punktów kontrolnych"
|
|
||||||
print(f"Tryb zmieniony na: {mode}")
|
|
||||||
|
|
||||||
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
|
||||||
# Tryb dodawania punktów kontrolnych
|
if self.inputMode == 1 and len(self.controlPoints) < self.n:
|
||||||
elif self.inputMode == 1 and len(self.controlPoints) < 15:
|
|
||||||
self.controlPoints.append((event.xdata, event.ydata))
|
self.controlPoints.append((event.xdata, event.ydata))
|
||||||
# Automatyczna generacja segmentów krzywej Béziera po dodaniu nowych punktów
|
|
||||||
if len(self.controlPoints) >= 4:
|
|
||||||
self.generate_bezier_segments()
|
|
||||||
self.drawPlot()
|
self.drawPlot()
|
||||||
|
|
||||||
def on_mouse_move(self, event):
|
def on_mouse_move(self, event):
|
||||||
@ -46,38 +32,19 @@ 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
|
||||||
def generate_bezier_segments(self):
|
|
||||||
"""Tworzy segmenty krzywej Béziera na podstawie punktów kontrolnych."""
|
|
||||||
self.approximation_points = []
|
|
||||||
# Dzielenie punktów kontrolnych na segmenty długości 4 (dla krzywej Béziera stopnia 3)
|
|
||||||
for i in range(0, len(self.controlPoints) - 3, 3):
|
|
||||||
segment = self.controlPoints[i:i + 4]
|
|
||||||
t_values = np.linspace(0, 1, num=100)
|
|
||||||
bezier_points = np.array([self.de_casteljau(segment, t) for t in t_values])
|
|
||||||
self.approximation_points.append(bezier_points)
|
|
||||||
|
|
||||||
def drawPlot(self):
|
def drawPlot(self):
|
||||||
self.ax.clear()
|
self.ax.clear()
|
||||||
# Rysowanie linii narysowanej przez użytkownika
|
self.ax.plot(*zip(*self.line), color='red', label='linia')
|
||||||
if len(self.line) > 1:
|
|
||||||
self.ax.plot(*zip(*self.line), color='red', label='Linia narysowana')
|
|
||||||
|
|
||||||
# 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')
|
||||||
|
if len(self.controlPoints) >= self.n:
|
||||||
# Rysowanie segmentów krzywej Béziera (dodanie etykiety tylko raz)
|
t_values = np.linspace(0, 1, num=100)
|
||||||
first_segment = True
|
bezier_points = np.array([self.de_casteljau(t) for t in t_values])
|
||||||
for segment in self.approximation_points:
|
self.ax.plot(bezier_points[:, 0], bezier_points[:, 1], color='blue', label='Krzywa Béziera')
|
||||||
if first_segment:
|
|
||||||
self.ax.plot(segment[:, 0], segment[:, 1], color='blue', label='Krzywa Béziera')
|
|
||||||
first_segment = False
|
|
||||||
else:
|
|
||||||
self.ax.plot(segment[:, 0], segment[:, 1], color='blue')
|
|
||||||
|
|
||||||
self.ax.legend()
|
self.ax.legend()
|
||||||
self.ax.set_xlim(0, 800)
|
self.ax.set_xlim(0, 800)
|
||||||
@ -85,9 +52,8 @@ class Plot:
|
|||||||
self.ax.grid()
|
self.ax.grid()
|
||||||
plt.draw()
|
plt.draw()
|
||||||
|
|
||||||
def de_casteljau(self, points, 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(points)
|
|
||||||
n = len(points)
|
n = len(points)
|
||||||
for r in range(1, n):
|
for r in range(1, n):
|
||||||
for i in range(n - r):
|
for i in range(n - r):
|
||||||
@ -100,3 +66,4 @@ class Plot:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
plot = Plot()
|
plot = Plot()
|
||||||
plot.run()
|
plot.run()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user