Created
January 14, 2013 13:22
-
-
Save 0xKD/4530035 to your computer and use it in GitHub Desktop.
Mid-point circle drawing algorithm implemented in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mid-point circle drawing algorithm. | |
# Requires pygame: http://pygame.org | |
from pygame import gfxdraw | |
import sys,pygame | |
pygame.init() | |
screen = pygame.display.set_mode((400,400)) | |
screen.fill((0,0,0)) | |
pygame.display.flip() | |
def circle(radius,offset): | |
x,y = 0,radius | |
plotCircle(x,y,radius,offset) | |
def symmetry_points(x,y,offset): | |
gfxdraw.pixel(screen,x+offset,y+offset,(255,255,255)) | |
gfxdraw.pixel(screen,-x+offset,y+offset,(255,255,255)) | |
gfxdraw.pixel(screen,x+offset,-y+offset,(255,255,255)) | |
gfxdraw.pixel(screen,-x+offset,-y+offset,(255,255,255)) | |
gfxdraw.pixel(screen,y+offset,x+offset,(255,255,255)) | |
gfxdraw.pixel(screen,-y+offset,x+offset,(255,255,255)) | |
gfxdraw.pixel(screen,y+offset,-x+offset,(255,255,255)) | |
gfxdraw.pixel(screen,-y+offset,-x+offset,(255,255,255)) | |
pygame.display.flip() | |
def plotCircle(x,y,radius,offset): | |
d = 5/4.0 - radius | |
symmetry_points(x,y,radius+offset) | |
while x < y: | |
if d < 0: | |
x += 1 | |
d += 2*x + 1 | |
else: | |
x += 1 | |
y -= 1 | |
d += 2*(x-y) + 1 | |
symmetry_points(x,y,radius+offset) | |
circle(100,25) # circle(radius,<offset from edge>) | |
pygame.display.flip() | |
while 1: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks a lot
please add ellipse source too