CG Lab (With Op)
CG Lab (With Op)
Aim : To write a C Program for Line Drawing using Digital Differential Analyzer (DDA)
Method.
Algorithm
1. Start
2. Read the two end points of a line (x1, y1) & (x2, y2).
3. ∆x = x2-x1 & ∆y = y2-y1
4. Initialize (x, y) with (x1,y1)
x = x1
y = y1
5. if abs(∆ x) > abs(∆y) then
steps =∆x
else
steps=∆y
6. xincrement =∆x / steps.
7. yincrement =∆y /steps
8. Plot the rounded coordinate (x, y)
9. Initialize counter k=1
10. Start the loop
x = x + xincrement
y = y+ yincrement
11. Plot the rounded coordinate(x, y)
12. Continue the loop till the counter k= steps
13. Stop.
Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2,dx,dy,steps,k;
float xincrement,yincrement,x,y;
initgraph(&gd, &gm, "..\\bgi");
printf("Enter the Starting Point of x axis : ");
scanf("%d", &x1);
printf("Enter the Starting of y axis : ");
scanf("%d", &y1);
printf("Enter the End Point of x axis : ");
scanf("%d", &x2);
printf("Enter the End Point of y axis : ");
scanf("%d", &y2);
clrscr();
1
dx = x2 – x1;
dy = y2 – y1;
x=x1;
y=y1;
if(abs(dx) > abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(ceil(x), ceil(y), RED);
for(k=1;k<=steps;k++)
{
x=x+xincrement;
y=y+yincrement;
putpixel(ceil(x),ceil(y), RED);
}
getch();
closegraph();
}
2
output
Result:
Thus the C program to implement the DDA algorithm for line drawing was written,
executed and the output was verified successfully.
3
Ex.No: 1B
Aim:
To write a C program to draw a line by using Bresenham’s algorithm.
Algorithm:
4
Program:
#include<stdio.h>
#include<graphics.h>
void main()
{
int dx,dy,x,y,xend,p,gd=DETECT,gm,xa,xb,ya,yb;
printf("Line drawing using Bresenham's Algorithm:\n");
printf("Enter the starting point and ending point:");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
initgraph(&gd,&gm,"d:/tc/bgi");
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*(dy-dx);
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,12);
do
{
if(p<0)
p+=2*dy;
else
{
y++;
x++;
p+=2*dy;
}
putpixel(x,y,12);
}
while(x<xend);
getch();
closegraph();
}
5
Output:
6
Result:
Thus the C program to implement the Bresenham’s algorithm for line drawing was
written, executed and the output was verified successfully.
7
Ex. No. 1C
Midpoint Circle Drawing Method
Aim: To write a C Program for Circle Drawing using Midpoint Circle Method.
Algorithm
1. Start
2. Get the center point (xcenter, ycenter) and radius(r) of a circle.
3. Initialize the variables
i. x=0; y=r; p=1-r;
4. If (p<0) then
i. p=p+(2*x)+1
else
i. y--;
ii. p=p+2* (x-y)+1
5. Repeat step 4 until x<y and Increment x by 1 each time.
6. Plot the pixel to display the circle.
7. Display the circle
8. Stop.
Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x,y,r,p,xcenter, ycenter;
void circleplot(int,int,int,int);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"..\\BGI:");
printf("\nEnter the x-coordinate of the centre point :");
scanf("%d",&xcenter);
printf("\nEnter the y-coordinate of the centre point :");
scanf("%d",&ycenter);
printf("\nEnter the radius of a circle:");
scanf("%d",&r);
x=0;
y=r;
p=1-r;
while (x<y)
{
x++;
8
if (p<0)
p=p+2*x+1;
else
{
y--;
p=p+2*(x-y)+1;
}
circleplot(xcenter,ycenter,x,y);
}
getch();
closegraph();
}
void circleplot(int xcenter, int ycenter,int x, int y)
{
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
putpixel(xcenter+y,ycenter+x,10);
putpixel(xcenter-y,ycenter+x,10);
putpixel(xcenter+y,ycenter-x,10);
putpixel(xcenter-y,ycenter-x,10);
}
Output:
9
Result:
10
Thus the C program to implement the Midpoint circle drawing was written, executed
and the output was verified successfully
Ex.No: 2A
2D Transformations – Translation.
Aim:
Algorithm:
1. Start the program.
2. Input the no of vertices and their corresponding co-ordinates.
3. Display the input figure by using the draw() function.
4. Input the Transformation factor for both x and y , then also display the figure before
transformation.
5. By using these co-ordinates , we can change the position of the figure.
6. Clear the screen and then display the figure after the transformation.
7. Terminate the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int n,a[25],a1[25],i;
void draw(int *,int);
void main()
{
int gm,gd=DETECT;
int tpx,tpy;
initgraph(&gd,&gm,"c:/tc/bgi");
cleardevice();
printf("Enter the no of vertices\n");
scanf("%d",&n);
printf("Enter the co_ordinates\n");
for(i=0;i<n*2;i++)
scanf("%d",&a[i]);
outtextxy(50,50,"The input figure is");
draw(a,4);
clrscr();
11
cleardevice();
printf("Enter the transformation factor x and y\n");
scanf("%d%d",&tpx,&tpy);
for(i=0;i<n*2;i+=2)
a1[i]=a[i]+tpx;
for(i=1;i<n*2;i+=2)
a1[i]=a[i]+tpy;
cleardevice();
outtextxy(50,50,"Before transformation");
draw(a,2);
outtextxy(50,50,"After transformation");
draw(a1,4);
getch();
}
void draw(int *a,int col)
{
setfillstyle(BKSLASH_FILL,col);
fillpoly(n,a);
getch();
cleardevice();}
Output:
12
13
Result:
Thus the C program to implement the two dimensional translation was written, executed
and the output was verified successfully.
14
Ex.No: 2 B
2D Transformations – Rotation
Aim:
Algorithm:
1. Start the program.
2. Declare the variables and initiate the initgraph() method.
3. Get the no of vertices and their corresponding co-ordinates.
4. Using them, display the input figure then get the rotation angle and fixed point of
rotation.
5. Depending upon the fixed point, rotate the figure by changing the co-ordinates of
the vertex.
6. Display the given figure, before rotation and also after rotation.
7. Terminate the program.
15
Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int a[25],a1[25],i,n;
void draw(int *,int);
void main()
{
double angle;
float pi;
int px,py,t,j;
int gm,gd=DETECT;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\t\n**Rotation***");
printf("Enter the no of vertices:");
scanf("%d",&n);
printf("Enter the coordinates:");
for(i=0;i<n*2;i++)
scanf("%d",&a[i]);
outtextxy(50,50,"Input figure");
draw(a,4);
pi=3.14/180;
printf("Enter the rotation angle:");
scanf("%d",&t);
printf("Enter the rotation point:");
scanf("%d%d",&px,&py);
j=0;
angle=(double)(t*pi);
for(i=0;i<n*2;i+=2)
{
a1[j]=px+((a[i]-px)*cos(angle))-((a[i+1]-py)*sin(angle));
a1[++j]=py+((a[i]-px)*sin(angle))-((a[i+1]-py)*cos(angle));
j++;
}
cleardevice();
outtextxy(50,50,"Before Rotation:");
circle(px,py,2);
line(px,py,px,0);
line(px,py,getmaxx(),py);
line(px,py,px,getmaxy());
outtextxy(px,py,"axis");
draw(a,2);
outtextxy(300,50,"After Rotation");
circle(px,py,2);
line(px,py,0,py);
line(px,py,getmaxx(),py);
line(px,py,px,getmaxy());
16
outtextxy(px,py,"axis");
draw(a1,4);
}
void draw(int *a,int col)
{
setfillstyle(BKSLASH_FILL,col);
fillpoly(n,a);
getch();
cleardevice();
}
Output:
17
Result:
Thus the C program to implement the two dimensional rotation was written, executed
and the output was verified successfully.
18
Ex.No : 2C
2D Transformations – Scaling
Aim:
Algorithm:
19
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int n,a[25],a1[25],i,k;
void draw(int *,int);
void main()
{
int gm,gd=DETECT;
int tpx,tpy;
initgraph(&gd,&gm,"c:/tc/bgi");
cleardevice();
printf("***SCALLING**8\n");
printf("Enter the no of vertices:\n");
scanf("%d",&n);
printf("Enter the coordinates:\n");
for(i=0;i<n*2;i++)
scanf("%d",&a[i]);
clrscr();
cleardevice();
outtextxy(50,50,"The input figure is");
draw(a,4);
clrscr();
cleardevice();
printf("Enter the value to zoom:\n");
scanf("%d",&k);
for(i=0;i<n*2;i+=2)
a1[i]=a[i]*k;
for(i=1;i<n*2;i+=2)
a1[i]=a[i]*k;
outtextxy(50,50,"after zooming");
gotoxy(10,10);
draw(a1,4);
}
void draw(int *a,
int col)
{
setfillstyle(SLASH_FILL,col);
fillpoly(n,a);
getch();
cleardevice();
}
20
Output:
21
Result:
Thus the C program to implement the two dimensional scaling was written, executed
and the output was verified successfully.
22
Ex.No: 2D
Aim:
Algorithm:
23
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,midx,midy,midw,n;
int xs1,xs2,xs3,ys1,ys2,ys3,ch,x1,y1,x2,y2,x3,y3,abs,c,b,w,z,sh;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\nEnter the I co-ordinate***\n");
scanf("%d%d",&x1,&y1);
printf("\nEnter the II coordinate***\n");
scanf("%d%d",&x2,&y2);
midx=abs((x2-x1)/2);
midy=abs((y2-y1)/2);
cleardevice();
do
{
printf("1.Shearing\n2.Reflection\n3.Quit\n");
scanf("%d",&n);
switch(n)
{
case 1:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
printf("Enter the shearing factor\n");
scanf("%d",&sh);
printf("Enter the choice 1 with respect to x,2 with respect to y\n");
scanf("%d",&ch);
printf("%d",ch);
if(ch==1)
{
xs1=x1+sh*y1;
xs2=x2+sh*y2;
line(x1,y1,xs2,y2);
moveto(xs2,y2);
lineto(xs1+abs((xs2-xs1)/2),y1+abs((y2-y1)/2));
lineto(x1,y1);
}
else
{
24
ys1=y1+sh*x1;
ys2=y2+sh*x2;
line(x1,y1,x2,ys2);
moveto(x2,ys2);
lineto(x1+abs((x2-x1)/2),ys1+abs((ys2-ys1)/2));
lineto(x1,y1);
}
getch();
break;
case 2:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
if(y1>y2)
z=y1;
else
z=y2;
if(x1<x2)
w=x2;
else
w=x1;
line(0,z+10,w+50,z+10);
line(x1,z+20+(z-y1),x2,z+20+(z-y2));
moveto(x2,z+20+(z-y2));
lineto(x1+midx,z+20+(z-(y1+midy)));
lineto(x1,z+20+(z-y1));
getch();
break;
case 3:
exit(0);
break;
}}
while(n<=3);
getch();
}
25
Output:
26
27
28
Result:
Thus the C program to implement the 2D reflection and shearing was written, executed
and the output was verified successfully.
29
Ex.No: 2E Window to View Port Algorithm
Aim:
To write a C program for implementing Window to viewport Translation.
STEPS
Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gm,gr,xwmin,ywmin,xwmax,
ywmax,xvmin,yvmin,xvmax,yvmax,xw,yw,xv,yv,sx,sy;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the window min coordinate(xwmin,ywmin):\n");
scanf("%d%d",&xwmin,&ywm+in);
printf("Enter the window max coordinate(xwmax,ywmax):\n");
scanf("%d%d",&xwmax,&ywmax);
rectangle(xwmin,ywmax,xwmax,ywmin);
printf("Enter the viewport min coordinate(xvmin,yvmin):\n");
scanf("%d%d",&xvmin,&yvmin);
printf("Enter the viewport max coordinate(xvmax,yvmax):\n");
scanf("%d%d",&xvmax,&yvmax);
sx=(xvmax-xvmin)/(xwmax-xwmin);
sy=(yvmax-yvmin)/(ywmax-ywmin);
rectangle(xvmin,yvmax,xvmax,yvmin);
printf("Enter the point coordinate(xw,yw) in the window:\n");
scanf("%d%d",&xw,&yw);
putpixel(xw,yw,15);
xv=(sx*(xw-xwmin))+xvmin;
yv=(sy*(yw-ywmin))+yvmin;
putpixel(xv,yv,15);
getch();
}
30
OUTPUT
RESULT
31
Thus the c program to implement window to view port mapping was coded and executed
successfully.
Ex.No: 3
Composite 2D Transformations
Aim:
Algorithm:
32
Program:
include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void input();
void output();
void translation();
void rotation();
void scaling();
int a[10][2],i,x,temp,y,n,fx,fy,opt,ch,j=1;
int sx,sy;
int tx,ty;
float k;
void input()
printf("\t\t\tTranslation");
scanf("%d",&n);
for(i=0;i<n;i++)
33
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
void output()
for(i=0;i<n;i++)
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); }
void translation()
scanf("%d%d",&tx,&ty);
cleardevice();
output();
for(i=0;i<=n;i++)
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before translation");
setlinestyle(2,1,1);
line(300,460,350,460);
34
outtextxy(355,460,"After translation");
outtextxy(520,460,"S.S.RENJNI");
delay(100);
Void rotation()
scanf("%d",&y);
scanf("%d%d",&fx,&fy);
cleardevice();
output();
k=(y*3.14)/180;
for(i=0;i<=n;i++)
tx=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
ty=fy+(a[i][0]-fx)*sin(k)+(a[i][1]-fy)*cos(k);
a[i][0]=tx;
a[i][1]=ty;
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before rotation");
setlinestyle(2,1,1);
line(300,460,350,460);
35
outtextxy(355,460,"After rotation");
outtextxy(520,460,"SUNDAR");
delay(100);
void scaling()
scanf("%d%d",&sx,&sy);
cleardevice();
output();
for(i=0;i<=n;i++)
a[i][0]=a[i][0]*sx;
a[i][1]=a[i][1]*sy;
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before scaling");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After scaling");
outtextxy(520,460,"S.S.RENJNI");
delay(100);
void main()
36
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\\TC\\bgi");
input();
output();
do
printf("\n1:Translation\n2:Rotation\n3:Scaling\n");
scanf("%d",&ch);
switch(ch)
case 1:translation();
break;
case 2:rotation();
break;
case 3:
scaling();
break;
delay(6000);
closegraph();
initgraph(&gd,&gm,"D:\\TC\\bgi");
setlinestyle(0,1,1);
scanf("%d",&j);
37
if(j==1)
for(i=0;i<n;i++)
printf("\n");
printf("%d %d %d %d",a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}while(j==1);
getch();}
38
Output:
39
40
41
Result:
Thus the C program to implement the Composite 2D-Transformations was written, executed and
the output was verified successfully.
42
Ex.No: 4
Cohen Sutherland 2D line clipping and Windowing
Aim:
Algorithm:
2. Read two end points of the line say p1(x1,y1) and p2(x2,y2)
3.Read two corners (left-top and right-bottom)of the window , say (Wx1,Wy1andWx2,Wy2)
4. Assign the region codes for two endpoints p1 and p2 using following steps:
a)If region codes for both endpoints p1 and p2 are zero then the line is completely visible. Hence
draw the line and go to 10.
b)If region codes for endpoints are not zero and the logical ANDing of them is also nonzero then the
line is completely invisible, so reject the line and go to 10.
c)If region codes for two endpoints do not satisfy the conditions in 4a & 4b the line is partially
visible.
6. Determine the intersecting edge of the clipping window by inspecting the region codes of
two endpoints.
a)If region codes for both the endpoints are non-zero, find intersection points p1’ and p2’ with
boundary edges of clipping window with respect to point p1 and point p2,respectively.
b) If region codes for any one endpoints is non-zero, find intersection points p1’ and p2’ with
boundary edge of clipping window with respect to it.
8. Reject the line segments if any one end point of it appears outside the clipping window.
43
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
int x,y;
char code[4];
}pt;
void drawwindow();
pt setcode(pt p);
main()
int gd=DETECT,gm,v;
pt p1,p2,ptemp;
initgraph(&gd,&gm,"D:/tc/bgi");
cleardevice();
scanf("%d%d",&p1.x,&p1.y);
scanf("%d%d",&p2.x,&p2.y);
44
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
case 0:
cleardevice();
drawwindow();
drawline(p1,p2,15);
break;
case 1:
cleardevice();
drawwindow();
break;
case 2:
cleardevice();
p1=resetendpt(p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;
45
getch();
closegraph();
return(0);
void drawwindow()
setcolor(RED);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
pt setcode(pt p)
pt ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
46
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
int i,flag=0;
for(i=0;i<4;i++)
if((p1.code[i]!='\0')||(p2.code[i]!='0'))
flag=1;
if(flag==0)
return 1;
return 2;
47
pt temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1');
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
48
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
else return(p1);
Output:
49
50
Result:
Thus the C program to implement the line clipping was written, executed and the output was
verified successfully
51
Ex. No: 5
Aim:
Algorithm:
4. The choices is ‘1’ then get the translation values and move the 3D-object.
5. The choices is ‘2’ then get the scaling vectors and change the size of 3D-object.
6. The choices is ‘3’ then get the rotation angle to rotate the 3D-object.
Program:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<process.h>
int points[50];
int points1[50];
int x=25;
int n=5;
52
void construct(int[]);
void construct1(int[],int[]);
void main()
int gd=DETECT;
int gm=DETECT;
char z;
initgraph(&gm,&gd,"D:/tc/bgi");
do
int points[50]={100,100,100,150,150,150,150,100,100,100};
int a;
cleardevice();
printf("\n\n\n\t\t\t\t1.Translation");
printf("\n\n\n\t\t\t\t2.Scaling");
printf("\n\n\n\t\t\t\t3.Rotation");
printf("\n\n\n\t\t\t\t4.Exit");
scanf("%d",&a);
switch(a)
case 1:
int tx,ty,i,point[50],point1[50];
construct(points);
scanf("%d%d",&tx,&ty);
53
for(i=0;i<=2*n;i=i+2)
point[i]=points[i]+tx;
point1[i]=points1[i]+tx;
for(i=1;i<2*n;i=i+2)
point[i]=points[i]+ty;
point1[i]=points1[i]+ty;
construct1(point,point1);
getch();
break;
case 2:
int sx,sy,sz,point[50],point1[50],i;
construct(points);
scanf("%d%d%d",&sx,&sy,&sz);
point[0]=points[0];
point[1]=points[1];
point[2]=points[2];
point[3]=points[1]+sy*(points[3]-points[1]);
point[4]=points[2]+sx*(points[4]-points[2]);
point[5]=points[7]+sy*(points[5]-points[7]);
point[6]=points[0]+sx*(points[6]-points[0]);
54
point[7]=points[7];
point[8]=points[8];
point[9]=points[9];
for(i=0;i<2*n;i++)
point1[i]=point[i]+(x*sz);
construct1(point,point1);
getch();
break;
case 3:
int point[50],point1[50],i;
float an;
construct(points);
scanf("%f",&an);
an=(float)an*3.14/180;
for(i=0;i<=8;i+=2)
point[i]=points[0]+(points[i]-points[0])*cos(an)-(points[i+1]-points[1])*sin(an);
for(i=1;i<=9;i+=2)
point[i]=points[1]+(points[i]-points[1])*cos(an)-(points[i-1]-points[0])*sin(an);
55
for(i=0;i<=9;i+=2)
point1[i]=points1[0]+(points1[i]-points1[0])*cos(an)-(points1[i+1]-points1[1])*sin(an);
for(i=1;i<=9;i+=2)
point1[i]=points1[1]+(points1[i]-points1[1])*cos(an)-(points1[i-1]-points1[0])*sin(an);
construct1(point,point1);
getch();
break;
case 4:
exit(0);
break;
scanf("%s",&z);
}while(z=='Y');
getch();
closegraph();
int x=25,n=5,i;
56
cleardevice();
for(i=0;i<2*n;i++)
points1[i]=points[i]+x;
construct1(points,points1);
drawpoly(n,x);
drawpoly(n,y);
line(x[0],x[1],y[0],y[1]);
line(x[2],x[3],y[2],y[3]);
line(x[4],x[5],y[4],y[5]);
line(x[6],x[7],y[6],y[7]);
57
Output:
58
59
60
Result:
Thus the C program to implement the 3D-Transformations was written, executed and the output
was verified successfully.
61
EX NO : 6 3D Projections – Parallel, Perspective.
Algorithm
1. Start the program.
4. The choices is ‘1’ then get the perspective values and move the 3D-object.
5. The choices is ‘2’ then get the scaling vectors and change the size of 3D-object.
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
# define n1 -1.0
# define n2 1.0
# define n3 1.0
# define x0 1.0
# define y0 1.0
# define z0 1.0
# define a 0.2
# define b 0.2
# define c 0.2
void show_screen( );
void draw_cube(int [8][3]);
void draw_pyramid(int [5][3]);
void get_projected_point(int&,int&,int&);
void multiply_matrices(constfloat[4],constfloat[4][4],float[4]);
void Line(constint,constint,constint,constint);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..\\Bgi");
show_screen( );
int cube[8][3]={
62
{370,200,50}, // front left top
{470,200,50}, // front right top
{470,300,50}, // front right bottom
{370,300,50}, // front left bottom
{370,200,-50}, // back left top
{470,200,-50}, // back right top
{470,300,-50}, // back right bottom
{370,300,-50} // back left bottom
};
setcolor(15);
draw_cube(cube);
settextstyle(0,0,1);
outtextxy(400,320,"Cube");
int pyramid[5][3]={
{120,300,50}, // base front left
{220,300,50}, // base front right
{220,300,-50}, // base back right
{120,300,-50}, // base back left
{170,150,0} // top
};
setcolor(15);
draw_pyramid(pyramid);
settextstyle(0,0,1);
outtextxy(135,320,"Pyramid");
getch( );
closegraph( );
return 0;
}
/************************************************************************///------------------------
---- draw_cube( )
--------------------------///***********************************************************************
*/
void draw_cube(int edge_points[8][3])
{
for(int i=0;i<8;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[1][0],edge_points[1][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[2][0],edge_points[2][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[3][0],edge_points[3][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[0][0],edge_points[0][1]);
Line(edge_points[4][0],edge_points[4][1],
edge_points[5][0],edge_points[5][1]);
Line(edge_points[5][0],edge_points[5][1],
edge_points[6][0],edge_points[6][1]);
63
Line(edge_points[6][0],edge_points[6][1],
edge_points[7][0],edge_points[7][1]);
Line(edge_points[7][0],edge_points[7][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[5][0],edge_points[5][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[7][0],edge_points[7][1]);
}
/************************************************************************///------------------------
-- draw_pyramid( )
-------------------------///***********************************************************************
*/void draw_pyramid(int edge_points[5][3])
{
for(int i=0;i<5;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[1][0],edge_points[1][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[2][0],edge_points[2][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[3][0],edge_points[3][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[0][0],edge_points[0][1]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[4][0],edge_points[4][1]);
}
/************************************************************************///---------------------
get_projected_point( )
-----------------------///************************************************************************/
void get_projected_point(int& x,int& y,int& z)
{
float d0=((n1*x0)+(n2*y0)+(n3*z0));
float d1=((n1*a)+(n2*b)+(n3*c));
float d=(d0-d1);
float Per_NRC[4][4]={
{(d-(a*n1)),(b*n1),(c*n1),n1},
{(a*n2),(d+n2*b),(c*n2),n2},
{(a*n3),(b*n3),(d+(c*n3)),n3},
{(-a*d0),(-b*d0),(-c*d0),-d1}
};
64
float xy[4]={x,y,z,1};
float new_xy[4]={0};
multiply_matrices(xy,Per_NRC,new_xy);
x=(int)(new_xy[0]+0.5);
y=(int)(new_xy[1]+0.5);
z=(int)(new_xy[2]+0.5);
}
/************************************************************************///----------------------
multiply_matrices( )
------------------------///************************************************************************/
void multiply_matrices(constfloat matrix_1[4],
constfloat matrix_2[4][4],float matrix_3[4])
{
for(int count_1=0;count_1<4;count_1++)
{
for(int count_2=0;count_2<4;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
}
}
/*************************************************************************///----------------------
---- Line( )
------------------------///************************************************************************
*/
void Line(constint x_1,constint y_1,constint x_2,constint y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
65
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
/*************************************************************************///----------------------
---- show_screen( )
---------------------------///**********************************************************************
***/void show_screen( )
{
setfillstyle(1,1);
bar(182,26,438,38);
settextstyle(0,0,1);
66
setcolor(15);
outtextxy(5,5,"******************************************************************************
");
outtextxy(5,17,"*-
**************************************************************************-*");
outtextxy(5,29,"*-------------------- ----------------------*");
outtextxy(5,41,"*-
**************************************************************************-*");
outtextxy(5,53,"*-
**************************************************************************-*");
setcolor(11);
outtextxy(189,29,"General Perspective Projection");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"*-* *-*");
outtextxy(5,438,"*-
**************************************************************************-*");
outtextxy(5,450,"*--------------------------- ------------------------*");
outtextxy(5,462,"****************************************************************************
**");
setcolor(12);
outtextxy(227,450," Press any key to exit ");
Output
Result:
67
Thus the C program to implement the parallel and perspective was written, executed
and the output was verified successfully
Aim :
To do Basic Operations on image using image editing software, Creating gif animated
images, Image optimization
1. Open Photoshop.
2. Choose File > Open from the main menu and browse to your JPEG file.
3. Select the file, but be sure to change the file format to Camera Raw before opening
the file.
Basic Operations :
2. Tone curve
68
Output
2.Tone curve
69
9B) Build Animated GIFs in Photoshop CS3
Result :
Thus Image Editing and Manipulation - Basic Operations on image using image editing software,
Creating gif animated images, Image optimization was done successfully.
EX NO: 10 2D Animation – To create Interactive animation using any authoring tool.
step 4 Export your animation in .FLV format, or as individual images for each
frame.
Output :
10 b)Synfig Studio
Software Decsription
Synfig Studio is a free and open-source 2D animation software
Designed as powerful industrial-strength solution for creating film-quality
animation using a vector and bitmap artwork.
It eliminates the need to create animation frame-by frame, allowing you to produce
2D animation of a higher quality with fewer people and resources.
Steps:
Output :
Result :
Thus 2D– Interactive animation is created using the software pencil and synfig studio