You are on page 1of 31

1.

Program to draw a teddy bear

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\BGI");
circle(275,100,75);
circle(275,275,100);
circle(240,80,15);
circle(300,80,15);
circle(270,105,10);
circle(270,140,20);
circle(215,25,20);
circle(330,22,20);
circle(212,225,20);
circle(338,225,20);
circle(212,360,20);
circle(338,360,20);
getch();
closegraph();
}
OUTPUT
2. Program to implement DDA Line Algorithm

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

void draw(int x1,int y1,int x2,int y2);

void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"..\\bgi");
printf("\n Enter the Co-ordinates of point A::");
scanf("%d%d",&x1,&y1);
printf("\n Enter the Co-ordinates of point B::");
scanf("%d%d",&x2,&y2);
printf("\n The AB Line is \n");
draw(x1,y1,x2,y2);
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}

OUTPUT
3. Program to implement Bresenham's Line algorithm

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void BLA(int x1,int y1,int xn,int yn)
{
int x=x1,y=y1,c=1;
float dx=xn-x1;
float dy=yn-y1;
float di=(2*dy)-dx;
float ds=2*dy;
float dt=2*(dy-dx);
putpixel(x,y,c++);
while(x<xn)
{
x++;
if(di<=0)
di=di+ds;
else
{
di=di+dt;
y++;
}
putpixel(x,y,c++);
}
}
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2;
initgraph(&gd,&gm,"..\\BGI");
printf("Enter first Co-ordinates::");
scanf("%d%d",&x1,&y1);
printf("Enter last Co-ordinates::");
scanf("%d%d",&x2,&y2);
BLA(x1,y1,x2,y2);
getch();
closegraph();
}
OUTPUT
4. Program to implement Bresenham's circle algorithm

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void drawcircle(int xc, int yc,int x,int y)
{
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
void main ()
{
int gd=DETECT,gm;
int r,x,y,p,xc,yc;
initgraph(&gd,&gm,"..\\BGI");
printf("Enter the Center Co-ordinates(Xc and Yc)::");
scanf("%d%d",&xc,&yc);
printf("Enter the radius::");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
while(x<y)
{
x++;
if(p<0)
p=p+(4*x)+6;
else
{
y--;
p=p+(4*(x-y)+10);
}
drawcircle(xc,yc,x,y);
}
getch();
closegraph();
}

OUTPUT
5. Program to implement Mid Point Circle Algorithm

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

void Drawcircle(int x1,int y1,int r)


{
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}

void cliplot(int xctr,int yctr,int x,int y)


{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,2);
putpixel(xctr +x,yctr -y,3);
putpixel(xctr -x,yctr -y,4);
putpixel(xctr +y,yctr +x,5);
putpixel(xctr -y,yctr +x,6);
putpixel(xctr +y,yctr -x,7);
putpixel(xctr -y,yctr -x,8);
getch();
}

void main()
{
int gd=DETECT,gm;
int x,y,r;
initgraph(&gd,&gm,"..\\BGI");
printf("Enter the Mid points(x and y) and Radius:");
scanf("%d%d%d",&x,&y,&r);
Drawcircle(x,y,r);
getch();
closegraph();
}

OUTPUT
6. Program to implement incremental line algorithm

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void DDALine(int x1,int y1,int xn,int yn)
{
int m=(yn-y1)/(xn-x1),dy,dx,i,c=1;
for(i=x1;i<=xn;i++)
{
if(m<=1)
{
dx=1;
dy=m*dx;
}
else
{
dy=1;
dx=dy/m;
}
x1=x1+dx;
y1=y1+dy;
putpixel(x1,y1,c++);
}
}
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2;
initgraph(&gd,&gm,"..\\BGI");
printf("Enter first Co-ordinates::");
scanf("%d%d",&x1,&y1);
printf("Enter last Co-ordinates::");
scanf("%d%d",&x2,&y2);
DDALine(x1,y1,x2,y2);
getch();
closegraph();
}
OUTPUT
7. Program to implement midpoint ellipse algorithm

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void disp();
float x,y;
float xc,yc;
void main ()
{
int gd=DETECT,gm,ec;
int a,b;
float p1,p2;
initgraph(&gd,&gm,"..\\BGI");
ec=graphresult();
if(ec!=grOk)
{
printf("Graphics Error::%s",ec);
printf("Press any key to halt!!");
getch();
exit(1);
}
printf("Enter the value of Xc and Yc::");
scanf("%f%f",&xc,&yc);
printf("Enter the value of a and b::");
scanf("%d%d",&a,&b);
x=0;
y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,4);
}
OUTPUT
8. Program to draw a rectangle using DDA Line Algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2,x3,y3,x4,y4;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"..\\bgi");
printf("\n Enter the Co-ordinates of point A::");
scanf("%d%d",&x1,&y1);
printf("\n Enter the Co-ordinates of point B::");
scanf("%d%d",&x2,&y2);
printf("\n Enter the Co-ordinates of point C::");
scanf("%d%d",&x3,&y3);
printf("\n Enter the Co-ordinates of point D::");
scanf("%d%d",&x4,&y4);
printf("\n The ABCD Rectangle is \n");
draw(x1,y1,x2,y2);
draw(x2,y2,x3,y3);
draw(x3,y3,x4,y4);
draw(x4,y4,x1,y1);
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}

OUTPUT
9. Program to implement Cohen Sutherland Line Clipping
Algorithm

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20

enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };

enum { FALSE, TRUE };


typedef unsigned int outcode;

outcode compute_outcode(int x, int y,


int xmin, int ymin, int xmax, int ymax)
{
outcode oc = 0;

if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;

if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;

return oc;
}

void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;

accept = FALSE;
done = FALSE;

outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);


outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do
{
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE;
}
else
{
double x, y;
int outcode_ex = outcode1 ? outcode1 : outcode2;
if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}

else if (outcode_ex & BOTTOM)


{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
}
else if (outcode_ex & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
}
else
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
}
else
{
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
}
}
} while (done == FALSE);

if (accept == TRUE)
line (x1, y1, x2, y2);
}

void main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;

printf ("Enter the number of lines to be clipped::");


scanf ("%d", &n);

printf ("Enter the x- and y-coordinates of the line-endpoints:\n");


for (i=0; i<n; i++)
for (j=0; j<4; j++)
scanf ("%d", &ln[i][j]);

printf ("Enter the x- and y-coordinates of the left-top and right-");


printf ("bottom corners\nof the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]);

initgraph (&gd, &gm, "..//bgi");

rectangle (clip[0], clip[1], clip[2], clip[3]);


for (i=0; i<n; i++)
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
getch();
cleardevice();
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
{
cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
clip[0], clip[1], clip[2], clip[3]);
getch();
}
closegraph();
}

OUTPUT
10. Program to translate a rectangle

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

void RectAngle(int x,int y,int Height,int Width)


{
line(x,y,x+Width,y);
line(x,y,x,y+Height);
line(x+Width,y,x+Width,y+Height);
line(x,y+Height,x+Width,y+Height);
}

void Translate(int x,int y,int Height,int Width)


{
int tx,ty,a,b;
printf("Enter the Translation unit::");
scanf("%d%d",&tx,&ty);
a=x+tx;
b=y+ty;
setcolor(2);
RectAngle(a,b,Height,Width);
}

void main()
{
int gd=DETECT,gm;
int x,y,Height,Width;
initgraph(&gd,&gm,"..//bgi");
printf("Enter the co-ordinates of First point for the Rectangle:");
scanf("%d%d",&x,&y);
printf("Enter the Height & Width for the Rectangle:");
scanf("%d%d",&Height,&Width);
RectAngle(x,y,Height,Width);
Translate(x,y,Height,Width);
getch();
}
OUTPUT
11. Menu Driven program to rotate a triangle and a line

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

void Line(int x1,int y1,int x2,int y2);


void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void RotateT(int x1,int y1,int x2,int y2,int x3,int y3);
void RotateL(int x1,int y1,int x2,int y2);

void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3,ch;
initgraph(&gd,&gm,"..\\bgi");
printf("_______MENU__________");
printf("\n1. Rotate a Line");
printf("\n2. Rotate a Triangle");
printf("\n3.Exit");
printf("\nenter your choice::");
scanf("%d",&ch);
switch(ch)
{

case 1:printf("Enter the 1st co-orinate points for the Line::");


scanf("%d%d",&x1,&y1);
printf("Enter the 2nd co-ordinate points for the Line:");
scanf("%d%d",&x2,&y2);
Line(x1,y1,x2,y2);
RotateL(x1,y1,x2,y2);
break;
case 2:printf("Enter the 1st co-ordinate points for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd co-ordinate points for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd co-ordinate points for the triangle:");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
RotateT(x1,y1,x2,y2,x3,y3);
break;
case 3:exit(1);
}
getch();
closegraph();
}

void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)


{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}

void Line(int x1,int y1,int x2,int y2)


{
line(x1,y1,x2,y2);
}

void RotateL(int x1,int y1,int x2,int y2)


{
int a1,b1,a2,b2,p=x1,q=y1;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
printf("After Rotation\n");
setcolor(2);
Line(a1,b1,a2,b2);
}

void RotateT(int x1,int y1,int x2,int y2,int x3,int y3)


{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("After Rotation");
setcolor(2);
TriAngle(a1,b1,a2,b2,a3,b3);
}

OUTPUT
15. Program to display an analog clock
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>

#define arg_sec M_PI/30


#define arg_hour M_PI/6
#define arg_min M_PI/360
void main()
{
int
gd=DETECT,gm,a,b,dig_sec,i,sec=0,hour,min,x1=0,y1=0,x2=0,y2=0,x3=0,y3=
0;
char *k[12]={"1","2","3","4","5","6","7","8","9","10","11","12"};
char Time_Dig[14];
struct time t;
initgraph(&gd,&gm,"..\\bgi");
setcolor(YELLOW);
circle(300,200,200);
circle(300,200,180);
setfillstyle(1,RED);
floodfill(300,390,YELLOW);
settextstyle(DEFAULT_FONT,0,2);
//----------------------Constants----------------------//

for(i=0;i<12;i++)
{
a=160*cos(arg_hour*i-M_PI_2);
b=160*sin(arg_hour*i-M_PI_2);
outtextxy(a+300,b+200,k[i]);
}

while(!kbhit())
{
settextstyle(7,0,4);
outtextxy(264,100,"Vinit");
settextstyle(7,0,1);
outtextxy(278,280,"Quartz");
setcolor(BLACK);
line(300,200,x1+300,y1+200);
line(300,200,x2+300,y2+200);
line(300,200,x3+300,y3+200);
gettime(&t);
if(sec!=t.ti_sec)
{
sound(5000);
delay(1);
nosound();
}
hour=t.ti_hour;
sec=t.ti_sec;
min=t.ti_min;
Time_Dig[0]=hour/10+48;
Time_Dig[1]=hour%10+48;
Time_Dig[2]=':';
Time_Dig[3]=min/10+48;
Time_Dig[4]=min%10+48;
Time_Dig[5]=':';
Time_Dig[6]=sec/10+48;
Time_Dig[7]=sec%10+48;
Time_Dig[8]='\0';
outtextxy(270,250,"ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ");
x1=150*cos(arg_sec*sec-M_PI_2)*0.98;
y1=150*sin(arg_sec*sec-M_PI_2)*0.98;
x2=150*cos(arg_sec*min-M_PI_2)*0.9;
y2=150*sin(arg_sec*min-M_PI_2)*0.9;
if(hour>12) hour-=12;
x3=150*cos(arg_hour*hour-M_PI_2+arg_min*min)*0.6;
y3=150*sin(arg_hour*hour-M_PI_2+arg_min*min)*0.6;
setcolor(YELLOW);
line(300,200,x1+300,y1+200);
setcolor(CYAN);
line(300,200,x2+300,y2+200);
setcolor(WHITE);
line(300,200,x3+300,y3+200);
setcolor(YELLOW);
outtextxy(270,250,Time_Dig);
delay(50);

getch();
closegraph();
restorecrtmode();
}

OUTPUT
16. Program to show Flying Balloons

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>

typedef struct balloon


{
int x;
int y;
int size;
int inc;
int color;
int tlen;
}balloon;

int no=0;
balloon stk[10];

void show(balloon a);


void binit();

void main()
{
int i,driver=DETECT,mode;
randomize();
initgraph(&driver,&mode,"..\\bgi"); while(!kbhit())
{
cleardevice();
binit();
for(i=0;i<no;i++)
{
stk[i].y=stk[i].y-stk[i].inc;
if(stk[i].y<=0)
stk[i].y=getmaxy();
show(stk[i]) ;
}
delay(60);
}
getch();
closegraph();
}

void binit()
{
while(no<10)
{
stk[no].x=random(getmaxx());
stk[no].size=random(15)+10;
stk[no].y=getmaxy()-stk[no].size-5;
stk[no].color=random(30);
stk[no].inc=random(8)+5;
stk[no].tlen=random(20)+15;
no++;
}
}

void show(balloon a)
{
setcolor(a.color);
circle(a.x,a.y,a.size);
setfillstyle(SOLID_FILL,a.color);
floodfill(a.x,a.y,a.color);
line(a.x, (a.y+a.size), a.x, (a.y+a.size+a.tlen));
}

OUTPUT

You might also like