Skip to main content

C Code For Bresenham's Circle Drawing Algorithm


Bresenham’s Algorithm: C Code For Bresenham's Circle Drawing Algorithm

Bresenham's circle algorithm is derived from the midpoint circle algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel position to complete the arc.
From the following illustration, you can see that we have put the pixel at (X, Y) location and now need to decide where to put the next pixel − at N (X+1, Y) or at S (X+1, Y-1)
This can be decided by the decision parameter d.
  • If d <= 0, then N(X+1, Y) is to be chosen as next pixel.
  • If d > 0, then S(X+1, Y-1) is to be chosen as the next pixel.

Algorithm

Step 1 − Get the coordinates of the center of the circle and radius, and store them in x, y, and R respectively. Set P=0 and Q=R.
Step 2 − Set decision parameter D = 3 – 2R.
Step 3 − Repeat through step-8 while P ≤ Q.
Step 4 − Call Draw Circle (X, Y, P, Q).
Step 5 − Increment the value of P.
Step 6 − If D < 0 then D = D + 4P + 6.
Step 7 − Else Set R = R - 1, D = D + 4(P-Q) + 10.
Step 8 − Call Draw Circle (X, Y, P, Q).

This is the program 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
conio.h
dos.h
stdio.h
process.h
graphics.h
math.h
void main()
{
   int r,gd,gm,d,x,y;
   printf("\n Enter Radius For Bresenham's Circle Drawing : ");
   scanf("%d",&r);
   detectgraph(&gd,&gm);
   initgraph(&gd,&gm,"bgi");
   d=3-2*r;
   x=0;
   y=r;
   do
   {
   delay(30);
   putpixel(300+x,300+y,14);
   putpixel(300+y,300+x,14);
   putpixel(300+y,300-x,14);
          putpixel(300+x,300-y,14);
   putpixel(300-x,300-y,14);
   putpixel(300-y,300-x,14);
   putpixel(300-y,300+x,14);
   putpixel(300-x,300+y,14);

   if(d)
             d=d+(4*x)+6;
   else
   {
       d=d+4*(x-y)+10;
       y=y-1;
   }
   x=x+1;
   delay(10);
      }while(x=getch());
   closegraph();
}

Comments

Popular posts from this blog

4. Lex and Yacc Program to detect errors in a 'C' Language Program

Lex and Yacc Program to detect errors in a 'C' Language Program   Lex Code : %{ #include"y.tab.h" #include<stdio.h> int LineNo = 1 ; %} identifier [ a - zA - Z ][ _a - zA - Z0 - 9 ]* number [ 0 - 9 ]+|([ 0 - 9 ]*\.[ 0 - 9 ]+) %% main \(\) return MAIN ; if return IF ; else return ELSE ; while return WHILE ; int | char | flaot return TYPE ; { identifier } return VAR ; { number } return NUM ; \> | \< | \<= | \>= | == return RELOP ; [\ t ] ; [\ n ] LineNo ++; . return yytext [ 0 ]; %% Yacc Code : %{ #include<string.h> #include<stdio.h> extern int LineNo ; int errno = 0 ; %} % token NUM VAR RELOP % token MAIN IF ELSE WHILE TYPE % left '-' '+' % left '*' '/' %% PROGRAM : MAIN BLOCK ; BLOCK : '{' CODE '}' ; CODE : BLOCK | STATEMENT CODE | STATEMENT ; STATEMENT : DECST ';' | DECST { printf ( "\nLine number %d...

Selenium + Python + UnexpectedAlertPresentException: Dealing with annoying alerts

Handling  UnexpectedAlertPresentException   Alerts who hates them? I Do!  Who doesn't hate an annoying alert causing your tests / scraping job to fail? I must say they are pretty much on point on the Unexpected part!  Fortunately, there are easy ways to mitigate the issue. 1. Disable alerts completely: driver . execute_script( 'window.alert = function(){};' ); execute this script just before where you anticipate the alert and you're golden. 2. You want to see the alert text but not disturb the execution flow. driver . execute_script( 'window.alert = console.info;' ); Now the alerts have been redirected to the console and you don't have to worry about them. (Unless you have to - then you'd have to monitor the console) 3. You know exactly when it comes and want to accept the alert and move on. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from selenium import webdriver from selenium.webdriver.s...

2. Lex program that detects statement type i.e. Simple or Compound

Lex program that detects statement type i.e. Simple or Compound Note: Only AND | OR | BUT conjunctions are supported. Program: % option noyywrap %{ char test = 's' ; %} %% ( "" [ aA ][ nN ][ dD ] "" )|( "" [ oO ][ rR ] "" )|( "" [ bB ][ uU ][ tT ] "" ) { test = 'c' ;} . {;} \ n return 0 ; %% main () { yylex (); if ( test == 's' ) printf ( "\n Its a simple sentence" ); else if ( test == 'c' ) printf ( "\n This is compound sentence" ); }