-1

I drew a circle using pygame.draw.circle(), and I was wondering if there is an easy way to get the coordinates for every pixel that makes up the circle.

I plan to put the coordinates in a list to access later.

Here is what I've tried so far:

I tried drawing a circle pixel-by-pixel on my own so that I could add all the pixel coordinates to a list and use that, but the "circle" that I made (white) isn't made up of the same pixels as the Pygame circle (black).


import pygame
import random
pygame.init()

winW = 26
winH = 26
scaling = 30

win = pygame.display.set_mode((winW*scaling, winH*scaling))

screen = pygame.Surface((winW, winH))

running = True

def fillScreen():
    global winW
    global winH
    for x in range (0, winW):
        for y in range (0, winH):
            screen.set_at((x, y), (random.randrange(5, 255), random.randrange(5, 255), random.randrange(5, 255)))

def drawPygameCircle(radius):
    global winW
    global winH
    pygame.draw.circle(screen, (0,0,0), (winW/2, winH/2), radius)

def myCircle(radius):
    global circlePointSet
    circlePointSet = []
    searchSquare = []
    for x in range(int(winW/2-radius), int(winW/2+radius)):
        for y in range(int(winH/2-radius), int(winH/2+radius)):
            searchSquare.append((x,y))
    for i in searchSquare:
        screen.set_at(i, (100, 100, 100))
    for i in searchSquare:
        if (i[0]-winW/2)**2 + (i[1]-winH/2)**2 <= radius**2:
            circlePointSet.append(i)
    for i in circlePointSet:
        screen.set_at(i, (255, 255, 255))
    del circlePointSet
    del searchSquare

fillScreen()

myCircle(5)     #my attempts to draw a circle or radius 5
drawPygameCircle(5)               #draw a circle using Pygame

win.blit(pygame.transform.scale(screen, (winW*scaling, winH*scaling)), (0, 0))

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.update()

pygame.quit()

I then looked into how Pygame circles are drawn, and it appears to be through a modified "Bresenham Algorithm", but I don't fully understand how that works.

Edit:

Please ignore.

Biri
  • 1
  • 2
  • *"I plan to put the coordinates in a list to access later."* - What do you want to do with these coordinates? Do you need them for collision detection? – Rabbid76 Jun 08 '23 at 21:02
  • 1
    @Rabbid76 Yes, and also to prevent multiple circles from overlapping with each other. – Biri Jun 08 '23 at 21:11

1 Answers1

0

I think that you should take a look into algebra (maths). In my opinion you are lacking important concepts regarding the fundamentals of data visualization.

In algebra you will see how a circle is mathematically modeled and how you can obtain all the points that compose a circle from a center point and its radious.

Answering your question: Mathematically, a circle is composed of infinite points, but in an information system data must be discretized in order to be represented. The Bresenham algorithm is used to print lines in a screen for this purpose, so it basically chooses which pixels have to be coloured out in order to represent correctly the line. Modifications of the Bresenham algorithm are commonly used to print other similar structures in a screen, but if you are doing visualization you should start by properly understanding the Bresenham algorithm.

This is Java code that implemement the Bresenham algorithm.

Aleix Mariné
  • 110
  • 1
  • 9