0% found this document useful (0 votes)
74 views21 pages

Index: S.No. Program No. Signature

This document contains an index of computer graphics programs along with their page numbers. It then provides the source code for 10 programs: 1. A program to draw a line 2. A program to draw a line using Bresenham's algorithm 3. A program to draw a line using DDA algorithm 4. A program to draw a circle 5. A program to show animated text 6. A program to draw an ellipse 7. A program to simulate a bouncing ball 8. A program to draw a wheel 9. A program for rotation transformations 10. A program for translation transformations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views21 pages

Index: S.No. Program No. Signature

This document contains an index of computer graphics programs along with their page numbers. It then provides the source code for 10 programs: 1. A program to draw a line 2. A program to draw a line using Bresenham's algorithm 3. A program to draw a line using DDA algorithm 4. A program to draw a circle 5. A program to show animated text 6. A program to draw an ellipse 7. A program to simulate a bouncing ball 8. A program to draw a wheel 9. A program for rotation transformations 10. A program for translation transformations
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

INDEX

S.No.

Program

Page
No.

1.

Line

2.

Line (Bresenhems Algo)

3.

Line (DDA Algo)

4.

Animated Text

5.

Bouncing Ball

6.

Wheel

7.

Circle

8.

Ellipse

10

9.

Rotation

13

10.

Translation

17

Signature

Program to draw a line.


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int i;
int gm;
int gd=DETECT;
initgraph(& gd,& gm,"d:/tc/bgi");
setbkcolor(9);
setcolor(15);
line(0, 0, getmaxx(), getmaxy());
getch();
closegraph();
restorecrtmode();
}

Program to draw a line using Bresenhams Algorithm.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void swap(int *x,int *y)
{
int k;
k=x;
x=y;
y=k;
}
void main()
{
int gd=DETECT,gm=DETECT,x1,x2,y1,y2,dx,dy,p,k,x,y;
float m=0;
clrscr();
printf("Enter the sarting point x1 & y1n");
scanf("%d%d",&x1,&y1);
printf("Enter the end point x2 & y2n");
scanf("%d%d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
m=(float)(y2-y1)/(x2-x1);
initgraph(&gd,&gm,"d:/tc/bgi");
cleardevice();
if(fabs(m)>1)
{
swap(&x1,&y1);
swap(&x2,&y2);
swap(&dx,&dy);
}
if(x1>x2)
{
x=x2;
y=y2;
}
putpixel(x,y,15);
for(k=0;k<abs(dx);k++)
{
x++;

if(p<0)
{
p=p+2*dy;
}
else
{
y++;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,15);
}
getch();
}

Program to draw a line using DDAs Algorithm.


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,s,dx,dy,m,x1,y1,x2,y2;
float xi,yi,x,y;
clrscr();
printf("Enter the sarting point x1 & y1n");
scanf("%d%d",&x1,&y1);
printf("Enter the end point x2 & y2n");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"d:/tc/bgi");
cleardevice();
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/(float)s;
yi=dy/(float)s;
x=x1;
y=y1;
putpixel(x+0.5,y+0.5,12);
for(m=0;m<s;m++)
{
x+=xi;
y+=yi;
putpixel(x+0.5,y+0.5,12);
}
getch();
}

Program to draw a circle


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gmode,gdriver;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"d:/tc/bgi");
setcolor(9+128);
setbkcolor(4);
circle(320,220,110);;
outtextxy(300,250,"Computer Graphics");
getch();
closegraph();
}

Program to show animated text.


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int i;
int gm;
int gd=DETECT;
initgraph(& gd,& gm,"d:/tc/bgi");
setbkcolor(9);
setcolor(15);
for(i=10;i<200;i++)
{
outtextxy(i,35,"Computer Graphics");
delay(200);
clrscr();
}
getch();
closegraph();
restorecrtmode();
}

Program to draw an Ellipse


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#define ROUND(a) ((int)(a+0.5))
void ellipsemidpoint(int,int,int,int);
void main()
{
int gd=DETECT,gm,xc,yc,rx,ry;
printf("Enter the center nd semi major nd semi minor axis of ellipse as 'xc,yc,rx,ry'");
scanf("%d%d%d%d",&xc,&yc,&rx,&ry);
initgraph(&gd,&gm,"d:/tc/bgi");
cleardevice();
ellipsemidpoint(xc,yc,rx,ry);
}
void ellipsemidpoint(int xc,int yc,int rx,int ry)
{
long rx2=rx*rx;
long ry2=ry*ry;
long tworx2=2*rx2;
long twory2=2*ry2;
long p;
long x=0;
long y=ry;
long px=0;
long py=tworx2*y;
void ellipseplotpoint(int,int,long,long);
p=ROUND(ry2-(rx2*ry)+(0.25*rx2));
ellipseplotpoint(xc,yc,x,y);
while(px<py)
{
x++;
px+=twory2;
if(p<0)
p+=ry2+px;
else
{
y--;
py-=tworx2;
p+=ry2+px-py;

}
ellipseplotpoint(xc,yc,x,y);
}
p=ROUND(ry2*(x+0.5)*(x+0.25)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y--;
py-=tworx2;
if(p>0)
p+=rx2-py;
else
{
x++;
px+=twory2;
p+=rx2-py+px;
}
ellipseplotpoint(xc,yc,x,y);
}
getch();
}
void ellipseplotpoint(int xc,int yc,long x,long y)
{
putpixel(xc+x,yc+y,12);
delay(10);
putpixel(xc-x,yc+y,12);
delay(10);
putpixel(xc+x,yc-y,12);
delay(10);
putpixel(xc-x,yc-y,12);
delay(10);
}

Program to Draw a Bouncing ball

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm=DETECT;
int x,y=0,j,t=400,c=1;
initgraph(&gd,&gm,"");
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
for(x=40;x<602;x++)
{
cleardevice();
circle(x,y,30);
floodfill(x,y,RED);
delay(40);
if(y>=400)
{
c=0;
t-=20;
}
if(y<=(400-t))
c=1;
y=y+(c?15:-15);
}
getch();
}

Program to Draw a Wheel

#include<conio.h>
#include<graphics.h>
#include<math.h>
int L=0;
void ddaline(int x1,int y1,int x2,int y2)
{
int s,dx,dy,m,c=0;
float xi,yi,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/(float)s;
yi=dy/(float)s;
x=x1;
y=y1;
putpixel(x1+0.5,y1+0.5,15);
for(m=0;m<s;m++)
{
x+=xi;
y+=yi;
if(getpixel(x+0.5,y+0.5)==4)
{
c++;
continue;
}
if(c%2==0)
{
putpixel(x+0.5,y+0.5,15);

}
}
}

void plotpoints(int x,int y,int cx,int cy,int s)


{
if(s==0)
{
if(L%20==0)
{
ddaline(cx-x,cy-y,cx+x,cy+y);
ddaline(cx-y,cy+x,cx+y,cy-x);
ddaline(cx-y,cy-x,cx+y,cy+x);
ddaline(cx-x,cy+y,cx+x,cy-y);
}
L++;
}
putpixel(cx+x,cy+y,4);
putpixel(cx-x,cy+y,4);
putpixel(cx+x,cy-y,4);
putpixel(cx-x,cy-y,4);
putpixel(cx+y,cy+x,4);
putpixel(cx-y,cy+x,4);
putpixel(cx+y,cy-x,4);
putpixel(cx-y,cy-x,4);
}

void mcircle(int cx,int cy,int r,int s)


{
int x=0,y,p;
y=r;
p=1-r;
while(x<=y)
{
plotpoints(x,y,cx,cy,s);
x++;
if(p<0)
p+=2*x+1;

else
{
y--;
p+=2*(x-y)+1;
}
}
}
void main()
{
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"");
mcircle(300,250,20,1);
mcircle(300,250,100,0);
getch();
}

Program for Rotation

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
float x1,y1,x2,y2,x,y,x3,y3,x4,y4,a;
int ch;
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
clrscr();
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);
}
do
{
cout<<" #############MAIN-MENU###############\n";

cout<<" ROTATION\n";
cout<<" 1.LINE\n";
cout<<" 2.RECTANGLE\n";
cout<<" 3.TRIANGLE\n";
cout<<"enter your choice:0 for exit:\n";
cin>>ch;
switch(ch)

{
case 1: cout<<"enter the values of line coordinates:";
cin>>x1>>y1>>x2>>y2;
cout<<"enter the value for angle of rotation:";
cin>>a;
cleardevice();
line(x1,y1,x2,y2);
a=a*(3.14/180);
x1=(x1*cos(a))-(y1*sin(a));
y1=(x1*sin(a))+(y1*cos(a));
x2=(x2*cos(a))-(y2*sin(a));
y2=(x2*sin(a))+(y2*cos(a));
cout<<"now hit a key to see rotation:";
getch();
line(x1,y1,x2,y2);
break;

case 2: cout<<"enter the top left coordinates:";


cin>>x1>>y1;

cout<<"enter the bottom right coordinates:";


cin>>x2>>y2;
cout<<"enter the value for angle of rotation:";
cin>>a;
cleardevice();
rectangle(x1,y1,x2,y2);
a=a*(3.14/180);
x1=(x1*cos(a))-(y1*sin(a));
y1=(x1*sin(a))+(y1*cos(a));
x2=(x2*cos(a))-(y2*sin(a));
y2=(x2*sin(a))+(y2*cos(a));
cout<<"now hit a key to see rotation:";
getch();
rectangle(x1,y1,x2,y2);
break;
case 3: cout<<"enter coordinates of line1:\n";
cin>>x1>>y1>>x2>>y2;
cout<<"enter coordinates for relative line:\n";
cin>>x3>>y3;
cout<<"enter the angle of rotation:\n";cin>>a;
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x3,y3);
moveto(x3,y3);
lineto(x1,y1);
a=a*(3.14/180);

x1=(x1*cos(a))-(y1*sin(a));
y1=(x1*sin(a))+(y1*cos(a));
x2=(x2*cos(a))-(y2*sin(a));
y2=(x2*sin(a))+(y2*cos(a));
x3=(x3*cos(a))-(y3*sin(a));
y3=(x3*sin(a))+(y3*cos(a));
cout<<"now hit a key to see rotation:";
getch();
moveto(x1,y1);
lineto(x2,y2);
moveto(x2,y2);
lineto(x3,y3);
moveto(x3,y3);
lineto(x1,y1);
break;
case 0: break;
default:cout<<"invalid choice";break;
}}while(ch!=0);
getch();
closegraph();
return 0;
}

Program for Translation

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int x1,y1,x2,y2,x,y,x3,y3,x4,y4,ch;
int main(void)
{
int 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);
}
do
{
getch();
cleardevice();
cout<<" #############MAIN-MENU###############\n";
cout<<" TRANSLATION\n";

cout<<" 1.LINE\n";
cout<<" 2.RECTANGLE\n";
cout<<" 3.TRIANGLE\n";
cout<<"enter your choice:0 for exit:\n";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter the values of line coordinates:";
cin>>x1>>y1>>x2>>y2;
cout<<"enter the translation coordinates:";cin>>x>>y;
cleardevice();
line(x1,y1,x2,y2);
cout<<"now hit a key to see translation:";
getch();
line(x1+x,y1+y,x2+x,y2+y);
break;

case 2: cout<<"enter the top left coordinates:";


cin>>x1>>y1;
cout<<"enter the bottom right coordinates:";
cin>>x2>>y2;
cout<<"enter the values of translation coordinates:\n";
cin>>x>>y;
cleardevice();
rectangle(x1,y1,x2,y2);
cout<<"now hit a key to see translation:";
getch();

rectangle(x1+x,y1+y,x2+x,y2+y);
break;
case 3: cout<<"enter coordinates of line1:\n";
cin>>x1>>y1>>x2>>y2;
cout<<"enter coordinates for relative line:\n";
cin>>x3>>y3;
cout<<"enter translation coordinates:\n";cin>>x>>y;
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x3,y3);
moveto(x3,y3);
lineto(x1,y1);
cout<<"now hit a key to see translation:";
getch();
moveto(x1+x,y1+y);
lineto(x2+x,y2+y);
moveto(x2+x,y2+y);
lineto(x3+x,y3+y);
moveto(x3+x,y3+y);
lineto(x1+x,y1+y);
break;
case 0: break;
default:cout<<"invalid choice";break;
}}while(ch!=0);
getch();
closegraph();

return 0;}

You might also like