Translate

Saturday, 31 December 2011

Program for draw a circle using Bresenham's Algorithm


# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>

void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;

initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();


printf("Enter the radius ");
scanf("%d",&r);


x=0;
y=r;

p=3-(2*r);

for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;

p=p+((4*(x-y)+10));
}

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);

}
getch();
closegraph();
}

Thursday, 29 September 2011

Loops:


       Loops are used to execute a set of statements again and again by writing once. Eg. we want to print ABC 100 times on the output scree then it will be better to use loops instead of writing cout<<"ABC"; 100 times in program.

Types of Loops:

1) for() 2) while()3) do-while()


1) for() loop:
  it execute statements till the condition is true. It ist checks the condition if  true then enters within the body of loop.

Syntax:

   for(initialization;condition;updating)

        {
         
           body

      }


2) while() loop:


      like for loop while loop also execute statements till the condition is true. Like for loop it also checks the condition ist, if true then enters within the body of loop, so we can say that for can be replaced with while and vice versa.



Syntax:

    initialization;
   while(condition)

        {
         
           body
    
           updating;
        }


3) do-while() loop:

  like for and while loops do-while also executes statements till the condition is true. But unlike other loops it ist execute the statements within the loop body and then checks the condition, so it always executes at least once.

Syntax:



   initialization;
  
    do
        {
         
           body
    
           updating;
        }
   while(condition);