Compare commits
2 Commits
master
...
kornus_ana
Author | SHA1 | Date | |
---|---|---|---|
|
3f5fc7ffe8 | ||
|
193e7f2afa |
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
4
.idea/misc.xml
Normal file
4
.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?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>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?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>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/zadanie1Analiza.iml
Normal file
8
.idea/zadanie1Analiza.iml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?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>
|
67
main.py
67
main.py
@ -5,25 +5,39 @@ 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 = []
|
self.line = [] # Linia rysowana przez użytkownika
|
||||||
self.controlPoints = []
|
self.controlPoints = [] # Punkty kontrolne krzywej Béziera
|
||||||
self.curvePoints = []
|
self.approximation_points = [] # Punkty aproksymacji krzywej
|
||||||
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 = 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
|
||||||
if self.inputMode == 1 and len(self.controlPoints) < self.n:
|
# Tryb dodawania punktów kontrolnych
|
||||||
|
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):
|
||||||
@ -32,28 +46,48 @@ 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()
|
||||||
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 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:
|
|
||||||
t_values = np.linspace(0, 1, num=100)
|
# Rysowanie segmentów krzywej Béziera (dodanie etykiety tylko raz)
|
||||||
bezier_points = np.array([self.de_casteljau(t) for t in t_values])
|
first_segment = True
|
||||||
self.ax.plot(bezier_points[:, 0], bezier_points[:, 1], color='blue', label='Krzywa Béziera')
|
for segment in self.approximation_points:
|
||||||
|
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)
|
||||||
self.ax.set_ylim(0, 600)
|
self.ax.set_ylim(0, 600)
|
||||||
self.ax.grid()
|
self.ax.grid()
|
||||||
plt.draw()
|
plt.draw()
|
||||||
|
|
||||||
def de_casteljau(self, t):
|
def de_casteljau(self, points, t):
|
||||||
points = np.array(self.controlPoints)
|
"""Oblicza punkt na krzywej Béziera za pomocą algorytmu de Casteljau dla danego t."""
|
||||||
|
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):
|
||||||
@ -66,4 +100,3 @@ class Plot:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
plot = Plot()
|
plot = Plot()
|
||||||
plot.run()
|
plot.run()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user