Infant Jesus College of Engineering Keelavallanadu: CS 1355 Graphics & Multimedia Lab Manual
Infant Jesus College of Engineering Keelavallanadu: CS 1355 Graphics & Multimedia Lab Manual
Infant Jesus College of Engineering Keelavallanadu: CS 1355 Graphics & Multimedia Lab Manual
KEELAVALLANADU
Prepared by
R.Indrakumari
Sr.Lecturer / CSE
CS1355 GRAPHICS AND MULTIMEDIA LAB
AIM
In Bresenham’s approach the pixel position along a line path are determined by sampling unit X intervals. Starting
from the left end point(X0, Y0)of a given line we step to each successive columns and plot the pixel whose scan line
Y-value is closest to the line path. Assuming the Kth step in process, determined that the pixel at(X k, Yk)decide
which pixel to plot in column Xk+1.The choices are (Xk+1, Yk) and (Xk+1,Yk+1)
Algorithm
Step 1: Input the line endpoints and store the left endpoint in (X0, Y0)
Step 3: Calculate constants ∆x, ∆y, 2∆y, and 2∆y -2∆x, and obtain the decision parameters as
P0 = 2 ∆y – ∆x
Step 4 : At each Xk along the line, starting at k = 0, perform the following test.
If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2∆y
Pk+1 = Pk+2 ∆y - 2 ∆x
Program
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"
#include"dos.h"
#include"math.h"
void main()
{
int driver,mode,k;
int xa,xb,ya,yb,dx,dy,x,y,i,p,dy2,dydx2;
clrscr();
printf("\n\tEnter(xa,ya)...");
scanf("%d%d",&xa,&ya);
printf("\n\tEnter(xb,yb)...");
scanf("%d%d",&xb,&yb);
x=xa;
y=ya;
driver=DETECT;
initgraph(&driver,&mode,"");
dx=abs(xb-xa);
dy=abs(yb-ya);
p=(2*dy)-dx;
dy2=2*dy;
dydx2=2*(dy-dx);
line(100,0,100,500);
line(0,100,500,100);
putpixel(x,y,2);
for(i=0;i<=dx;i++)
{
if(p<0)
{
x=x+1;
putpixel(x,y,2);
p+=dy2;
}
else
{
x=x+1;
y=y+1;
putpixel(x,y,2);
p+=dydx2;
}
}
getch();
}
output:
Enter(xa,ya)...100 100
Enter(xb,yb)...250 252
AIM
Algorithm
Step 1:Input radius r and circle center(Xc, Yc)and obtain the first point on the circumference of a circle centered on
the origin as (X0, Y0) = (0, r)
If Pk < 0, the next point to plot is (Xk+1, Yk) and Pk+1 = Pk+2 Xk+1 + 1
Otherwise the next point is (Xk+1, Yk-1) and Pk+1 = Pk+2 Xk+1 + 1- 2Yk+1 where 2Xk+1=2Xk+2 and 2Yk+1=2Yk-2
Step 5: Move each pixel position(X, Y) onto the circular path centered on(Xc, Yc) and plot the coordinate values as
X = X + Xc Y = Y + Yc
AIM
To perform the various 2-dimensional transformations such as translation, scaling, rotation, reflection and
shearing.
Algorithm
Step 2: Display the menu as 1.Translation 2.Scaling 3.Rotation 4.Reflection 5.Shearing 6.Exit
Step 4: If the choice is 1 get the translation vector. Add the translation vector to the original coordinate position to
move to a new position.
Step 5: If the choice is 2 get the scaling factor. Multiply the coordinate values of each vertex by scaling factors to
produce the transformed coordinates.
Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the specified angle.
Step 7: If the choice is 4 get the axis of reflection. Mirror image is generated relative to an axis of reflection by
rotating the object 180◦ about the reflection axis.
Step 8: If the choice is 5 shearing is done which causes the transformation that distorts the shape of an object.
Program
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
int gd=0,gm,theta,xf,yf,x1,y1,x2,y2,xa1,xa2,ya1,ya2,a;
int midx,midy,tx,ty,choice,sx,sy,sh,xa[10],ya[10],first;
int i,n=5;
int xf,yf;
int xb[10],yb[10];
void main()
{
do
{
first=0;
org();
getch();
printf("\n1.translation\n2.rotation\n3.scaling\n4.reflection\n5.shear\n6.exit\nenter ur choice\n");
scanf("%d",&choice); //input options
switch(choice)
{
case 1: //translation
printf("Enter the translation factor (x,y) :"); //get the factor (x,y)
scanf("%d%d",&tx,&ty);
org();
rectangle(midx+tx,midy-100+ty,midx+100+tx,midy+ty); //add with x,y
getch();
closegraph();
break;
case 2: //rotation
printf("Enter the angle :");
scanf("%d",&a);
org();
rotation(); //call rotation function
break;
case 3: //scaling
printf("Enter the scaling factor (x,y):"); //get the factor
scanf("%d%d",&sx,&sy);
org();
x1=0;
y1=-100;
x2=100;
y2=0;
xa1=x1*sx+midx; //multiply with x,y
ya1=y1*sy+midy;
xa2=x2*sx+midx;
ya2=y2*sy+midy;
rectangle(xa1,ya1,xa2,ya2);
getch();
closegraph();
break;
case 4: //Reflection
org();
rectangle(midx,midy,midx+100,midy+100);
getch();
closegraph();
break;
case 5: //Shear
printf("enter the shear value");
scanf("%d",&sh);
cleardevice();
org();
for(i=0;i < n;i++) //multiply with shear factor
{
xb[i]=xa[i]+sh*(ya[i]-yf);
yb[i]=ya[i];
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
}
}while(choice<6);
getch();
}
org()
{
initgraph(&gd,&gm,"");
midx=getmaxx()/2;
midy=getmaxy()/2;
xf=midx;
yf=midy;
xa[0]=midx;
ya[0]=midy-100;
xa[1]=midx+100;
ya[1]=midy-100;
xa[2]=midx+100;
ya[2]=midy;
xa[3]=midx;
ya[3]=midy;
xa[4]=midx;
ya[4]=midy-100;
cleardevice();
setcolor(WHITE);
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
setcolor(4);
if(first==0)
{
setlinestyle(SOLID_LINE,1,1);
first=1;
}
else
{
setlinestyle(DASHED_LINE,1,1);
}
rectangle(midx,midy-100,midx+100,midy);
setlinestyle(SOLID_LINE,1,1);
return;
}
rotation()
{
theta=(a*2*3.14)/180;
for(i=0;i < n;i++)
{
xb[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
yb[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
cleardevice();
return;
}
EX NO 3a COHEN-SUTHERLAND ALGORITHM
AIM:
Algorithm:
The method speeds up the processing of line segments by performing initial tests that reduce the number of
intersections that must be calculated.
1. Every line endpoint is assigned a four digit binary code, called region code, that identifies the location of
the point relative to the boundaries of the clipping rectangle.
2. Each bit position in the region code is used to indicate one of the four relative coordinate positions of the
point with respect to the clip window.
Bit 1: left
Bit 2: right
Bit 3: below
Bit 4: above
3. Bit values in the region code are determined by comparing endpoint coordinates values (x, y) with respect
to the clip boundaries. eg.Bit 1 is set to 1 if x<xwmin
4. Once we have established region codes for all line endpoints, we can quickly determine which lines are
completely outside or inside the clip window.
5. Lines that cannot be identified as completely inside or outside a clip window are checked for intersection
with boundaries.
6. Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line
equation.
m ( y2 y1 ) ( x2 x1 )
y y1 y yw min
x x1 y yw
Program m max
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
scanf("%f %f",&xmin,&ymin);
scanf("%f %f",&xmax,&ymax);
printf("\n\tEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
scanf("%f %f",&x2,&y2);
for(i=0;i<4;i++)
start[i]=0;
end[i]=0;
m=(y2-y1)/(x2-x1);
if(x1<xmin)
start[0]=1;
if(x1>xmax)
start[1]=1;
if(y1>ymax)
start[2]=1;
if(y1<ymin)
start[3]=1;
if(x2<xmin)
end[0]=1;
if(x2>xmax)
end[1]=1;
if(y2>ymax)
end[2]=1;
if(y2<ymin)
end[3]=1;
for(i=0;i<4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&
(end[0]==0)&&(end[1]==0)&&(end[2]==0)&&(end[3]==0))
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
else
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
x1=x1+(ymin-y1)/m;
y1=ymin;
if((end[2]==0)&&(end[3]==1))
x2=x2+(ymin-y2)/m;
y2=ymin;
if((start[2]==1)&&(start[3]==0))
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
x2=x2+(ymax-y2)/m;
y2=ymax;
if((start[1]==0)&&(start[0]==1))
y1=y1+m*(xmin-x1);
x1=xmin;
if((end[1]==0)&&(end[0]==1))
y2=y2+m*(xmin-x2);
x2=xmin;
if((start[1]==1)&&(start[0]==0))
y1=y1+m*(xmax-x1);
x1=xmax;
if((end[1]==1)&&(end[0]==0))
y2=y2+m*(xmax-x2);
x2=xmax;
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
else
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
getch();
closegraph();
OUTPUT:
AIM:
Algorithm
Step1: Draw a world coordinate area selected for display called as window. This window defines what is to be
viewed.
Step 2: Draw an area on a display device to which a window is mapped called as Viewport. The viewport defines
where it is to be displayed.
Step 3: Now perform the mapping of a part of a world-coordinate scene to device coordinates referred as viewing
transformation.
Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/tc/bgi");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;
w3=635;
w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
getch();
v1=425;
v2=75;
v3=550;
v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+0.5);
x2=v1+floor(((float)(x2-w1)*sx)+0.5);
x3=v1+floor(((float)(x3-w1)*sx)+0.5);
y1=v2+floor(((float)(y1-w2)*sy)+0.5);
y2=v2+floor(((float)(y2-w2)*sy)+0.5);
y3=v2+floor(((float)(y3-w2)*sy)+0.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
EX NO 4 3-D TRANSFORMATIONS
AIM
Algorithm
Step 4: If the choice is 1 a point or an object is translated from position P to position P' with the operation P'=T.P
where tx,ty and tz specifying translation distances.
x'=x+ tx
y'=y+ ty
z'=z+ tz
Step 5: If the choice is 2 the scaling transformation of a position P can be written as P'=S.P where scaling
parameters sx,sy and sz are assigned any positive values.
x'=x.sx
y'=y.sy
z'=z.sz
Step 6: If the choice is 3 get the rotation angle. Rotate the figure with respect to the axis of rotation.
x'=xcosӨ-ysinӨ
y'=xsinӨ+ycosӨ
z'=z
y'=ycosӨ-zsinӨ
z'=ysinӨ+zcosӨ
x'=x
z'=zcosӨ-xsinӨ
x'=zsinӨ+xcosӨ
y'=y
Program
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int x[20],y[20],x1[20],y1[20],tx,ty,sx,sy,sz,i,s,d;
void main()
int gd=DETECT,gm,ch,a,theta;
initgraph(&gd,&gm,"");
scanf("%d",&s);
for(i=0;i<s;i++)
scanf("%d%d",&x[i],&y[i]);
printf("\tDepth :");
scanf("%d",&d);
clrscr();
cleardevice();
axis();
setcolor(RED);
draw3d(s,x,y,d);
getch();
do
clrscr();
cleardevice();
setcolor(WHITE);
scanf("%d",&ch);
switch(ch)
case 1:
scanf("%d%d",&tx,&ty);
clrscr();
cleardevice();
axis();
setcolor(RED);
draw3d(s,x,y,d);
getch();
cleardevice();
for(i=0;i<s;i++)
x1[i]=x[i]+tx;
y1[i]=y[i]+ty;
}
axis();
setcolor(DARKGRAY);
setlinestyle(DASHED_LINE,1,1);
draw3d(s,x,y,d);
setlinestyle(SOLID_LINE,1,1);
setcolor(RED);
draw3d(s,x1,y1,d);
getch();
break;
case 2:
scanf("%d%d%d",&sx,&sy,&sz);
clrscr();
cleardevice();
axis();
setcolor(RED);
draw3d(s,x,y,d);
getch();
cleardevice();
for(i=0;i<s;i++)
x1[i]=x[i]*sx;
y1[i]=y[i]*sy;
axis();
setcolor(DARKGRAY);
setlinestyle(DASHED_LINE,1,1);
draw3d(s,x,y,d);
setlinestyle(SOLID_LINE,1,1);
setcolor(RED);
draw3d(s,x1,y1,sz*d);
getch();
break;
case 3:
scanf("%d",&a);
clrscr();
cleardevice();
axis();
setcolor(RED);
draw3d(s,x,y,d);
getch();
cleardevice();
theta=(a*2*3.14)/180;
for(i=0;i<s;i++)
x1[i]=x[0]+((x[i]-x[0])*cos(theta))-((y[i]-y[0])*sin(theta));
y1[i]=y[0]+((y[i]-y[0])*cos(theta))+((x[i]-x[0])*sin(theta));
axis();
setcolor(DARKGRAY);
setlinestyle(DASHED_LINE,1,1);
draw3d(s,x,y,d);
setlinestyle(SOLID_LINE,1,1);
setcolor(RED);
draw3d(s,x1,y1,d);
getch();
break;
default :
break;
}
}while(ch<4);
closegraph();
axis()
setcolor(WHITE);
line(100,0,100,getmaxy()-100);
outtextxy(90,10,"x");
line(100,getmaxy()-100,getmaxx(),getmaxy()-100);
outtextxy(getmaxx()-10,getmaxy()-110,"y");
line(100,getmaxy()-100,0,getmaxy());
outtextxy(10,getmaxy()-10,"z");
return;
int i,j,k=0;
for(j=0;j<2;j++)
for(i=0;i<s-1;i++)
line(x[i]+xorg-k,yorg-y[i]+k,x[i+1]+xorg-k,yorg-y[i+1]+k);
line(x[i]+xorg-k,yorg-y[i]+k,x[0]+xorg-k,yorg-y[0]+k);
k=d;
for(i=0;i<s;i++)
line(x[i]+xorg,yorg-y[i],x[i]+xorg-d,yorg-y[i]+d);
}
OUTPUT:
Depth : 45
EX NO 5 PROJECTIONS OF 3D IMAGES
AIM
Algorithm
Step 4: For a perspective projection object positions are transformed to the view plane along lines that converge to a
point called the projection reference point.
Step 5: The projected view of an object is determined by calculating the intersection point of the projection lines
with the view plane.
Program
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void main()
int gd=DETECT,gm;
int x[20],y[20],i,s,d;
initgraph(&gd,&gm,"");
scanf("%d",&s);
for(i=0;i<s;i++)
printf("(x%d,y%d) :",i,i);
scanf("%d%d",&x[i],&y[i]);
printf("Depth :");
scanf("%d",&d);
draw3d(s,x,y,d);
getch();
setcolor(14);
for(i=0;i<s-1;i++)
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
line(x[i]+200,y[i],x[0]+200,y[0]);
getch();//top view
for(i=0;i<s-1;i++)
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
getch();//side view
for(i=0;i<s-1;i++)
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
getch();
closegraph();
int i,j,k=0;
for(j=0;j<2;j++)
for(i=0;i<s-1;i++)
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
k=d;
for(i=0;i<s;i++)
line(x[i],y[i],x[i]+d,y[i]-d);
}
EX NO 6 RGB TO HSV COLOR MODEL
Aim
To implement the color models using c/c++ program RGB Color in C program CS1355-Graphics & Multimedia Lab
Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,n;
float r=0,g=0,b=0;
float y=0,i=0,q=0;
float c=0,m=0,y1=0;
initgraph(&gd,&gm,"");
printf("(r,g,b)keys for incrementing R,G,B values respectively\n");
printf("(shift+(r,g,b))keys for decrementing R,G,B values respectively\n");
printf("press esc to exit\n");
setcolor(15);
while(1)
{
gotoxy(18,10);
printf("R G B");
c=1.0-r;
m=1.0-g;
y1=1.0-y;
gotoxy(47,10);
printf("C M Y");
y=0.299*r+0.587*g+0.144*b;
i=0.596*r-0.275*g-0.3218*b;
q=0.212*r-0.528*g+0.311*b;
gotoxy(18,23);
printf("Y I Q");
switch(getche())
{
case 'r':
r++;
break;
case 'g':
g++;
break;
case 'b':
b++;
break;
case 'R':
r--;
break;
case 'G':
g--;
break;
case 'B':
b--;
break;
case 27:
closegraph();
exit(0);
}
if(r>255)
r=0;
if(g>255)
g=0;
if(b>255)
b=0;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(2,c,m,y1);
setfillstyle(1,2);
bar(275,50,495,250);
rectangle(275,50,495,250);
setrgbpalette(3,y,i,q);
setfillstyle(1,3);
bar(50,255,270,455);
rectangle(50,255,270,455);
}
}
Output color model in RGB color Red, Green Blue