From 46daf9ec9325fd5521cf0cdc037a7b27829e66a4 Mon Sep 17 00:00:00 2001 From: Alexandre RIO Date: Tue, 10 Sep 2024 21:54:07 +0200 Subject: [PATCH] second and third circle --- grid/ui/pattern.py | 48 +++++++++++++++++++++++++++++++++------- grid/ui/sphere_widget.py | 4 ++-- grid/ui/window.py | 2 +- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/grid/ui/pattern.py b/grid/ui/pattern.py index d530a78..10a4710 100644 --- a/grid/ui/pattern.py +++ b/grid/ui/pattern.py @@ -1,3 +1,7 @@ +import math + +from typing import List, Tuple + from PySide6.QtWidgets import QGraphicsItemGroup from random import randint @@ -20,16 +24,12 @@ class Pattern(QGraphicsItemGroup): 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 + 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)) - 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) @@ -40,5 +40,37 @@ class Pattern(QGraphicsItemGroup): # 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)) \ No newline at end of file + # 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 diff --git a/grid/ui/sphere_widget.py b/grid/ui/sphere_widget.py index ac925b8..32f670f 100644 --- a/grid/ui/sphere_widget.py +++ b/grid/ui/sphere_widget.py @@ -10,9 +10,9 @@ class SphereGraphicsItem(QGraphicsEllipseItem): 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) + brush = QBrush(Qt.GlobalColor.darkBlue) self.setBrush(brush) - pen = QPen(Qt.GlobalColor.green) + pen = QPen(Qt.GlobalColor.blue) pen.setWidth(3) self.setPen(pen) self.show() diff --git a/grid/ui/window.py b/grid/ui/window.py index d05eb86..10e8b23 100644 --- a/grid/ui/window.py +++ b/grid/ui/window.py @@ -20,7 +20,7 @@ class Window(QWidget): self.scene = QGraphicsScene(0, 0, 600, 400) - pp = Pattern(0, 0) + pp = Pattern(200, 100) #for wid in pp.content: # self.scene.addItem(wid)