Compare commits
2 Commits
787d4e776a
...
46daf9ec93
| Author | SHA1 | Date | |
|---|---|---|---|
| 46daf9ec93 | |||
| 054384ca85 |
@@ -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
|
||||
|
||||
76
grid/ui/pattern.py
Normal file
76
grid/ui/pattern.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import math
|
||||
|
||||
from typing import List, Tuple
|
||||
|
||||
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
|
||||
sphere_on_circle = 4
|
||||
points = self.generate_circle_points(center_x, center_y, self.SPACE_BETWEEN_SPHERE, sphere_on_circle)
|
||||
for x,y in points:
|
||||
self.addToGroup(SphereGraphicsItem(x, y))
|
||||
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))
|
||||
|
||||
|
||||
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
|
||||
points = self.generate_circle_points(center_x, center_y, self.SPACE_BETWEEN_SPHERE*2, sphere_on_circle)
|
||||
for x,y in points:
|
||||
self.addToGroup(SphereGraphicsItem(x, y))
|
||||
#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))
|
||||
|
||||
# third cercle
|
||||
sphere_on_circle = 8
|
||||
points = self.generate_circle_points(center_x, center_y, self.SPACE_BETWEEN_SPHERE*3, sphere_on_circle)
|
||||
for x,y in points:
|
||||
self.addToGroup(SphereGraphicsItem(x, y))
|
||||
|
||||
def generate_circle_points(self, center_x: int, center_y: int, radius: int, n_points: int) -> List[Tuple[int, int]]:
|
||||
"""generate coordinates of points on a circle equally spaced
|
||||
|
||||
Args:
|
||||
center_x (int): center x coordinate
|
||||
center_y (int): center y coordinate
|
||||
radius (int): circle radius
|
||||
n_points (int): number of points on the circle to generate
|
||||
|
||||
Returns:
|
||||
List[Tuple[int, int]]: list of points coordinates, (x,y)
|
||||
"""
|
||||
points = []
|
||||
angle_step = 2 * math.pi / n_points
|
||||
|
||||
for i in range(n_points):
|
||||
theta = i * angle_step
|
||||
x = int(center_x + radius * math.cos(theta))
|
||||
y = int(center_y + radius * math.sin(theta))
|
||||
points.append((x, y))
|
||||
|
||||
return points
|
||||
@@ -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.darkBlue)
|
||||
self.setBrush(brush)
|
||||
pen = QPen(Qt.GlobalColor.blue)
|
||||
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)
|
||||
|
||||
@@ -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(200, 100)
|
||||
#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()
|
||||
|
||||
Reference in New Issue
Block a user