first circle

This commit is contained in:
2024-09-04 21:26:42 +02:00
parent 787d4e776a
commit 054384ca85
4 changed files with 109 additions and 45 deletions

View File

@@ -1,8 +1,10 @@
"""
"""
from random import choice, randint
from grid.sphere.cell import Cell
class Grid():
class Grid:
def __init__(self) -> None:
self.start = None

44
grid/ui/pattern.py Normal file
View File

@@ -0,0 +1,44 @@
from PySide6.QtWidgets import QGraphicsItemGroup
from random import randint
from grid.ui.sphere_widget import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
class Pattern(QGraphicsItemGroup):
"""Common pattern on the sphere grid
"""
PATTERN_WIDTH = 250
SPACE_BETWEEN_SPHERE = 60
def __init__(self, top_left_corner_x: int, top_left_corner_y: int) -> None:
super().__init__()
# center coordinates of the pattern
center_x = int((top_left_corner_x + self.PATTERN_WIDTH) / 2)
center_y = int((top_left_corner_y + self.PATTERN_WIDTH) / 2)
# center
self.addToGroup(SphereGraphicsItem(center_x, center_y))
# first cercle
self.addToGroup(SphereGraphicsItem(center_x, center_y - self.SPACE_BETWEEN_SPHERE))
self.addToGroup(SphereGraphicsItem(center_x, center_y + self.SPACE_BETWEEN_SPHERE))
self.addToGroup(SphereGraphicsItem(center_x - self.SPACE_BETWEEN_SPHERE, center_y))
self.addToGroup(SphereGraphicsItem(center_x + self.SPACE_BETWEEN_SPHERE, center_y))
sphere_on_circle = 4
for i in range(sphere_on_circle):
self.addToGroup(ArcGraphicsItem(center_x, center_y, self.SPACE_BETWEEN_SPHERE, (i*360/sphere_on_circle)%360, ((i+1)*360/sphere_on_circle)%360, self))
print(center_x-self.SPACE_BETWEEN_SPHERE+i*self.SPACE_BETWEEN_SPHERE)
self.addToGroup(SphereGraphicsItem(center_x, center_y - self.SPACE_BETWEEN_SPHERE))
line_start_x = int(center_x + self.SPACE_BETWEEN_SPHERE/2)
line_start_y = int(center_y + self.SPACE_BETWEEN_SPHERE/2)
line_end_x = int(center_x + self.SPACE_BETWEEN_SPHERE/2)
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE*1.5)
self.addToGroup(LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self))
# second cercle
sphere_on_circle = 8
#for i in range(sphere_on_circle):
# self.addToGroup(ArcGraphicsItem(center_x, center_y, self.SPACE_BETWEEN_SPHERE*2, int(i*360/sphere_on_circle/4)%360, int((i+1)*360/sphere_on_circle)%360, self))

View File

@@ -1,15 +1,53 @@
from PySide6.QtCore import Qt
from PySide6.QtGui import QPaintEvent, QPainter, QPalette
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
from PySide6.QtCore import QRectF, Qt
from PySide6.QtGui import QBrush, QPainter, QPen
from PySide6.QtWidgets import QGraphicsEllipseItem, QGraphicsItem, QStyleOptionGraphicsItem, QWidget
class SphereWidget(QWidget):
class SphereGraphicsItem(QGraphicsEllipseItem):
def __init__(self) -> None:
super().__init__()
SPHERE_WIDTH = 30
def paintEvent(self, event: QPaintEvent) -> None:
with QPainter(self) as painter:
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(Qt.red)
painter.drawEllipse(0, 0, self.width(), self.height())
return super().paintEvent(event)
def __init__(self, x_pos: int, y_pos: int) -> None:
super().__init__(self.SPHERE_WIDTH/2, self.SPHERE_WIDTH/2, self.SPHERE_WIDTH, self.SPHERE_WIDTH)
print(f"sphere at {x_pos}:{y_pos}")
self.setPos(x_pos, y_pos)
brush = QBrush(Qt.GlobalColor.blue)
self.setBrush(brush)
pen = QPen(Qt.GlobalColor.green)
pen.setWidth(3)
self.setPen(pen)
self.show()
class ArcGraphicsItem(QGraphicsItem):
def __init__(self, x_pos: int, y_pos: int, width: int, angle_start: int, angle_end: int, parent: QGraphicsItem | None = ...):
super().__init__(parent)
print(f"arc from {x_pos}:{y_pos} of {width} {angle_start}° {angle_end}°")
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.angle_start = angle_start
self.angle_end = angle_end
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget | None = ...) -> None:
shift = int(self.width/2)
painter.drawArc(self.x_pos-shift, self.y_pos-shift, self.width*2,
self.width*2, self.angle_start*16, self.angle_end*16)
def boundingRect(self) -> QRectF:
return QRectF(0, 0, self.x_pos, self.y_pos)
class LineGraphicsItem(QGraphicsItem):
def __init__(self, begin_x: int, begin_y: int, end_x: int, end_y: int, parent: QGraphicsItem | None = ...) -> None:
super().__init__(parent)
print(f"line from {begin_x}:{begin_y} to {end_x}:{end_y}")
self.begin_x = begin_x
self.begin_y = begin_y
self.end_x = end_x
self.end_y = end_y
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget | None = ...) -> None:
painter.drawLine(self.begin_x, self.begin_y, self.end_x, self.end_y)
def boundingRect(self) -> QRectF:
return QRectF(self.begin_x, self.begin_y, self.end_x, self.end_y)

View File

@@ -1,48 +1,30 @@
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QPainter, QPen
from PySide6.QtGui import QPainter
from PySide6.QtWidgets import (
QApplication,
QGraphicsEllipseItem,
QGraphicsRectItem,
QGraphicsScene,
QGraphicsView,
QHBoxLayout,
QVBoxLayout,
QWidget,
QWidget
)
from grid.ui.pattern import Pattern
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("sphere grid")
self.setGeometry(0, 0, 600, 600)
# Defining a scene rect of 400x200, with it's origin at 0,0.
# If we don't set this on creation, we can set it later with .setSceneRect
self.scene = QGraphicsScene(0, 0, 600, 400)
for i in range(5):
# Draw a rectangle item, setting the dimensions.
rect = QGraphicsRectItem(0, 0, 200, 50)
rect.setPos(50+i*10, 20+ i*20)
brush = QBrush(Qt.GlobalColor.red)
rect.setBrush(brush)
# Define the pen (line)
pen = QPen(Qt.GlobalColor.cyan)
pen.setWidth(10)
rect.setPen(pen)
self.scene.addItem(rect)
pp = Pattern(0, 0)
#for wid in pp.content:
# self.scene.addItem(wid)
ellipse = QGraphicsEllipseItem(0, 0, 100, 100)
ellipse.setPos(75, 30)
brush = QBrush(Qt.GlobalColor.blue)
ellipse.setBrush(brush)
pen = QPen(Qt.GlobalColor.green)
pen.setWidth(5)
ellipse.setPen(pen)
# Add the items to the scene. Items are stacked in the order they are added.
self.scene.addItem(ellipse)
self.scene.addItem(pp)
# Define our layout.
hbox = QHBoxLayout()
@@ -55,10 +37,8 @@ class Window(QWidget):
vbox.addWidget(view)
self.setLayout(vbox)
self.show()
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec()