59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
from PyQt5.QtGui import QPen, QBrush
|
|
from PyQt5.QtCore import Qt, QPointF
|
|
from PyQt5.QtWidgets import (
|
|
QWidget,
|
|
QGraphicsView,
|
|
QGraphicsScene,
|
|
QGraphicsEllipseItem,
|
|
QGraphicsLineItem,
|
|
QVBoxLayout
|
|
)
|
|
|
|
|
|
class GridDots(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.view = QGraphicsView()
|
|
self.scene = QGraphicsScene()
|
|
self.scene.setSceneRect(0, 0, 800, 800)
|
|
self.view.setScene(self.scene)
|
|
|
|
self.dots = []
|
|
self.lines = set()
|
|
self.setup_ui()
|
|
self.create_grid()
|
|
|
|
def setup_ui(self):
|
|
layout = QVBoxLayout(self)
|
|
layout.addWidget(self.view)
|
|
|
|
def create_grid(self):
|
|
"""Draw 3x3 grid of dots"""
|
|
dot_radius = 30
|
|
spacing = 240
|
|
offset = 150
|
|
|
|
for row in range(3):
|
|
for col in range(3):
|
|
x, y, = offset + col * spacing, offset + row * spacing
|
|
dot = QGraphicsEllipseItem(
|
|
x - dot_radius, y - dot_radius,
|
|
2 * dot_radius, 2 * dot_radius)
|
|
dot.setBrush(QBrush(Qt.black))
|
|
self.scene.addItem(dot)
|
|
self.dots.append((x, y))
|
|
|
|
def draw_line(self, start, end):
|
|
line_id = tuple(sorted((start, end)))
|
|
if line_id not in self.lines:
|
|
start_pos = QPointF(*self.dots[start])
|
|
end_pos = QPointF(*self.dots[end])
|
|
|
|
line = QGraphicsLineItem(
|
|
start_pos.x(), start_pos.y(),
|
|
end_pos.x(), end_pos.y())
|
|
|
|
line.setPen(QPen(Qt.black, 2))
|
|
self.scene.addItem(line)
|
|
self.lines.add(line_id)
|