You are on page 1of 23

Write a program to draw a circle using Bresenham‘s algorithm.

Bresenham's Circle Algorithm:


Step1: Start Algorithm
Step2: Declare p, q, x, y, r, d variables p, q are coordinates of the center of the
circle r is the radius of the circle
Step3: Enter the value of r
Step4: Calculate d = 3 - 2r
Step5: Initialize x=0 &nbsy= r
Step6: Check if the whole circle is scan converted If x > = y Stop
Step7: Plot eight points by using concepts of eight-way symmetry. The center
is at

(p, q). Current active pixel is (x, y).


putpixel (x+p, y+q)
putpixel (y+p, x+q)
putpixel (-y+p, x+q)
putpixel (-x+p, y+q)
putpixel (-x+p, -y+q)
putpixel (-y+p, -x+q)
putpixel (y+p, -x+q)
putpixel (x+p, -y-q)

Step8: Find location of next pixels to be scanned


If d < 0
then d = d + 4x + 6
increment x = x + 1 If d ≥ 0
then d = d + 4 (x - y) + 10
increment x = x + 1
decrement y = y – 1
Step9: Go to step 6
Step10: Stop Algorithm
Program to draw a circle using Bresenham's circle drawing algorithm:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void BPlot(int xc,int yc,int x,int y)
{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}
void BresenhamCircle(int xc,int yc,int r)
{
int x=0,y=r,d=3-(2*r);
BPlot(xc,yc,x,y);
while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
BPlot(xc,yc,x,y);
}
}
void main(void)
{
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r);
BresenhamCircle(xc,yc,r);
getch();
closegraph();
}
Output:
Program to draw a rectangle using DDA line drawing algorithm.
DDA Algorithm:
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy) Then step = abs (dx) Else
Step7: xinc=dx/step yinc=dy/step assign x = x1 assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc y = y + yinc Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
Program:
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define ROUND(x) ((int)(x+0.5))
void ddaline(int x1, int y1, int x2, int y2, int c)
{
int dx = x2 - x1;
int dy = y2 - y1;
int steps;
if(abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
float x_inc = dx / (float)steps;
float y_inc = dy / (float)steps;
int x = x1, y = y1;
putpixel(x, y, c);
for (int i = 0; i < steps; ++i)
{
x += x_inc;
y += y_inc;
putpixel(ROUND(x), ROUND(y), c);
}
}
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "O:\\bgi");
ddaline(10, 10, 50, 10, 111);
ddaline(10, 10, 10, 50, 111);
ddaline(50, 10, 50, 50, 111);
ddaline(10, 50, 50, 50, 111);
getch();
}
Output:
Write a Program to make a moving colored car using inbuilt
functions algorithm.
Step1:start algorithm

Step2:graphics.h library installed

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

Step3:Function to draw moving car

void draw_moving_car(void)
{
  
     int i, j = 0, gd = DETECT, gm;
step4:function to initialize graphics mode
 initgraph(&gd, &gm, "");
  
     for (i = 0; i <= 420; i = i + 10)
{
Step5:Set color of car as red
 setcolor(RED);
  
step6:Thease lines for bonnet and body of car
 line(0 + i, 300, 210 + i, 300);
        line(50 + i, 300, 75 + i, 270);
        line(75 + i, 270, 150 + i, 270);
        line(150 + i, 270, 165 + i, 300);
        line(0 + i, 300, 0 + i, 330);
        line(210 + i, 300, 210 + i, 330);
step7:For left wheel of car

  circle(65 + i, 330, 15);


        circle(65 + i, 330, 2);
step8:For right wheel of car
 circle(145 + i, 330, 15);
        circle(145 + i, 330, 2);
step9:Line left of left wheel
line(0 + i, 330, 50 + i, 330);
step10:Line middle of both wheel
 line(80 + i, 330, 130 + i, 330);
step11:Line right of right wheel
 line(210 + i, 330, 160 + i, 330);
  
        delay(100);
  
step12: To erase previous drawn car, draw
the whole car at same possition
but color using black.

 setcolor(BLACK);
step13:Lines for bonnet and body of car
  line(0 + i, 300, 210 + i, 300);
        line(50 + i, 300, 75 + i, 270);
        line(75 + i, 270, 150 + i, 270);
        line(150 + i, 270, 165 + i, 300);
        line(0 + i, 300, 0 + i, 330);
        line(210 + i, 300, 210 + i, 330);
step14: For left wheel of car
        circle(65 + i, 330, 15);
        circle(65 + i, 330, 2);
  
        step15: For right wheel of car
        circle(145 + i, 330, 15);
        circle(145 + i, 330, 2);
  
step16: Line left of left wheel
        line(0 + i, 330, 50 + i, 330);
step17:Line middle of both wheel
         line(80 + i, 330, 130 + i, 330);
  
step18:Line right of right wheel
         line(210 + i, 330, 160 + i, 330);
   }
  
     getch();
  
     closegraph();
}
Step19:Driver code
int main()
{
     draw_moving_car();
  
     return 0;
}
Step20:stop algorithm
Program:
#include<graphics.h>  
#include<conio.h>  
int main()  
{  
    intgd=DETECT,gm, i, maxx, cy;  
    initgraph(&gd, &gm, "C:\\TC\\BGI");  
    setbkcolor(WHITE);  
    setcolor(RED);  
    maxx = getmaxx();  
    cy = getmaxy()/2;  
    for(i=0;i<maxx-140;i++)  
        {  
        cleardevice();  
        line(0+i,cy-20, 0+i, cy+15);  
        line(0+i, cy-20, 25+i, cy-20);  
        line(25+i, cy-20, 40+i, cy-70);  
        line(40+i, cy-70, 100+i, cy-70);  
        line(100+i, cy-70, 115+i, cy-20);  
        line(115+i, cy-20, 140+i, cy-20);  
        line(0+i, cy+15, 18+i, cy+15);  
        circle(28+i, cy+15, 10);  
        line(38+i, cy+15, 102+i, cy+15);  
        circle(112+i, cy+15,10);  
        line(122+i, cy+15 ,140+i,cy+15);  
        line(140+i, cy+15, 140+i, cy-20);  
        rectangle(50+i, cy-62, 90+i, cy-30);  
        setfillstyle(1,BLUE);  
        floodfill(5+i, cy-15, RED);  
        setfillstyle(1, LIGHTBLUE);  
        floodfill(52+i, cy-60, RED);  
        delay(10);  
         }  
    getch();  
    closegraph();  
    return 0;  
}  

          

  

  
Output:
Write a program to draw a pentagon using bresenham's line
drawing algorithm.
Algorithm:
1. Declare all variables including graphics variables and polygon
array.
2. Initialize all variables.
3. Initialize the graph while the path is set to the graphics driver.
4. Assign values to polygon array in pairs.
5. Use drawpoly( ) function to draw the polygon shape.
6. Close the graph.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main()
{
int gd=DETECT, gm;

int pentagon[12]={340,150, 320,110, 360,70, 400,110, 380,150, 340,150};


initgraph(&gd, &gm, "c:\\turboc3\\bgi");

outtextxy(150,15, "Polygon Drawing in Graphics.h using drawpoly() & fillpoly().");

drawpoly(6,pentagon);
outtextxy(330, 160, "Pentagon");

getch();
closegraph();
return 0;

}
Output:
write a program to draw a hexagon using generalized bresenham's
line drawing algorithm.
Algorithm:
1) move out: ε1=ε0+k*s
1a) decide where to move: ε1<=0.5, so we will move to the R hex
2) color hex (1,0), and increase ε once more: ε2=ε1+k*s. Now hex (1,0) is the current one
3) move out: ε3=ε2+k*s
3a) decide where to move: ε3>0.5, so we will move to the UR hex
4) color hex (1,1), and adjust ε accordingly: ε4=ε3-1.5. Now hex (1,1) is current, and ε is
negative (thus shown in red)
5) move out: ε5=ε4+k*s, ε is still negative (and will remain such till the final hex)
5a) decide where to move: ε5<=0.5, hence R hex is next
6) color hex (2,1), and increase ε once more: ε6=ε5+k*s. Now hex (2,1) is current
7) move out: ε7=ε6+k*s
7a) decide where to move: ε7<=0.5, hence R hex is next
8) color hex (3,1), and we are done

Program:
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

int main()

int gd=DETECT, gm;

int hexagon[14] ={ 360,260, 340,240, 360,220, 400,220, 420,240, 400,260, 360,260};

initgraph(&gd, &gm, "c:\\turboc3\\bgi");

outtextxy(150,15, "Polygon Drawing in Graphics.h using drawpoly() & fillpoly().");

fillpoly(7,hexagon);

outtextxy(362, 262, "Hexagon");

getch();

closegraph();

return 0;
}

Output:
Writte a program to perform the following sequencial transformation
on a square.
1.rotate the square by an angle of 90 degree w.r.t origin.
Algorithm:
 Initial co-ordinates of the object O = (Xold, Yold)
 Initial angle of the object O with respect to origin = Φ
 Rotation angle = θ
 New co-ordinates of the object O after rotation = (Xnew, Ynew)

Program:
#include<stdio.h>  
#include<conio.h>  
#include<graphics.h>  
#include<math.h>  
int x1,y1,x2,y2,x3,y3,a,b;  
void draw();  
void rotate();  
int main(void)  
{  
int gd=DETECT,gm;  
initgraph(&gd,&gm,"C:\\TC\\BGI");  
printf("Enter first co-ordinate value for triangle:");  
scanf("%d%d",&x1,&y1);  
printf("Enter second co-ordinatevalues for triangle:");  
scanf("%d%d",&x2,&y2);  
printf("Enter third co-ordinate valuesfor triangle:");  
scanf("%d%d",&x3,&y3);  
draw();  
getch();  
rotate();  
getch();  
  
return 0;  
}  
  
void draw()  
{  
  line(x1,y1,x2,y2);  
  line(x2,y2,x3,y3);  
  line(x3,y3,x1,y1);  
}  
 void rotate()  
 {  
    int a1,a2,a3,b1,b2,b3;  
    float angle;  
    printf("Enter the rotation angle co-ordinates:");  
    scanf("%f",&angle);  
    cleardevice();  
      angle=(angle*3.14)/180;  
      a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);  
      b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);  
      a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);  
      b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);  
      a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);  
      b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);  
      printf("ROTATION");  
      printf("\n Changed coordinates\n");  
      printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);  
    line(a1,b1,a2,b2);  
    line(a2,b2,a3,b3);  
    line(a3,b3,a1,b1);  
 }  
Output:
To write a program to draw the various attributes of line, circle and ellipse.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the variables.
Step 3: Call the initgraph() function
Step 4: Set color for the output primitives.
Step 5: Using Outtextxy() display the chosen particular primitives.
Step 6: Include the various attributes of line, circle and ellipse.
Step 7: close the graph and run the program.
Step 8: stop the program

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
char ch='y';
int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");
while(ch=='y')
{
cleardevice();
setbkcolor(9);
outtextxy(100,130,"Choose From The Following ");
outtextxy(100,150,"1. Line");
outtextxy(100,170,"2.Circle");
outtextxy(100,190,"3.Box");
outtextxy(100,210,"4.Arc");
outtextxy(100,230,"5.Ellipse");
outtextxy(100,250,"6.Rectangle");
outtextxy(100,270,"7.Exit");
ch=getch();
cleardevice();
switch(ch)
{
case '1':
line(100,200,300,400);
break;
case '2':
circle(200,200,100);
break;
case '3':
setfillstyle(5,4);
bar(100,300,200,100);
break;
case '4':
setfillstyle(5,4);
arc(200,200,100,300,100);
break;
case '5':
setfillstyle(5,4);
fillellipse(100,100,50,100);
break;
case '6':
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"AMS COLLEGE ");
line(100,100,100,300);
line(300,300,100,300);
line(100,100,300,100);
line(300,100,300,300);
break;
case '7':
closegraph();
return;
}
ch='y';
getch();
}
}
OUTPUT:
Choose from the following
1.Line
2. Circle
3.Box
4.Arc
5.Ellipse
6.Rectangle
7.Exit

LINE

CIRCLE

ARC

You might also like