#!/usr/bin/env python import pygame from pygame.locals import * def circle_draw( x, y, radius, color, antialias, surf ): def draw_points( x0, y0, ox, oy ): surf.set_at( ( x0 + ox, y0 + oy ), color ) surf.set_at( ( x0 + ox, y0 - oy ), color ) surf.set_at( ( x0 - ox, y0 + oy ), color ) surf.set_at( ( x0 - ox, y0 - oy ), color ) surf.set_at( ( x0 + oy, y0 + ox ), color ) surf.set_at( ( x0 + oy, y0 - ox ), color ) surf.set_at( ( x0 - oy, y0 + ox ), color ) surf.set_at( ( x0 - oy, y0 - ox ), color ) # draw_points() def trivial( x, y, radius, color, surf ): """Uses floating point operations to calculate values""" from math import ceil, sqrt for ox in xrange( 0, int(ceil( float(radius) / sqrt(2) )) ): oy = sqrt( radius ** 2 - ox ** 2 ) draw_points( x, y, int(ox), int(oy) ) # trivial() def bresenham( x, y, radius, color, surf ): """Uses integer only operations to calculate values""" ox = 0 oy = radius e = 1 - radius while ox <= oy: draw_points( x, y, ox, oy ) e += 2 * ox + 3 ox += 1 if e > 0: e += 2 - 2 * oy oy -= 1 # bresenham() ## Base (correct) circle #pygame.draw.circle( surf, Color( "gray50" ), ( x, y ), radius, 5 ) #trivial( x, y, radius, color, surf ) bresenham( x, y, radius, color, surf ) # circle_draw() pygame.init() screen = pygame.display.set_mode( ( 800, 600 ) ) screen.fill( Color( "black" ) ) circle_draw( 100, 100, 50, Color( "green" ), 0, screen ) run = True clock = pygame.time.Clock() while run: for e in pygame.event.get(): if e.type == QUIT or \ e.type == KEYDOWN and e.key == K_ESCAPE: run = False pygame.display.flip() clock.tick( 24 )