C Program to implement the midpoint circle drawing algorithm

Friday, October 2nd, 2009

C Program to implement the midpoint circle drawing algorithm to draw a circle. Modify the algorithm toimplement specified arc or sector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#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);
}

Author Name :
Ranjith

Total : 9 Comments


9 Responses to “C Program to implement the midpoint circle drawing algorithm”

  1. Mohammad Faisal says:

    1-Error: Unable to open TCCLASS.LIB

  2. Nathulal says:

    that’s really coooooooolllllll…….

  3. PRABHAKAR says:

    THANKS BROTHERS

  4. kaushal says:

    hi
    it is more important to me because it will ask my exam

  5. Roberto says:

    Great code but how to invert the drawing direction of the arc? It is drawn with “anticlockwise” direction and I would like it to be drawn clockwise.
    Thanks, regards.

  6. Roberto says:

    Sorry, I forgot to write this: how to make the starting point on the left of the center (now is on the right) and draw the arc (or the circle) following a clockwise direction?

  7. RENU says:

    thanks sir..

  8. soumith says:

    how to directly copy into dosbox

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: