Bresenham’s Circle Drawing Algorithm in C and C++
In this example, you will get the program code of Bresenham’s Circle Drawing Algorithm in C and C++.
This graphics program can be run by using Turbo C++ or Dev C++ compiler. Here we are using Turbo C++ compiler.
Bresenham’s Circle Drawing Algorithm
Bresenham’s circle drawing algorithm is an algorithm used to calculate the positions of pixels required for drawing a circle. It is an incremental algorithm used for approximating the user’s desired circle with the help of a series of straight lines. It is commonly used to draw circles and other curved objects on computers and other devices, including plotters and digital printers.
Bresenham Circle Drawing Algorithm Example in C
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 | //C program Code for Bresenham Circle Drawing Algorithm #include <stdio.h> #include <dos.h> #include <graphics.h> // Function for print circle void drawCircle(int xc, int yc, int x, int y) { putpixel(xc+x, yc+y, 15); putpixel(xc-x, yc+y, 15); putpixel(xc+x, yc-y, 15); putpixel(xc-x, yc-y, 15); putpixel(xc+y, yc+x, 15); putpixel(xc-y, yc+x, 15); putpixel(xc+y, yc-x, 15); putpixel(xc-y, yc-x, 15); } int main() { int xc = 200, yc = 200, r = 70,d,x,y; int gd = DETECT, gm; initgraph(&gd, &gm, "c:\turboc3\bgi"); // initialize graph printf("\n\n Bresenham Circle Drawing Algorithm Example in C Graphics\n\n"); x = 0, y = r; d = 3 - 2 * r; drawCircle(xc, yc, x, y); while (y >= x) { x++; if (d > 0) { y--; d = d + 4 * (x - y) + 10; } else d = d + 4 * x + 6; drawCircle(xc, yc, x, y); delay(70); } getch(); return 0; } |
Bresenham Circle Drawing Algorithm Example in C++
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 | //C++ program Code for Bresenham Circle Drawing Algorithm #include <iostream.h> #include <dos.h> #include <graphics.h> // Function for print circle void drawCircle(int xc, int yc, int x, int y) { putpixel(xc+x, yc+y, 15); putpixel(xc-x, yc+y, 15); putpixel(xc+x, yc-y, 15); putpixel(xc-x, yc-y, 15); putpixel(xc+y, yc+x, 15); putpixel(xc-y, yc+x, 15); putpixel(xc+y, yc-x, 15); putpixel(xc-y, yc-x, 15); } int main() { int xc = 200, yc = 200, r = 70,d,x,y; int gd = DETECT, gm; initgraph(&gd, &gm, "c:\turboc3\bgi"); // initialize graph cout<<"\n\nBresenham Circle Drawing Algorithm Example in C Graphics\n\n"; x = 0, y = r; d = 3 - 2 * r; drawCircle(xc, yc, x, y); while (y >= x) { x++; if (d > 0) { y--; d = d + 4 * (x - y) + 10; } else d = d + 4 * x + 6; drawCircle(xc, yc, x, y); delay(70); } getch(); return 0; } |
Output:
Other Circle Drawing Algorithm
MidPoint Circle Drawing Algorithm