Я хочу создать функционал, который поможет объединить любые два круга с помощью линии. Функция должна быть в состоянии два сделать соединение между двумя кругами через линию, выбрав два круга или любым другим способом. Любая помощь будет оценена ...
import random
from PyQt5 import QtCore
from PyQt5.QtCore import QPoint, QRect, QSize, Qt, QLineF
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QAction, QWidget, QToolBar
from PyQt5.QtGui import QPainter, QVector2D, QPen, QIcon
import sys
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.drag_position = QPoint()
self.circles = []
self.current_circle = None
button = QPushButton("Add", self)
button.clicked.connect(self.on_clicked)
self.setMinimumSize(400, 400)
def on_clicked(self):
pos = QPoint(random.randrange(self.width()), random.randrange(self.height()))
self.circles.append(QRect(pos, QSize(100, 100)))
self.update()
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
for circle in self.circles:
painter.drawEllipse(circle)
def mousePressEvent(self, event):
for circle in self.circles:
line = QLineF(circle.center(), event.pos())
if line.length() < circle.width() / 2:
self.current_circle = circle
self.drag_position = event.pos()
break
def mouseMoveEvent(self, event):
if self.current_circle is not None:
self.current_circle.translate(event.pos() - self.drag_position)
self.drag_position = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.current_circle = None
if __name__ == "__main__":
app = QApplication(sys.argv)
Circle = Window()
Circle.show()
sys.exit(app.exec_())