Search Here

Python program to draw Circle using Midpoint algorithm

# Circle using Midpoint 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 MIDPOINT_CIRCLE():      
    x=0
    y=r
    d=1 - r    

    CIRCLEPOINTS(x,y)
    while(y>x):
        x+=1
        if(d<0):
            d=d+2*x+3
        else:
            y-=1
            d=d+2*x-2*y+5
        
        CIRCLEPOINTS(x,y)
    
    pygame.display.flip()
    time.sleep(5)
    
MIDPOINT_CIRCLE()

No comments:

Post a Comment