line on first layer then spheres
This commit is contained in:
@@ -15,7 +15,7 @@ class SphereGraphicsItem(QGraphicsEllipseItem):
|
||||
|
||||
SPHERE_WIDTH = 30
|
||||
|
||||
def __init__(self, x_pos: int, y_pos: int) -> None:
|
||||
def __init__(self, x_pos: int, y_pos: int, color: Qt.GlobalColor = Qt.GlobalColor.darkBlue) -> None:
|
||||
"""Create a sphere
|
||||
|
||||
Args:
|
||||
@@ -32,7 +32,7 @@ class SphereGraphicsItem(QGraphicsEllipseItem):
|
||||
self.y_pos = y_pos - self.SPHERE_WIDTH / 2
|
||||
logging.debug(f"sphere at {self.x_pos}:{self.y_pos}")
|
||||
|
||||
brush = QBrush(Qt.GlobalColor.darkBlue)
|
||||
brush = QBrush(color)
|
||||
self.setBrush(brush)
|
||||
pen = QPen(Qt.GlobalColor.blue)
|
||||
pen.setWidth(3)
|
||||
@@ -42,6 +42,15 @@ class SphereGraphicsItem(QGraphicsEllipseItem):
|
||||
def boundingRect(self) -> QRectF:
|
||||
return QRectF(self.x_pos, self.y_pos, self.SPHERE_WIDTH, self.SPHERE_WIDTH)
|
||||
|
||||
class HPSphereGraphicsItem(SphereGraphicsItem):
|
||||
|
||||
def __init__(self, x_pos: int, y_pos: int) -> None:
|
||||
super().__init__(x_pos, y_pos, Qt.GlobalColor.darkGreen)
|
||||
|
||||
class StrenghSphereGraphicsItem(SphereGraphicsItem):
|
||||
|
||||
def __init__(self, x_pos: int, y_pos: int) -> None:
|
||||
super().__init__(x_pos, y_pos, Qt.GlobalColor.darkRed)
|
||||
|
||||
class ArcGraphicsItem(QGraphicsItem):
|
||||
"""Circle arc."""
|
||||
@@ -72,7 +81,7 @@ class ArcGraphicsItem(QGraphicsItem):
|
||||
self.width = width
|
||||
self.angle_start = angle_start
|
||||
self.angle_end = angle_end
|
||||
self.draw_angle_start = int(angle_start / 16)
|
||||
self.draw_angle_start = int(angle_start * 16)
|
||||
self.draw_angle_end = int(angle_end * 16)
|
||||
logging.debug(
|
||||
"arc from %s:%s of %s from %s° to %s°",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import math
|
||||
import random
|
||||
|
||||
from typing import List, Tuple
|
||||
|
||||
@@ -12,33 +13,46 @@ from PySide6.QtWidgets import (
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
|
||||
from grid.ui.items import HPSphereGraphicsItem, SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem, StrenghSphereGraphicsItem
|
||||
|
||||
|
||||
class Pattern(QGraphicsItemGroup):
|
||||
"""Common pattern on the sphere grid"""
|
||||
|
||||
PATTERN_WIDTH = 600
|
||||
PATTERN_WIDTH = 400
|
||||
SPACE_BETWEEN_SPHERE = 60
|
||||
|
||||
def __init__(self, top_left_corner_x: int, top_left_corner_y: int) -> None:
|
||||
super().__init__()
|
||||
self.top_left_corner_x = top_left_corner_x
|
||||
self.top_left_corner_y = top_left_corner_y
|
||||
# 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))
|
||||
self.addToGroup(SphereGraphicsItem(0, 0))
|
||||
|
||||
line_start_x = center_x
|
||||
line_start_y = center_y
|
||||
line_end_x = center_x
|
||||
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE)
|
||||
self.addToGroup(
|
||||
LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self)
|
||||
)
|
||||
|
||||
line_start_x = top_left_corner_x + center_x
|
||||
line_start_y = top_left_corner_y + self.SPACE_BETWEEN_SPHERE * 1.5
|
||||
line_end_x = top_left_corner_x + center_x
|
||||
line_end_y = top_left_corner_y + self.SPACE_BETWEEN_SPHERE * 2.5
|
||||
self.addToGroup(
|
||||
LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self)
|
||||
)
|
||||
|
||||
self.addToGroup(HPSphereGraphicsItem(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-1):
|
||||
for i in range(sphere_on_circle):
|
||||
self.addToGroup(
|
||||
ArcGraphicsItem(
|
||||
x_pos=center_x,
|
||||
@@ -49,47 +63,39 @@ class Pattern(QGraphicsItemGroup):
|
||||
parent=self,
|
||||
)
|
||||
)
|
||||
|
||||
line_start_x = center_x
|
||||
line_start_y = center_y
|
||||
line_end_x = center_x
|
||||
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE)
|
||||
self.addToGroup(
|
||||
LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self)
|
||||
points = self.generate_circle_points(
|
||||
center_x, center_y, self.SPACE_BETWEEN_SPHERE, sphere_on_circle
|
||||
)
|
||||
for x, y in points:
|
||||
if random.random() > 0.5:
|
||||
self.addToGroup(SphereGraphicsItem(x, y))
|
||||
else:
|
||||
self.addToGroup(StrenghSphereGraphicsItem(x, y))
|
||||
|
||||
# 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-2):
|
||||
for i in range(sphere_on_circle):
|
||||
self.addToGroup(
|
||||
ArcGraphicsItem(
|
||||
x_pos=center_x,
|
||||
y_pos=center_y,
|
||||
width=self.SPACE_BETWEEN_SPHERE * 4,
|
||||
angle_start=int(i * 360 / sphere_on_circle / 4) % 360,
|
||||
angle_start=int(i * 360 / sphere_on_circle) % 360,
|
||||
angle_end=int((i + 1) * 360 / sphere_on_circle) % 360,
|
||||
parent=self,
|
||||
)
|
||||
)
|
||||
for x, y in points:
|
||||
self.addToGroup(SphereGraphicsItem(x, y))
|
||||
# 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:
|
||||
# self.addToGroup(SphereGraphicsItem(x, y))
|
||||
|
||||
def paint(
|
||||
self,
|
||||
painter: QPainter,
|
||||
@@ -99,10 +105,11 @@ class Pattern(QGraphicsItemGroup):
|
||||
super().paint(painter, option, widget)
|
||||
brush = QBrush(Qt.GlobalColor.gray)
|
||||
painter.setBrush(brush)
|
||||
painter.drawRect(0, 0, self.PATTERN_WIDTH, self.PATTERN_WIDTH)
|
||||
painter.drawRect(self.top_left_corner_x, self.top_left_corner_y, self.PATTERN_WIDTH, self.PATTERN_WIDTH)
|
||||
|
||||
@staticmethod
|
||||
def generate_circle_points(
|
||||
self, center_x: int, center_y: int, radius: int, n_points: int
|
||||
center_x: int, center_y: int, radius: int, n_points: int
|
||||
) -> List[Tuple[int, int]]:
|
||||
"""generate coordinates of points on a circle equally spaced
|
||||
|
||||
|
||||
@@ -17,12 +17,14 @@ class Window(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setWindowTitle("sphere grid")
|
||||
self.setGeometry(0, 0, 600, 600)
|
||||
self.setGeometry(0, 0, 800, 600)
|
||||
|
||||
self.scene = QGraphicsScene(0, 0, 600, 600)
|
||||
self.scene = QGraphicsScene(0, 0, 400, 400)
|
||||
|
||||
pp = Pattern(0, 0)
|
||||
self.scene.addItem(pp)
|
||||
pp1 = Pattern(0, 0)
|
||||
#pp2 = Pattern(401, 0)
|
||||
self.scene.addItem(pp1)
|
||||
#self.scene.addItem(pp2)
|
||||
|
||||
view = QGraphicsView(self.scene)
|
||||
view.setRenderHint(QPainter.RenderHint.Antialiasing)
|
||||
|
||||
Reference in New Issue
Block a user