Cg Lab File Full Kantesh
Transcript of Cg Lab File Full Kantesh
-
7/31/2019 Cg Lab File Full Kantesh
1/37
S. NO. CONTENTS SIGN
1 W.A.P to Print a Pixel
2 W.A.P to draw some basic shapes using graphics.h
3 W.A.P to implement Digital Differential Analyzer
4 W.A.P to implement Generalized Bresenhams Algorithm for scan-
converting a line
5 W.A.P to implement Bresenhams Algorithm for scan-converting a
circle
6 W.A.P to implement mid-point algorithm for scan-converting an
ellipse
7 W.A.P to implement mid-point algorithm for scan-converting a
circle
8 W.A P to implement Cohan Southerlans Algorithm for line clipping
9 W.A.P to input the co-ordinates of an object and scale it using
scaling factors.
10 W.A.P to input the co-ordinates of an object and translate it using
translation factors
11 W.A.P to input the co-ordinates of an object and perform scaling
about any point
12 W.A.P to input the co-ordinates of any object and rotate that
object about origin or about any point depending upon the user.
INDEX
-
7/31/2019 Cg Lab File Full Kantesh
2/37
1. Program to Print a Pixel
#include
#include
main(){
int gd=DETECT, gm;
initgraph(&gd, &gm, "D:\\TC\\BGI");
putpixel(25,25,RED);
getch();
closegraph();
return 0;
}
Output
-
7/31/2019 Cg Lab File Full Kantesh
3/37
2. Program to draw some basic shapes using graphics.h.
#include
#include
main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };
initgraph(&gd, &gm, "");
circle(100,100,50);
outtextxy(75,170, "Circle");
rectangle(200,50,350,150);
outtextxy(240, 170, "Rectangle");
ellipse(500, 100,0,360, 100,50);
outtextxy(480, 170, "Ellipse");
line(100,250,540,250);
outtextxy(300,260,"Line");
sector(150, 400, 30, 300, 100,50);
outtextxy(120, 460, "Sector");
drawpoly(6, poly);
outtextxy(340, 460, "Polygon");
getch();
closegraph();
return 0;
}
-
7/31/2019 Cg Lab File Full Kantesh
4/37
Output
-
7/31/2019 Cg Lab File Full Kantesh
5/37
3. Program to implement Digital Differential Analyzer.
#include
#include
#include #include
int main()
{
int gd=DETECT,gm;
float xs,xe,ys,ye,x_inc,y_inc;
int x,y,length;
initgraph(&gd,&gm,"C:\\TC\\BGI");
coutxs;
cout
-
7/31/2019 Cg Lab File Full Kantesh
6/37
-
7/31/2019 Cg Lab File Full Kantesh
7/37
4) W.A.P to implement Generalized Bresenhams Algorithm for scan-converting aline.
#include
#include#include
#include
void main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int x1,x2,y1,y2,x,y,dx,dy,sx,sy,steps,t,pk,flag;
coutx1>>y1>>x2>>y2;dx=abs(x2-x1);
dy=abs(y2-y1);
if((x2-x1)>0)
sx=1;
else
sx=-1;
if((y2-y1)>0)
sy=1;
else
sy=-1;
pk=2*dy-dx;
x=x1;
y=y1;
putpixel(x,y,BLUE);
if(dy>dx)
{
steps=dy;
flag=1;
t=dx;
dx=dy;
dy=t;
}
else
{
steps=dx;
flag=0;
}
-
7/31/2019 Cg Lab File Full Kantesh
8/37
pk=2*dy-dx;
for(int i=1;i0)
{if(flag==1)
x=x+sx;
else
y=y+sy;
pk=pk-2*dx;
}
if(flag==1)
y=y+sy;
elsex=x+sy;
pk=pk+2*dy;
putpixel(x,y,BLUE);
}
getch();
closegraph();
}
-
7/31/2019 Cg Lab File Full Kantesh
9/37
OUTPUT:-
-
7/31/2019 Cg Lab File Full Kantesh
10/37
5) W.A.P to implement Bresenhams Algorithm for scan-converting a circle.#include
#include
#include#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int r;
cout
-
7/31/2019 Cg Lab File Full Kantesh
11/37
OUTPUT:-
-
7/31/2019 Cg Lab File Full Kantesh
12/37
6) W.A.P to implement mid-point algorithm for scan-converting an ellipse.
#include
#include
#include
#include
void main()
{
clrscr();
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int a,b,x,y,p1,p2;
coutb;
x=0;
y=b;
putpixel(x+200,y+300, WHITE);
p1=(b*b)-(a*a*b)+((1/4)*(a*a));
while((2*b*b*x)
-
7/31/2019 Cg Lab File Full Kantesh
13/37
{
x+=1;
y-=1;
p2=p2+(2*b*b*x)-(2*a*a*y)+(a*a);
}
putpixel(x+200,y+300, WHITE);
putpixel(-x+200,y+300, WHITE);
putpixel(x+200,-y+300, WHITE);
putpixel(-x+200,-y+300, WHITE);
}
getch();
}
-
7/31/2019 Cg Lab File Full Kantesh
14/37
OUTPUT:-
-
7/31/2019 Cg Lab File Full Kantesh
15/37
7) W.A.P to implement mid-point algorithm for scan-converting a circle.
#include
#include
#include
#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int r;
cout
-
7/31/2019 Cg Lab File Full Kantesh
16/37
OUTPUT:-
-
7/31/2019 Cg Lab File Full Kantesh
17/37
Q8 W.A P to implement Cohan Southerlans Algorithm for line clipping.
#include
#include
#include
#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int t,b,r,l;
float x1,y1,x2,y2,xi,yi,xp,yp;
int b1=0,b2=0,b3=0,b4=0,b5=0,b6=0,b7=0,b8=0;
coutb>>r>>t;
rectangle(l,b,r,t);couty1>>x2>>y2;
line(x1,y1,x2,y2);
cout
-
7/31/2019 Cg Lab File Full Kantesh
18/37
b7=1;
else
b7=0;
if(x2
-
7/31/2019 Cg Lab File Full Kantesh
19/37
xp=l;
yp=y2+m*(xp-x2);
}
if(b7==1)
{
xp=r;
yp=y2+m*(xp-x2);
}
if(b6==1)
{
yp=b;
xp=x2+(yp-y2)/m;
}
if(b5==1)
{
yp=t;xp=x2+(yp-y2)/m;
}
line(xi,yi,xp,yp);
}
getch();
closegraph();
return(0);
}
OUTPUT:-
-
7/31/2019 Cg Lab File Full Kantesh
20/37
-
7/31/2019 Cg Lab File Full Kantesh
21/37
-
7/31/2019 Cg Lab File Full Kantesh
22/37
-
7/31/2019 Cg Lab File Full Kantesh
23/37
Q9 W.A.P to input the co-ordinates of an object and scale it using scaling factors.
#include
#include
#include#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int x1,y1,x2,y2,x3,y3;
float sx,sy;
int i,j;
int q,w,e,r,t,y;
int m=0;
couty1>>x2>>y2>>x3>>y3;
int a[2][3];
float b[2][2];
int c[2][3];
a[0][0]=x1;
a[1][0]=y1;
a[0][1]=x2;
a[0][2]=x3;a[1][1]=y2;
a[1][2]=y3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
for(i=0;i
-
7/31/2019 Cg Lab File Full Kantesh
24/37
for(int l=0;l
-
7/31/2019 Cg Lab File Full Kantesh
25/37
-
7/31/2019 Cg Lab File Full Kantesh
26/37
Q10 W.A.P to input the co-ordinates of an object and translate it using translation
factors.
#include#include
#include
#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int x1,y1,x2,y2,x3,y3;
float tx,ty;
int i,j;
int q,w,e,r,t,y;
int m=0;
couty1>>x2>>y2>>x3>>y3;
int a[3][3];
float b[3][3];
int c[3][3];
a[0][0]=x1;
a[1][0]=y1;a[0][1]=x2;
a[0][2]=x3;
a[1][1]=y2;
a[1][2]=y3;
a[2][0]=1;
a[2][1]=1;
a[2][2]=1;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
for(i=0;i
-
7/31/2019 Cg Lab File Full Kantesh
27/37
b[0][2]=tx;
b[1][0]=0;
b[1][1]=1;
b[1][2]=ty;
b[2][0]=0;b[2][1]=0;
b[2][2]=1;
for(int k=0;k
-
7/31/2019 Cg Lab File Full Kantesh
28/37
OUTPUT:-
-
7/31/2019 Cg Lab File Full Kantesh
29/37
11) W.A.P to input the co-ordinates of an object and perform scaling about any point.
#include
#include
#include
#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int x1,y1,x2,y2,x3,y3;
int i,j,m,q,e,w,r,t,z;
float sx,sy,x,y;
couty1>>x2>>y2>>x3>>y3;int a[3][3];
float b[3][3];
int c[3][3];
a[0][0]=x1;
a[0][1]=x2;
a[0][2]=x3;
a[1][0]=y1;
a[1][1]=y2;
a[1][2]=y3;
a[2][0]=1;
a[2][1]=1;
a[2][2]=1;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
for(i=0;i
-
7/31/2019 Cg Lab File Full Kantesh
30/37
b[1][2]=y-(y*sy);
b[2][0]=0;
b[2][1]=0;
b[2][2]=1;
for(i=0;i
-
7/31/2019 Cg Lab File Full Kantesh
31/37
-
7/31/2019 Cg Lab File Full Kantesh
32/37
12) W.A.P to input the co-ordinates of any object and rotate that object about origin or
about any point depending upon the user.
#include
#include
#include
#include
int main()
{
int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int x1,y1,x2,y2,x3,y3;
int i,j,m,q,e,w,r,t,z,ch,s=0;
float sx,x,y;
couty1>>x2>>y2>>x3>>y3;
int a[3][3];
int n[2][3];
float b[3][3];
float d[2][2];
int c[3][3];
int f[2][3];
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout
-
7/31/2019 Cg Lab File Full Kantesh
33/37
cout
-
7/31/2019 Cg Lab File Full Kantesh
34/37
cout
-
7/31/2019 Cg Lab File Full Kantesh
35/37
c[i][j]+=(b[i][m]*a[m][j]);
}
}
}
q=c[0][0];
w=c[1][0];
e=c[0][1];
r=c[0][2];
t=c[1][1];
z=c[1][2];
line(q,w,e,t);
line(e,t,r,z);
line(r,z,q,w);
cout
-
7/31/2019 Cg Lab File Full Kantesh
36/37
-
7/31/2019 Cg Lab File Full Kantesh
37/37