Drawing a Square using Midpoint Circle Algorithm
In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for rasterizing a circle. Bresenham's circle algorithm is derived from the midpoint circle algorithm. This algorithm is used to draw square. But we can draw many shapes from it with little modifications in the formula.
Below is the code written in C to draw a square using midpoint algorithm.
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Title: Square using Midpoint Circle Algorithm | |
Description: C Program to draw a Square using Midpoint Circle Algorithm | |
Author: Saideep Dicholkar | |
*/ | |
#include<stdio.h> | |
#include<graphics.h> | |
void main() | |
{ | |
int d,x,y,xc,yc,r; | |
int gd=DETECT,gm; | |
initgraph(&gd,&gm,"C:\\tc\\bgi"); | |
cleardevice(); | |
printf("Enter radius of circle: "); | |
scanf("%d",&r); | |
printf("Enter x & y coordinates:\n"); | |
scanf("%d%d",&xc,&yc); | |
d=1-r; | |
x=0; | |
y=r; | |
do | |
{ | |
putpixel(xc+x,yc+y,WHITE); | |
putpixel(xc+x,yc-y,WHITE); | |
putpixel(xc-x,yc+y,WHITE); | |
putpixel(xc-x,yc-y,WHITE); | |
putpixel(xc+y,yc+x,WHITE); | |
putpixel(xc+y,yc-x,WHITE); | |
putpixel(xc-y,yc+x,WHITE); | |
putpixel(xc-y,yc-x,WHITE); | |
delay(100); | |
if(d<0) | |
d=d+(2-x)+3; | |
else | |
{ | |
d=d+5+(2*(x-y)); | |
y=y-1; | |
} | |
x=x+1; | |
}while(x<y); | |
getch(); | |
closegraph(); | |
restorecrtmode(); | |
} |
If you can't see the code, then Enable JavaScript in you browser
Output:
Note: Just copy the files "CGA.bgi" &" EGAVGA.bgi" from bgi folder to bin. So, if you have copied these 2 files then you can skip writing "C:\\tc\\bgi" in initgraph with just ""(2 double quotes).
connaciki Efrain Pleasant https://wakelet.com/wake/rPgHxng4Ruxf26cghDl0r
ReplyDeleteenopboopat