Computer Graphics(160703) - · PDF fileComputer Graphics(160703) Name ... 4 Write a program...
Transcript of Computer Graphics(160703) - · PDF fileComputer Graphics(160703) Name ... 4 Write a program...
VENUS INTERNATIONAL COLLEGE OF TECHNOLOGY Gandhinagar
Department of Computer Enggineering
Computer Graphics(160703)
Name :
Enroll no.
:
Class
: 6th SEM C.E.
Year
: 2014-15
VENUS INTERNATIONAL COLLEGE OF TECHNOLOGY
Gandhinagar
CERTIFICATE
This is certify that
Mr./Ms.________________________________Roll No. ___________________ of B.E Third Year 6th Sem C.E. has satisfactory completed the course in Computer Graphics.
_______________________ ____________________ Signature of Faculty Signature of HOD
DATE OF SUBMISSION : / / 2015
Index
Sr. No. Description Page No.
Date Sign
1 Write a program for Smiley. 1 / /15 2 Write a program for DDA Line drawing algorithm . 2 / /15 3
Write a program for Bresenham's Line Drawing
Algorithm. 4 / /15
4 Write a program for Midpoint Circle Algorithm. 6 / /15 5 Write a program for Midpoint ellipse Algorithm. 8 / /15 6 Write a program for 2D Translation Triangle Program. 10 / /15 7 Write a program for 2D Rotation of Triangle Program. 12 / /15 8 Write a program for 2D Scaling Triangle Program. 14 / /15 9 Write a program for Character generation. 16 / /15
10 Write a program for draw a moving person. 17 / /15 11 Project - I 19 / /15
Computer Graphics VICT CE 2014-15 1
Practical – 1
AIM : Write a program for Smiley.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
clrscr();
printf("Smiley");
circle(320,240,200);
circle(250,180,28);
circle(400,180,28);
arc(320,250,180,0,120);
line(300,280,350,280);
getch();
}
Output :
Computer Graphics VICT CE 2014-15 2
Practical – 2
AIM : Write a program for DDA Line drawing algorithm .
Program :
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#define round(val) (int)(val+0.5)
void main()
{
intgd=DETECT,gm;
voidline_dda(int,int,int,int);
intxa,xb,ya,yb;
printf("Enter the two values");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb); initgraph(&gd,&gm,"");
cleardevice();
line_dda(xa,ya,xb,yb);
getch();
closegraph();
}
voidline_dda(intxa,intya,intxb,intyb)
{
intDx=xb-xa,Dy=yb-ya,steps,k;
floatxin,yin,X=xa,Y=ya;
if(abs(Dx)>abs(Dy))
steps=abs(Dx);
else
steps=abs(Dy);
xin=Dx/(float)steps;
yin=Dy/(float)steps;
putpixel(round(X),round(Y),6);
for(k=0;k<steps;k++)
{
X=X+xin;
Y=Y+yin;
putpixel(round(X),round(Y),6);
}
}
Computer Graphics VICT CE 2014-15 3
OUTPUT:
Computer Graphics VICT CE 2014-15 4
Practical – 3
AIM : Write a program for Bresenham's Line Drawing Algorithm.
Program :
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2;
intgd=DETECT,gm;
voidlinebres(int,int,int,int);
printf("Enter the two end points:");
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
initgraph(&gd,&gm,"");
cleardevice();
linebres(x1,y1,x2,y2);
getch();
line(x1,y1,x2,y2);
getch();
closegraph();
}
voidlinebres(int x1,int y1,int x2,int y2)
{
int dx=abs(x1-x2),dy=abs(y1-y2);
intp,x,y,i,xend,yend;
if(dx!=0)
{
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,2);
for(i=x;i<xend;i++)
{
x+=1;
Computer Graphics VICT CE 2014-15 5
if(p<0)
p+=2*dy;
else
p+=2*(dy-dx);
}
putpixel(x,y,2);
}
else
{
p=2*dx-dy;
if(y1>y2)
{
x=x2;
y=y2;
yend=y2;
}
putpixel(x,y,2);
for(i=y;i<yend;i++)
{
y+=1;
if(p<0)
p+=2*dx;
else
{
x+=1;
p+=2*(dx-dy);
}
putpixel(x,y,2);
}
}
}
OUTPUT:
Computer Graphics VICT CE 2014-15 6
Practical – 4
AIM : Write a program for Midpoint Circle Algorithm.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
intx,y,r;
voidDrawcircle(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"");
Drawcircle(x,y,r);
getch();
closegraph();
}
voidDrawcircle(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
voidcliplot(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);
}
}
Computer Graphics VICT CE 2014-15 7
voidcliplot(intxctr,intyctr,intx,int y)
{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y,yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
}
OUTPUT:
Computer Graphics VICT CE 2014-15 8
Practical – 5
AIM : Write a program for Midpoint ellipse Algorithm.
Program :
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
long d1,d2;
int i,gd,gm,x,y,x0,y0;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
clrscr();
printf("Enter the X radius and Y radius of the ellipse:\n");
scanf("%ld%ld",&rx,&ry);
printf("\nEnter the center (x,y) of the ellipse:\n");
scanf("%d%d",&x0,&y0);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\BGI");
cleardevice();
rxsq=rx*rx;
rysq=ry*ry;
tworxsq=2*rxsq;
tworysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
{
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+tworysq;
d1=d1+dx+rysq;
}
Computer Graphics VICT CE 2014-15 9
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d1=d1+dx-dy+rysq;
}
delay(10);
}
while(dx<dy);
d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-(rxsq*rysq);
do
{
putpixel(x0+x,y0+y,15);
putpixel(x0-x,y0-y,15);
putpixel(x0+x,y0-y,15);
putpixel(x0-x,y0+y,15);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d2=d2+dx-dy+rxsq;
}
}
while(y>0);
getch();
closegraph();
}
OUTPUT :
Computer Graphics VICT CE 2014-15 10
Practical – 6
AIM : Write a program for 2D Translation Triangle Program.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void tri();
void main()
{
intgd=DETECT,gm;
int c;
initgraph(&gd,&gm,"d:\\tc\\bgi ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
getch();
tri();
getch();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
Computer Graphics VICT CE 2014-15 11
void tri()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Transaction coordinates");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
OUTPUT :
Computer Graphics VICT CE 2014-15 12
Practical – 7
AIM : Write a program for 2D Rotation of Triangle Program.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
void draw (int x1,int y1,int x2,int y2,int x3,int y3);
//int x1,y1,x,y,sx,i,j;
void main()
{
int x1,y1,x2,y2,x3,y3,x,y;
intgdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"d:\\tc\\bgi");
printf("\n Enter line1 co ordinates");
scanf("%d%d",&x1,&y1);
printf("\n Enter line2 co ordinates");
scanf("%d%d",&x2,&y2);
printf("\n Enter line3 co ordinates");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
draw(x1,y1,x2,y2,x3,y3);
getch();
}
void draw(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,p,q,d,a1,b1 ,a2,b2,a3,b3;
float a;
printf("\n Enter the rotation angle");
scanf("%d",&d);
//rotation point
p=x2;
q=y2;
//convert degree to radian
a=((d*3.14)/180);
a1=p+(x1-p)*cos(a)-(y1-q)*sin(a);
b1=p+(y1-p)*sin(a)+(x1-q)*cos(a);
a2=p+(x2-p)*cos(a)-(y2-q)*sin(a);
b2=p+(y2-p)*sin(a)+(x2-q)*cos(a);
a3=p+(x3-p)*cos(a)-(y3-q)*sin(a);
b3=p+(y3-p)*sin(a)+(x3-q)*cos(a);
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
Computer Graphics VICT CE 2014-15 13
OUTPUT:
Computer Graphics VICT CE 2014-15 14
Practical – 8
AIM : Write a program for 2D Scaling Triangle Program.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
intgd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
intmx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
Computer Graphics VICT CE 2014-15 15
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
OUTPUT:
Computer Graphics VICT CE 2014-15 16
Practical – 9
AIM : Write a program for Character generation.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int
a[7][5]={{0,0,1,0,0},{0,1,0,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,1},{1,0,0,0,1},{1,0,0,0,1}};
inti,j;
intgd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
for(i=0;i<7;i++)
{
for(j=0;j<5;j++)
{
putpixel(200+j,200+i,5*a[i][j]);
}
}
getch();
closegraph();
}
OUTPUT:
Computer Graphics VICT CE 2014-15 17
Practical – 10
AIM : Write a program for draw a moving person.
Program :
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<DOS.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
int x,y,xm;
printf("enter x,y");
scanf("%d%d",&x,&y);
xm=getmaxx();
circle(x,y,50);
ellipse(x-25,y-15,0,360,3,2);
ellipse(x+25,y-15,0,360,3,2);
line(x,y-10,x,y+10);
arc(x,y+15,180,360,15);
line(x,y+50,x,y+130);
line(x,y+130,x-30,y+180);
line(x-30,y+180,x-50,y+170);
line(x,y+130,x+30,y+180);
line(x+30,y+180,x+50,y+170);
line(x,y+75,x+75,y+75);
line(x+75,y+75,x+170,y-10);
ellipse(x+170,y-50,0,360,28,40);
while(!kbhit())
{
if(x<=xm)
{
cleardevice();
x=x+20;
circle(x,y,50);
ellipse(x-25,y-15,0,360,3,2);
ellipse(x+25,y-15,0,360,3,2);
line(x,y-10,x,y+10);
arc(x,y+15,180,360,15);
line(x,y+50,x,y+130);
line(x,y+130,x-30,y+180);
line(x-30,y+180,x-50,y+170);
line(x,y+130,x+30,y+180);
line(x+30,y+180,x+50,y+170);
line(x,y+75,x+75,y+75);
Computer Graphics VICT CE 2014-15 18
line(x+75,y+75,x+170,y-10);
ellipse(x+170,y-50,0,360,28,40);
delay(100);
}
else
{
do
{
cleardevice();
x=x-20;
circle(x,y,50);
ellipse(x-25,y-15,0,360,3,2);
ellipse(x+25,y-15,0,360,3,2);
line(x,y-10,x,y+10);
arc(x,y+15,180,360,15);
line(x,y+50,x,y+130);
line(x,y+130,x-30,y+180);
line(x-30,y+180,x-50,y+170);
line(x,y+130,x+30,y+180);
line(x+30,y+180,x+50,y+170);
line(x,y+75,x+75,y+75);
line(x+75,y+75,x+170,y-10);
ellipse(x+170,y-50,0,360,28,40);
delay(100);
}while(x!=0);
}
}
getch();
closegraph();
}
OUTPUT :