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