Search Here

Python program to draw Circle using Bresenham algorithm

# Circle using Bresenham Algorithm

import pygame
from pygame import gfxdraw
import time

screen = pygame.display.set_mode((400,300))
screen.fill((255,255,255))
black=(0,0,0)

xc = yc = 100 # Coordinate of the circle
r = 70 # Radius of the circle

def ROUND(n):
    return int(n+0.5)
    
def CIRCLEPOINTS(x,y):
    gfxdraw.pixel(screen,xc+x,yc-y,black)
    gfxdraw.pixel(screen,xc-x,yc-y,black)
    gfxdraw.pixel(screen,xc+x,yc+y,black)
    gfxdraw.pixel(screen,xc-x,yc+y,black)
    gfxdraw.pixel(screen,xc+y,yc-x,black)
    gfxdraw.pixel(screen,xc-y,yc-x,black)
    gfxdraw.pixel(screen,xc+y,yc+x,black)
    gfxdraw.pixel(screen,xc-y,yc+x,black)  

def BRESENHAM_CIRCLE():     
    x=0
    y=r

    CIRCLEPOINTS(x,y)
    p=3-2*r
    while(y>x):
        if(p<0):
            p=p+4*x+6
        else:
            y=y-1
            p=p+4*(x-y)+10
        x=x+1
        CIRCLEPOINTS(x,y)

    pygame.display.flip()
    time.sleep(15)  
    
# Main starts   
BRESENHAM_CIRCLE()

No comments:

Post a Comment