Write a C Program to implement midpoint circle drawing algorithm in Graphics

By | 02.12.2016

Implement midpoint circle drawing algorithm


Write a C Program to implement midpoint circle drawing algorithm in Graphics. Here’s simple C Program to implement midpoint circle drawing algorithm in Graphics in C Programming Language.


Below is the source code for C Program to implement midpoint circle drawing algorithm in Graphics which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

 


/*  C Program to implement midpoint circle drawing algorithm in Graphics  */

#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
 
#define PI 3.14
 
float startangle,endangle;
int x,y;
 
int Can_draw( float theta )
{
        if( theta >= startangle && theta<= endangle )
                return 1;
        return 0;
}
 
void Circlepoints(int x,int y,int xc,int yc)
{
        float theta;
        theta = atan( (float)y/x );
        theta = theta * (180/M_PI);
 
if( Can_draw(theta))
                putpixel(xc+x,yc-y,WHITE);
        if( Can_draw(360-theta))
                putpixel(xc+x,yc+y,WHITE);
 
if( Can_draw(90-theta))
                putpixel(xc+y,yc-x,WHITE);
        if( Can_draw(270+theta))
                putpixel(xc+y,yc+x,WHITE);
 
if( Can_draw(180-theta))
                putpixel(xc-x,yc-y,WHITE);
        if( Can_draw(180+theta))
                putpixel(xc-x,yc+y,WHITE);
 
if( Can_draw(90+theta))
                putpixel(xc-y,yc-x,WHITE);
        if( Can_draw(270-theta))
                putpixel(xc-y,yc+x,WHITE);
}
 
void MidPointcircle(int xc,int yc,int rad)
{
        float d = (5/4.0) - rad;
        x=0,y=rad;
 
        while(y>x)
        {
                if(d<0) 
                        d += 2*x+3;
                else    
                        d+=(2*x)-(2*y)+5,y--;
                x++;
                Circlepoints(x,y,xc,yc);
                delay(90);
        }
}
void main()
{
        int gd=DETECT,gm;
        int radius,xc,yc,choice,temp;
        float xstart,ystart,xend,yend;
        initgraph(&gd,&gm,"..\\bgi");
        do
        {
                clrscr();
                cleardevice();
                printf("\n Enter your choice\n");
                printf("\n 1.Draw a Circle\n 2.Draw a Sector\n 3.Draw an Arc\n 4.Exit\n");
                scanf("%d",&choice);
                switch(choice)
                {
                        case 1: printf("\n Enter the center:");
                                scanf("%d %d",&xc,&yc);
                                printf("\n Enter the radius:");
                                scanf("%d",&radius);
                                cleardevice();
                                startangle=0,endangle=360;
                                MidPointcircle(xc,yc,radius);
                                getch();
                                break;
 
                        case 2: printf("\n Enter the center:");
                                scanf("%d %d",&xc,&yc);
                                printf("\n Enter the radius:");
                                scanf("%d",&radius);
                                printf("\n Enter the startangle:");
                                scanf("%f",&startangle);
                                printf("\n Enter the endangle:");
                                scanf("%f",&endangle);
                                cleardevice();
                                if(startangle>endangle)
                                {
                                        temp=startangle;
                                        startangle=endangle;
                                        endangle=temp;
                                }
                                MidPointcircle(xc,yc,radius);
                                xstart=xc+radius*cos(PI/180*startangle);
                                ystart=yc-radius*sin(PI/180*startangle);
                                xend=xc+radius*cos(PI/180*endangle);
                                yend=yc-radius*sin(PI/180*endangle);
                                line(xc,yc,xstart,ystart);
                                line(xc,yc,xend,yend);
                                getch();
                                break;
 
                        case 3: printf("\n Enter the center:");
                                scanf("%d %d",&xc,&yc);
                                printf("\n Enter the radius:");
                                scanf("%d",&radius);
                                printf("\n Enter the startangle:");
                                scanf("%f",&startangle);
                                printf("\n Enter the endangle:");
                                scanf("%f",&endangle);
                                cleardevice();
                                if(startangle>endangle)
                                {
                                        temp=startangle;
                                        startangle=endangle;
                                        endangle=temp;
                                }
                                MidPointcircle(xc,yc,radius);
                                getch();
                                break;
 
                       case 4:  closegraph();
                }
        }while(choice!=4);
}

Above is the source code for C Program to implement midpoint circle drawing algorithm in Graphics which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Category: C Programming Graphic Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments