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.