Program to Draw a Circle using Midpoint Algorithm
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "c:\\TurboC3\\BGI");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();
}
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
if (d >= 0)
{
x++;
y--;
d=d+2*(x-y)+5;
}
else
{
x++;
d = d + 2 * x + 3;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}
OUTPUT:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void circleBres(int xc, int yc, int r);
void drawCircle(int xc, int yc, int x, int y);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "c:\\TurboC3\\BGI");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();
}
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
if (d >= 0)
{
x++;
y--;
d=d+2*(x-y)+5;
}
else
{
x++;
d = d + 2 * x + 3;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}
OUTPUT:
No comments:
Post a Comment