Professional Documents
Culture Documents
#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 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 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
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;
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;
OUTPUT
10. Program to translate a rectangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
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 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)
{
OUTPUT
15. Program to display an analog clock
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
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>
int no=0;
balloon stk[10];
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