This commit is contained in:
@@ -1,22 +1,21 @@
|
||||
class Cell:
|
||||
|
||||
MIN_NEIGHBORS = 1
|
||||
MAX_NEIGHBORS = 4
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.neighbors = []
|
||||
|
||||
def add_neightbor(self, new_neighbor: 'Cell'):
|
||||
def add_neightbor(self, new_neighbor: "Cell"):
|
||||
self.neighbors.append(new_neighbor)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "o"
|
||||
|
||||
class StatSphere(Cell):
|
||||
|
||||
class StatSphere(Cell):
|
||||
pass
|
||||
|
||||
|
||||
class StrengthSphere(StatSphere):
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"""
|
||||
"""
|
||||
""" """
|
||||
|
||||
from random import choice, randint
|
||||
|
||||
from grid.sphere.cell import Cell
|
||||
|
||||
class Grid:
|
||||
|
||||
class Grid:
|
||||
def __init__(self) -> None:
|
||||
self.start = None
|
||||
|
||||
@@ -13,7 +13,7 @@ class Grid:
|
||||
self.start = cell
|
||||
|
||||
@classmethod
|
||||
def generate_grid(cls, sphere_number: int=10) -> 'Grid':
|
||||
def generate_grid(cls, sphere_number: int = 10) -> "Grid":
|
||||
# pick an impl type instead
|
||||
starting_point = Cell()
|
||||
cls.generate_neighbors(starting_point, sphere_number)
|
||||
@@ -26,5 +26,5 @@ class Grid:
|
||||
number_of_neighbor = randint(Cell.MIN_NEIGHBORS, Cell.MAX_NEIGHBORS)
|
||||
for _ in number_of_neighbor:
|
||||
current_cell.add_neightbor(Cell())
|
||||
remaining_cells = max(0, remaining_cells-number_of_neighbor)
|
||||
remaining_cells = max(0, remaining_cells - number_of_neighbor)
|
||||
return choice(current_cell.neighbors)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from grid.ui.pattern import Pattern
|
||||
|
||||
class TestPattern():
|
||||
|
||||
class TestPattern:
|
||||
def test_position(self):
|
||||
p = Pattern(500, 500)
|
||||
assert len(p.childItems()) > 0
|
||||
assert len(p.childItems()) > 0
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
from PySide6.QtCore import QRectF, Qt
|
||||
from PySide6.QtGui import QBrush, QPainter, QPen
|
||||
from PySide6.QtWidgets import QGraphicsEllipseItem, QGraphicsItem, QStyleOptionGraphicsItem, QWidget
|
||||
from PySide6.QtWidgets import (
|
||||
QGraphicsEllipseItem,
|
||||
QGraphicsItem,
|
||||
QStyleOptionGraphicsItem,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
|
||||
class SphereGraphicsItem(QGraphicsEllipseItem):
|
||||
|
||||
SPHERE_WIDTH = 30
|
||||
|
||||
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)
|
||||
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)
|
||||
@@ -17,36 +27,69 @@ class SphereGraphicsItem(QGraphicsEllipseItem):
|
||||
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 = ...):
|
||||
def __init__(
|
||||
self,
|
||||
x_pos: int,
|
||||
y_pos: int,
|
||||
width: int,
|
||||
angle_start: int,
|
||||
angle_end: int,
|
||||
parent: QGraphicsItem | None = ...,
|
||||
):
|
||||
super().__init__(parent)
|
||||
shift = int(width/2)
|
||||
self.x_pos = x_pos-shift
|
||||
self.y_pos = y_pos-shift
|
||||
self.width = width*2
|
||||
self.angle_start = angle_start*16
|
||||
self.angle_end = angle_end*16
|
||||
print(f"arc from {self.x_pos}:{self.y_pos} of {self.width} {self.angle_start}° {self.angle_end}°")
|
||||
shift = int(width / 2)
|
||||
self.x_pos = x_pos - shift
|
||||
self.y_pos = y_pos - shift
|
||||
self.width = width * 2
|
||||
self.angle_start = angle_start * 16
|
||||
self.angle_end = angle_end * 16
|
||||
print(
|
||||
f"arc from {self.x_pos}:{self.y_pos} of {self.width} {self.angle_start}° {self.angle_end}°"
|
||||
)
|
||||
|
||||
def paint(
|
||||
self,
|
||||
painter: QPainter,
|
||||
option: QStyleOptionGraphicsItem,
|
||||
widget: QWidget | None = ...,
|
||||
) -> None:
|
||||
painter.drawArc(
|
||||
self.x_pos,
|
||||
self.y_pos,
|
||||
self.width,
|
||||
self.width,
|
||||
self.angle_start,
|
||||
self.angle_end,
|
||||
)
|
||||
|
||||
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget | None = ...) -> None:
|
||||
painter.drawArc(self.x_pos, self.y_pos, self.width,
|
||||
self.width, self.angle_start, self.angle_end)
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
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:
|
||||
|
||||
@@ -85,21 +85,19 @@ class TrafficLight(QWidget):
|
||||
|
||||
machine = QStateMachine(self)
|
||||
red_going_yellow = create_light_state(widget._red_light, 1000)
|
||||
red_going_yellow.setObjectName('redGoingYellow')
|
||||
red_going_yellow.setObjectName("redGoingYellow")
|
||||
yellow_going_green = create_light_state(widget._red_light, 1000)
|
||||
yellow_going_green.setObjectName('yellowGoingGreen')
|
||||
red_going_yellow.addTransition(red_going_yellow.finished,
|
||||
yellow_going_green)
|
||||
yellow_going_green.setObjectName("yellowGoingGreen")
|
||||
red_going_yellow.addTransition(red_going_yellow.finished, yellow_going_green)
|
||||
green_going_yellow = create_light_state(widget._yellow_light, 3000)
|
||||
green_going_yellow.setObjectName('greenGoingYellow')
|
||||
yellow_going_green.addTransition(yellow_going_green.finished,
|
||||
green_going_yellow)
|
||||
green_going_yellow.setObjectName("greenGoingYellow")
|
||||
yellow_going_green.addTransition(
|
||||
yellow_going_green.finished, green_going_yellow
|
||||
)
|
||||
yellow_going_red = create_light_state(widget._green_light, 1000)
|
||||
yellow_going_red.setObjectName('yellowGoingRed')
|
||||
green_going_yellow.addTransition(green_going_yellow.finished,
|
||||
yellow_going_red)
|
||||
yellow_going_red.addTransition(yellow_going_red.finished,
|
||||
red_going_yellow)
|
||||
yellow_going_red.setObjectName("yellowGoingRed")
|
||||
green_going_yellow.addTransition(green_going_yellow.finished, yellow_going_red)
|
||||
yellow_going_red.addTransition(yellow_going_red.finished, red_going_yellow)
|
||||
|
||||
machine.addState(red_going_yellow)
|
||||
machine.addState(yellow_going_green)
|
||||
@@ -109,9 +107,9 @@ class TrafficLight(QWidget):
|
||||
machine.start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
widget = TrafficLight()
|
||||
widget.resize(110, 300)
|
||||
widget.show()
|
||||
sys.exit(app.exec())
|
||||
sys.exit(app.exec())
|
||||
|
||||
@@ -3,17 +3,16 @@ import math
|
||||
from typing import List, Tuple
|
||||
|
||||
from PySide6.QtWidgets import QGraphicsItemGroup
|
||||
from random import randint
|
||||
|
||||
from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
|
||||
|
||||
|
||||
class Pattern(QGraphicsItemGroup):
|
||||
"""Common pattern on the sphere grid
|
||||
"""
|
||||
"""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
|
||||
@@ -26,35 +25,66 @@ class Pattern(QGraphicsItemGroup):
|
||||
|
||||
# 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:
|
||||
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))
|
||||
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))
|
||||
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:
|
||||
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))
|
||||
self.addToGroup(ArcGraphicsItem(center_x, center_y, self.SPACE_BETWEEN_SPHERE*2, 0, 90, self))
|
||||
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,
|
||||
)
|
||||
)
|
||||
self.addToGroup(
|
||||
ArcGraphicsItem(
|
||||
center_x, center_y, self.SPACE_BETWEEN_SPHERE * 2, 0, 90, 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:
|
||||
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]]:
|
||||
|
||||
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:
|
||||
@@ -74,5 +104,5 @@ class Pattern(QGraphicsItemGroup):
|
||||
x = int(center_x + radius * math.cos(theta))
|
||||
y = int(center_y + radius * math.sin(theta))
|
||||
points.append((x, y))
|
||||
|
||||
|
||||
return points
|
||||
|
||||
@@ -6,13 +6,13 @@ from PySide6.QtWidgets import (
|
||||
QGraphicsScene,
|
||||
QGraphicsView,
|
||||
QHBoxLayout,
|
||||
QWidget
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from grid.ui.pattern import Pattern
|
||||
|
||||
class Window(QWidget):
|
||||
|
||||
class Window(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setWindowTitle("sphere grid")
|
||||
@@ -21,7 +21,6 @@ class Window(QWidget):
|
||||
self.scene = QGraphicsScene(0, 0, 600, 600)
|
||||
|
||||
pp = Pattern(300, 300)
|
||||
|
||||
self.scene.addItem(pp)
|
||||
|
||||
view = QGraphicsView(self.scene)
|
||||
@@ -33,6 +32,7 @@ class Window(QWidget):
|
||||
self.setLayout(vbox)
|
||||
self.show()
|
||||
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
w = Window()
|
||||
app.exec()
|
||||
|
||||
Reference in New Issue
Block a user