Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CGM Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

RECORD

Name :
Register No. :
Year and Semester : IV / VII
Branch and Section : CSE / A
Course Code and Title : 18PD14 / Computer Graphics and Multimedia
Academic Year : 2023 – 2024
BONAFIDE CERTIFICATE

Roll No. _______________________

Certified that this is the Bonafide Record of work done by

Mr./Ms._____________________________________ of VII semester B.E

Computer Science and Engineering (Branch) during the academic year 2021-2022

in the 18PD14 / Computer Graphics and Multimedia Laboratory.

Date: Faculty in-charge

Roll No. _____________________

Submitted for the End Semester Practical Examination held on ______________ at


Karpagam College of Engineering, Coimbatore.

Internal Examiner External Examiner


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

VISION
To create computer professionals with a strong academic and technical background to achieve
special distinction at the national and international arena and also to serve and lead the society.

MISSION
 Providing excellent learning opportunities for students in Computer Science and
Engineering to meet the needs of Nation as a whole
 Establishing centers of research in areas of the immediate needs to the society

 Develop ICT based solutions for the development of the nation

PROGRAMME EDUCATIONAL OBJECTIVES (PEOS)


1. PEO1: Graduates will be able to comprehend mathematics, science, engineering
fundamentals, laboratory and work-based experiences to formulate and solve problems in
Computer Science and Engineering and other related domains and will develop proficiency
in computer based engineering and the use of computation tools.
2. PEO2: Graduates will be prepared to communicate and work effectively on the
multidisciplinary engineering projects practicing the ethics of their profession with a sense of
social responsibility.
3. PEO3:Graduates will recognize the importance of lifelong learning to become experts either
as entrepreneurs or employees and to widen their knowledge in their domain.

PROGRAM OUTCOMES

● PO1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
● PO2: Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
● PO3: Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
● PO4: Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
● PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
● PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
● PO7: Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the knowledge
of, and need for sustainable development.
● PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
● PO9: Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
● PO10: Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
● PO11: Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
● PO12: Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.

PROGRAM SPECIFIC OUTCOMES (PSOs)


After successful completion of the program, graduates of B.E (CSE) will:

PSO-1 Analyze, design, implement, test and evaluate computer programs in the areas related to
algorithms, networking, web design, cloud computing, IoT and data analytics of varying
complexity.

PSO-2 Develop innovative ideas to provide solutions for complex problems and apply advanced
knowledge of computer science domain to identify research challenges in Computer Science and
Engineering.
INDEX

Signature
Sl. Page
Date Name of Experiment Marks of the
No No.
faculty
1(a) BRESENHAM’S ALGORITHM FOR LINE 2
1(b) BRESENHAM’S ALGORITHM FOR ELLIPSE 5
1(c) BRESENHAM’S ALGORITHM FOR CIRCLE 8
1(d) MID – POINT CIRCLE ALGORITHM 11
2(a) PERFORMING 2D TRANSFORMATIONS 14
2(b) PERFORMING 2D TRANSFORMATIONS 18
3(a) IMPLEMENTING OF COHEN – SUTHERLAND 2D 23
CLIPPING
3(b) IMPLEMENTING OF WINDOW – TO – VIEWPORT 27
MAPPING
4 PERFORMING 3D TRANSFORMATIONS 30
5 VISUALIZING PROJECTIONS OF 3D IMAGES 34
6(a) CONVERSION BETWEEN RGB TO HSV COLOR 36
MODEL
6(b) CONVERSION BETWEEN HSV TO RGB COLOR 39
MODEL
6(c) IMPLEMENTING OF TEXT COMPRESSION 42
ALGORITHM
7(a) SIMPLE PRESENTATION USING MOTION TWEEN 46
7(b) IMAGE EDITING AND MANIPULATION USING 49
ADOBE PHOTOSHOP
7(c) MASKING, PENDULUM USING MACROMEDIA 59
FLASH
8 2D ANIMATION 61
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

LIST OF EXPERIMENTS

Implement the exercises using C/openGL/Java

1. Implementation of algorithms for drawing


- 2D Primitives
- Line (DDA, Bresenham’s) all slopes
- Circle (Midpoint).

2. Implementation of 2D geometric transformations


- Translation
- Rotation
- Scaling
- Reflection
- Shear.

3. Implementation of composite 2D transformations.

4. Implementation of 3D transformations – Translation, Rotation, and Scaling.

5. Composite 3D transformation.

6. Creating 3D objects and scenes.

7. Image editing and manipulation – basic operations on image using any image editing
software, creating gif animated images, and image optimization.

8. 2D animation – to create interactive animation using any authoring tool.

1
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 1(a) BRESENHAM'S ALGORITHM FOR LINE

Date :

Aim:

To write a C program to implement the Bresenham's algorithm for Line.

Algorithm:

Step 1: Start the program.

Step 2: Input the two line endpoints and the left endpoint in (x0, y0).

Step 3: Load (x0, y0) into the frame buffer that is plot the first point.

Step 4: Calculate constants ∆x, ∆y, 2∆y and 2∆y-2∆x, and obtain the starting value for the decision
parameter as P0=2∆y-∆x.

Step 5: 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.


Otherwise the next point to plot is (xk+1,yk+1) and Pk+1=Pk+2∆y-2∆x.

Step 6: Repeat the above step ∆x times.

Step 7: Stop the Program.

Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,dx,dy,x,y,xend,p;
initgraph(&gdriver, &gmode, "");
printf("\n\n\n\tBresanham line drawing algorithm");
printf("\n\t-----------------------------");
printf("\n\nEnter the value of x1,y1,x2,y2:::");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=abs(x1-x2), dy=abs(y1-y2),p=2*dy-dx;
if(x1>x2)
{
2
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,WHITE);
while(x<xend)
{
x++;
if(p<0)
p+=2*dy;
else
{
y++;
p+=2*(dy-dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}

3
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to implement the Bresenham's algorithm for Line is executed and the output is
verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

4
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 1 (b) BRESENHAM'S ALGORITHM FOR ELLIPSE

Date :

Aim:

To write a C program to implement the Bresenham's Algorithm for Ellipse.

Algorithm:

Step 1: Start the program.

Step 2: Input rx, ry and ellipse center (xc, yc) and obtain the first point on an ellipse centered on the
origin as (x0, y0)= (0, ry).

Step 3: Calculate the initial value of the decision parameter in region 1 as P10=ry2-rx2ry+1/4 rx2.

Step 4: At each xk position in Region 1, starting at k=0, perform the following test,

If p1k<0, the next point along the ellipse centered on (0, 0) is (xk+1, yk) and
p1k+1=p1k+2ry2xk+1+ry2

Otherwise, the next point along the circle is (xk+1, yk-1) and p1k+1=p1k+2ry2xk+1-2rx2yk+1+ry2 with
2ry2xk+1=2ry2xk+2ry2 2rx2yk+1=2rx2yk-2rx2, and continue the Initial value of the decision parameter
in region 2 using the last point (x0,y0) calculated in region 1 as p20=ry2(x0+1/2)2+rx2(y0-1)2 -rx2ry2.

Step 5: At each yk position in region 2, starting at k=0, perform the following test,

If p2k>0, the next point along the ellipse centered on (0, 0) is (xk ,yk-1) and p2k+1=p2k-2rx2yk+1+rx2.

Otherwise, the next point along the circle is (xk+1,yk-1) and p2k+1=p2k+2ry2xk+1-2ry2yk+1+rx2 using
the same incremental calculations for x and y as in region 1.

Step 6: Determine symmetry points in the other three quadrants.

Step 7: Move each calculated pixel position (x, y) on to the elliptical path centered on (xc, yc) and plot
the co-ordinate values:

x=c+xc
y=y+yc

Step 8: Repeat the steps for region 1 until 2ry2x>2rx2y.

Step 9: Stop the program.

5
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Program:

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#define round(a) ((int)(a+0.5))
void ellip(int xcen, int ycen, int x, int y)
{
putpixel(xcen + x, ycen + y, 2);
putpixel(xcen - x, ycen + y, 2);
putpixel(xcen + x, ycen - y, 2);
putpixel(xcen - x, ycen - y, 2);
}
int main()
{
int gd = DETECT, gm;
int xcen, ycen, rx, ry;
int rx1, ry1, rx2, ry2;
int p, x, y, px, py;
void ellip(int, int, int, int);
initgraph(&gd, &gm, "");
printf("\n\n\t\t Ellipse DRAWING ALGORITHM");
printf("\n\n\nEnter the coordinates degree(Rx,Ry)::");
scanf("%d%d", &rx, &ry);
printf("Enter coordinates center(x,y)::");
scanf("%d%d", &xcen, &ycen);
rx1 = rx * rx, ry1 = ry * ry, rx2 = 2 * rx1, ry2 = 2 * ry1;
x = 0, y = ry, px = 0, py = rx2;
ellip(xcen, ycen, x, y);
p = round(ry1 - (rx1 * ry) + (0.25 * rx1));
while (px < py)
{
x++;
px += ry2;
if (p < 0)
p += ry1 + px;
else
{
y--;
py -= rx2;
p += ry1 + px - py;
}
ellip(xcen, ycen, x, y);
}
p = round(ry1 * (x + 0.5) * (x + 0.5) + rx1 * (y - 1) * (y - 1) - rx1 * ry1);
while (y > 0)
{
y--;
py -= rx2;

6
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

if (p > 0)

p += rx1 - py;

else
{
x++;
px += ry2;
p += rx1 - py + px;
}
ellip(xcen, ycen, x, y);
}
getch();
closegraph();
return 0;
}

Output:

Result:
Thus the program to implement the Bresenham's Algorithm for Ellipse is executed and the
output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

7
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 1 (c) BRESENHAM'S ALGORITHM FOR CIRCLE

Date :

Aim:

To write a C program to implement the Bresenham’s Algorithm for Circle.

Algorithm:

Step 1: Start the Program.

Step 2: Input the 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)=(o, r).

Step 3: Calculate the Initial value of the decision parameter as, P0=5/4-r.

Step 4: At each xk position, starting at k=0, perform the following test,

If pk<0, the next point along the circle centered on (0, 0) is (xk+1, yk) and pk+1=pk+2xk+1+1.

Otherwise the next point along the circle is (xk+1, yk- 1) and pk+1=pk+2xk+1-2yk+1. Where,
2xk+1=2xk+2 2yk+1=2yk-2.

Step 5: Determine symmetry points in the other seven octants.

Step 6: Move each calculated pixel position (x, y) onto the circular path centered on (xc, yc) and plot
the co-ordinate values:
x=x+xc
y=y+yc

Step 7: Repeat step 3 through 5 until x≥y.

Step 8: Stop the Program.

Program:

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void circlepoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,WHITE);
putpixel(xcenter-x,ycenter+y,WHITE);
putpixel(xcenter+x,ycenter-y,WHITE);
putpixel(xcenter-x,ycenter-y,WHITE);
8
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

putpixel(xcenter+y,ycenter+x,WHITE);
putpixel(xcenter-y,ycenter+x,WHITE);
putpixel(xcenter+y,ycenter-x,WHITE);
putpixel(xcenter-y,ycenter-x,WHITE);
}
void circle1(int xcenter,int ycenter,int radius)
{
int x=0,y=radius,p=1-radius;
circlepoints(xcenter, ycenter,x,y);
while( x < y)
{
if(p<0)
x++;
else
{
x++;
y--;
}
if(p<0)
p+=2 * x+1;
else
p+= 2 * (x-y) +1;
circlepoints(xcenter,ycenter,x,y);
}
}
void main()
{
int gdriver = DETECT, gmode;
int x1,y1,r;
initgraph(&gdriver, &gmode, "");
printf("\n\n\n\tBresanham mid-point circle generating algorithm");
printf("\n\t ------------------------------------------------- ");
printf("\n\n\nEnter the values of x-y coordinates and radius::");
scanf("%d%d%d",&x1,&y1,&r);
circle1(x1,y1,r);
getch();
closegraph();
}

9
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to implement the Bresenham’s Algorithm for Circle is executed and the
output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

10
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 1(d) MID-POINT CIRCLE ALGORITHM

Date :

Aim:

To write a C program to implement the mid-point circle algorithm for moving wheel.

Algorithm:

Step 1: Get the input of two end points (X0,Y0) and (X1,Y1).

Step 2: Calculate the difference between two end points.

dx = X1 - X0
dy = Y1 - Y0

Step 3: Based on the calculated difference in step-2, you need to identify the number of steps to put
pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.

if (dx > dy)


Steps = absolute(dx);
else
Steps = absolute(dy);

Step4: Calculate the increment in x coordinate and y coordinate.

Xincrement = dx / (float) steps;


Yincrement = dy / (float) steps;

Step 5: Put the pixel by successfully incrementing x and y coordinates accordingly and complete the
drawing of the line.

for(int v=0; v < Steps; v++)


{
x = x + Xincrement;
y = y + Yincrement;
putpixel(x,y);
}

Step 6: Input radius r and circle center (xc,yc) and obtain the first point on the circumference of
the circle centered on the origin as (x0, y0) = (0, r)

Step 7: Calculate the initial value of decision parameter as P0 = 5/4 – r

Step 8: At each XK position starting at K=0, perform the following test


11
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

If PK< 0 then next point on circle (0,0) is (XK+1,YK) and PK+1 = PK + 2XK+1 + 1

Else PK+1 = PK + 2XK+1 + 1 – 2YK+1Where, 2XK+1 = 2XK+2 and 2YK+1 = 2YK-2.

Step 9: Determine the symmetry points in other seven octants.

Step 10: Move each calculate pixel position (X, Y) onto the circular path centered on (XC,YC)
and plot the coordinate values.
X = X + XC, Y = Y + YC

Step 11: Repeat step-3 through 5 until X >= Y

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0)
dp+=(2*x)+1;
else
{
y=y-1;
dp+=(2*x)-(2*y)+1;
}

12
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

x=x+1;
}while(y>x);
getch();
}

Output:

Result:
Thus the program to implement the mid-point circle algorithm for moving wheel is executed and
the output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

13
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 2 (a) PERFORMING 2D TRANSFORMATIONS

Date :

Aim:

To write a C program to perform 2D Transformations such as translation, scaling and rotation.

Algorithm:

Step 1: Start the Program

Step 2: Initialize the graphics system.

Step 3: Input the number to vertices and their corresponding co-ordinates.

Step 4: Draw the polygon using the vertices and input type of transformation on the Polygon.

Step 5: For Translation:

• Input the translating co-ordinates tx and ty.


• Call translate () which calculates,

x1=x1+tx
y1=y1+ty
x2=x2+tx
y2=y2+ty

o Draw the polygon using the new co-ordinates.

Step 6: For Scaling:

• Input the scaling factors sx,sy and the reference co-ordinates xf,yf.
• Call scale() which calculates

x'=x. sx+x’(1-sx)
y’=y. sy+y’(1-sy)

• Draw the polygon using the new co-ordinates.

Step 7: For Rotation:

• Input the Rotating Angle and the reference co-ordinates xr,yr.


• Call rotate() which calculates

14
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

x'=xr+(x-xr)cosθ-(y-yr)sinθ
y’=yr+(x-xr)sinθ-(y-yr)cosθ

• Draw the polygon using the new co-ordinates.

Step 8: Repeat the steps until the user needs to exit.

Step 9: Stop the program.

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate(int,int);
void scale(int,int);
void rotate(float);
void main()
{
intch;
intgd=DETECT,gm;
inttx,ty,sx,sy;
float theta;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(WHITE);
setcolor(6);
outtextxy (100,88,"Object.");
rectangle(100,250,150,200);
printf("---MENU---");
printf("\n 1)Translate\n 2)Scale\n 3)Rotate");
printf("\nEnter your choice: ");
scanf("%d",&ch);
cleardevice();
switch(ch)
{
case 1:
outtextxy(10,45,"Enter value of tx and ty:");
scanf("%d %d",&tx,&ty);
translate(tx,ty);
break;
case 2:
outtextxy(10,45,"Enter the value of sx and sy:");
scanf("%d%d",&sx,&sy);
scale(sx,sy);
break;
case 3:
outtextxy(10,50,"Enter the angle for rotation: ");

15
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

scanf("%f",&theta);
rotate(theta);
break;
default: printf("you have enterd wrong choice");
break;
}
getch();
closegraph();
}
void translate(inttx,intty)
{
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
rectangle(100,250,150,200);
rectangle(100+tx,250+ty,150+tx,200+ty);
}
void scale(intsx,intsy)
{
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
rectangle(100,250,150,200);
rectangle(100*sx,250*sy,150*sx,200*sy);
}
void rotate(float theta)
{
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
intrefx,refy;
theta=theta*(3.14/180);
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);

16
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
}

Output:

Result:
Thus the program to perform 2D Transformations such as translation, scaling and rotation is
executed and the output is verified

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

17
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 2 (b) PERFORMING 2D TRANSFORMATIONS

Date :

Aim:

To write a C program for implementing the two dimensional transformation such as Shearing &
Reflection.

Algorithm:

Step 1: Start the program.

Step 2: Initialize the graphics drive and mode.

Step 3: Read the co-ordinates of 2D object, which is transformed.

Step 4: For shearing read the shearing factor and apply the shearing factor formula with respect to x
& y.

i) x’ = x + shx*y y’ = y
ii) y’=y + shy*x x’= x

Step 5: Stop the program.

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<alloc.h>
#include<stdlib.h>
intn,i,p[20],q[20];
intconvertx(int x);
intconverty(int y);
void axis(void);
voidinit(void);
void shear(void);
void reflect(void);
void main()
{
intch,gd=DETECT,gm;
initgraph(&gd,&gm,"e:\\tc\\bgi");
axis();
printf("\b");
cleardevice();
printf("Enter the number of vertices:");
18
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

scanf("%d",&n);
printf("Enter the co-ordinate center the first vertex at the end");
for(i=0;i<2*n+2;i++)
scanf("%d",&p[i]);
clrscr();
init();
getch();
do
{
clrscr();
cleardevice();
printf("\n \t\t2D-Transformation \n1.reflection\n2.shearing\n");
printf("\n\nEnteryuor choice:\n");
scanf("%d",&ch);
clrscr();
cleardevice();
switch(ch)
{
case 1:
reflect();
break;
case 2:
shear();
break;
}
}while(ch!=3);
closegraph();
}
void axis(void)
{
cleardevice();
setlinestyle(SOLID_LINE,1,1);
line(320,0,320,getmaxy());
line(0,240,getmaxx(),240);
outtextxy(325,245,"0");
line(316,40,324,40);
outtextxy(325,41,"400");
line(316,440,324,440);
outtextxy(325,441,"-400");
line(20,236,20,244);
outtextxy(19,250,"-600");
line(620,236,620,244);
outtextxy(608,250,"600");
}
voidinit()
{
cleardevice();
axis();
setcolor(YELLOW);

19
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

setlinestyle(SOLID_LINE,1,1);
for(i=0;i<2*n+2;i+=2)
q[i]=convertx(p[i]);
for(i=1;i<2*n+2;i+=2)
q[i]=converty(p[i]);
drawpoly(n+1,q);
setcolor(WHITE);
}
intconvertx(int x)
{
x=x/2;
return(x+=320);
}
intconverty(int y)
{
y=y/2;
return(y+=240-y);
}
void shear()
{
intsh[20],sho[20];
floatshx,shy,c;
printf("(1)share relate to x-axis\n");
printf("(2)share relate to y-axis\n");
printf("\n Enter your choice:");
scanf("%f",&c);
if(c==1)
{
init();
printf("\ Enter the shear factor(shx):");
scanf("%f",&shx);
for(i=1;i<2*n+2;i+=2)
sh[i]=p[i]+(shx*(p[i+1]));
for(i=0;i<2*n+2;i+=2)
sh[i]=p[i];
}
else
{
init();
printf("\enter the shear factor(shy):");
scanf("%f",);
for(i=1;i<2*n+2;i+=2)
sh[i]=(shy*p[i-1])+p[i];
for(i=0;i<2*n+2;i+=2)
sh[i]=p[i];
}
setlinestyle(DOTTED_LINE,1,1);
for(i=0;i<2*n+2;i+=2)
sho[i]=convertx(sh[i]);

20
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

for(i=1;i<2*n+2;i+=2)
sho[i]=converty(sh[i]);
drawpoly(n+1,sho);
getch();
}
void reflect()
{
inti,c,rf[20],rft[20];
printf("\n 1.x-axis reflection\n 2.y-axis reflection");
printf("\n Enter the choice:");
scanf("%d",&c);
clrscr();
cleardevice();
if(c==1)
{
for(i=1;i<2*n+2;i+=2)
rf[i]=p[i]*(-1);
for(i=0;i<2*n+2;i+=2)
rf[i]=p[i];
}
else
{
for(i=0;i<2*n+2;i+=2)
rf[i]=p[i]*(-1);
for(i=1;i<2*n+2;i+=2)
rf[i]=p[i];
}
init();
setlinestyle(DOTTED_LINE,1,1);
for(i=0;i<2*n+2;i+=2)
rft[i]=convertx(rf[i]);
for(i=1;i<2*n+2;i+=2)
rft[i]=converty(rf[i]);
drawpoly(n+1,rft);
getch();
}

21
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to program for implementing the two dimensional transformation such as
Shearing & Reflection is executed and the output is verified

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

22
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 3 (a) IMPLEMENTATION OF COHEN-SUTHERLAND 2D CLIPPING

Date :

Aim:

To write a C program to implement Cohen-Sutherland 2D Clipping.

Algorithm:

Step 1: Start the program.

Step 2: Input the endpoints p1 and p2.

Input the window co-ordinates and draw the rectangular window on the screen.

Step 3: Check the bottom endpoint of the line p1 against the left, right and bottom boundaries of the
window and find whether it is below the clip window.

Step 4: If p1 is below the clip window, find the point of intersection of the line with the bottom
boundary of the window as p1’.

Step 5: Discard the line section from p1 tp p1’.

Step 6: Check whether the point p2 is above the clip window, (ie) outside the clip window.

Step 7: If p2 is above the clip window and to the left/right of the window, find the point of
intersection of the line with the left/right boundary of the window as p2’.

Step 8: Find the final point of intersection of the line with the top boundary of the clip window as
p2’’.

Step 9: Discard the line sections from p2 to p2’ and p2’ to p2’’.

Step 10: Stop the program.

Program:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int xa,xb,ya,yb;
int encode(float x,float y)
{
return(8*(x<xa)+4*(x>xb)+2*(y<ya)+(y>yb));
}
void swap(int *a,int *b)
23
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

{
int *t=a;
a=b;
b=t;
}
void lineclip(int x1,int y1,int x2,int y2)
{
float dx,dy;
int c1,c2;
c1=encode(x1,y1);
c2=encode(x2,y2);
while(c1|c2)
{
if(c1&c2)
return;
dx=x2-x1;
dy=y2-y1;
if(c1)
{
if(c1&8)
{
y1+=dy*(xa-x1)/dx;
x1=xa;
}
else if(c1&4)
{
y1+=dx*(xb-x1)/dx;
x1=xb;
}
else if(c1&2)
{
x1+=dx*(ya-y1)/dy;
y1=ya;
}
else if(c1&1)
{
x1+=dx*(yb-y1)/dy;
y1=yb;
}
c1=encode(x1,y1);
}
else
{
if(c2&8)
{
y2+=dy*(xa-x2)/dx;
x2=xa;
}
else if(c2&4)

24
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

{
y2+=dy*(xb-xa)/dx;
x2=xb;
}
else if(c2&2)
{
x2+=dx*(yb-y2)/dy;
y2=ya;
}
else if(c2&1)
{
x2+=dx*(yb-y2)/dy;
y2=yb;
}
c2=encode(x2,y2);
}
}
line(x1,y1,x2,y2);
}
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2;
initgraph(&gd,&gm,"");
textbackground(0);
setbkcolor(1);
cout<<"\n\nCohen sutherland line clipping algorithm\n-------------------------------";
cout<<"\n\n\nEnter 4 values of clipping area::";
cin>>xa>>ya>>xb>>yb;
cout<<"\n\nEnter the first co-ordinate value::";
cin>>x1>>y1;
cout<<"\n\nEnter the second co-ordinate value::";
cin>>x2>>y2;
if(x1>x2)
swap(&x1,&x2);
if(y1>y2)
swap(&y1,&y2);
cleardevice();
setcolor(13);
outtextxy(50,50,"Before clipping");
rectangle(xa,ya,xb,yb);
line(x1,y1,x2,y2);
getch();
cleardevice();
outtextxy(50,50,"After clipping");
rectangle(xa,ya,xb,yb);
lineclip(x1,y1,x2,y2);
getch();
closegraph();
}

25
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to implement Cohen-Sutherland 2D Clipping is executed and the output is
verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

26
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 3 (b) IMPLEMENTATION OF WINDOW-TO-VIEWPORT MAPPING

Date :

Aim:

To write a C program to implement Window to View port Mapping.

Algorithm:

Step 1: Initialize the graphics system.

Step 2: Assign the co-ordinates of the window to xwmin,xwmax,ywmin,ywmax.

Step 3: Assign the co-ordinates of the view port to xvmin,xvmax,yvmin,yvmax.

Step 4: Draw the window using the window co-ordinates.

Step 5: Draw the view port using the viewport co-ordinates.

Step 6: Choose the co-ordinates of a rectangle to be drawn inside the window and to be mapped onto
the viewport.

Step 7: Draw the rectangle inside the drawn window using its co-ordinates.

Step 8: Calculate the scaling factors sx,sy as,

Sx= (xvmax-xvmin)/(xwmax-xwmin)
Sy=(yvmax-yvmin)/(ywmax-ywmin)

Step 9: To map a point at position (xw,yw) in the window into position (xv,yv) in the associated
viewport, maintain the same relative placement in the viewport as in the window using the
expressions

xv= xvmin+(xw-xwmin).sx yv= yvmin+(yw-ywmin).sy

Step 10: Obtain the co-ordinates of the rectangle which is to be mapped in the window using the
above two expressions. Draw the relative rectangle in the viewport using the mapped co-ordinates.

Step 11: Close the graphics system.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
27
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

void main()
{
float xwmin,xwmax,ywmin,ywmax;
float xvmin,xvmax,yvmin,yvmax;
float x[10],y[10],yv,xv,sx,sy;
int gd=DETECT,gm,i;
clrscr();
printf("\n\n\t\tWindow to Viewport ");
printf("\n\nEnter window-port coordinates (xwmin,ywmin,xwmax,ywmax)::");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
printf("\nEnter view-port coordinates(xvmin,yvmin,xvmax,yvmax)::");
scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
printf("\nEnter the vertices for triangle:");
for(i=0;i<3;i++)
{
printf("\nEnter coordiante(x%d-y%d):",i,i);
scanf("%f%f",&x[i],&y[i]);
}
sx=((xvmax-xvmin)/(xwmax-xwmin));
sy=((yvmax-yvmin)/(ywmax-ywmin));
initgraph(&gd,&gm,"");
outtextxy(80,30,"***Window port***");
rectangle(xwmin,ywmin,xwmax,ywmax);
for(i=0;i<2;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[2],y[2],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<3;i++)
{
x[i]=xvmin+((x[i]-xwmin)*sx);
y[i]=yvmin+((y[i]-ywmin)*sy);
}
outtextxy(150,10,"***View port***");
rectangle(xvmin,yvmin,xvmax,yvmax);
for(i=0;i<2;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[2],y[2],x[0],y[0]);
getch();
}

28
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to implement Window to View port Mapping is executed and the output is
verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

29
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 4 PERFORMING 3D TRANSFORMATIONS

Date:

Aim:

To write a C program to perform 3D Transformations such as translation, scaling, and rotation.

Algorithm:

Step 1: Initialize the graphics system.

Step 2: Input the number of vertices and their corresponding co-ordinates.

Step 3: Draw the polygon ie the 3D object and get the type of transformation to be performed.

Step 4: For Scaling,

• Input the scaling factors along each axis sx,sy,sz.


• Calculate the new co-ordinates x1=sx.x, y1=y.sy, z1=z.sz
• Draw the polygon using the new co-ordinates.

Step 5: For Translation,

• Input the translation factors tx,ty,tz.


• Calculate the new co-ordinates x1=x.tx,y1=y.ty,z1=z.tz
• Draw the polygon 3D object using the new co-ordinates.

Step 6: For Rotation,

Rotation about X-Axis


• Input the rotating angle.
• Calculate the co-ordinates

y’= ycosθ-zsinθ
z’= ysinθ+zcosθ and x’=x.

Rotation about Y-Axis, Input the rotating angle.


• Calculate the co-ordinates

z’= zcosθ-xsinθ
x’= zsinθ+xcosθ and y’=y.

Rotation about Z-Axis


• Input the rotating angle.

30
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

• Calculate the co-ordinates


x’= xcosθ-ysinθ
y’= xsinθ+ycosθ and z’=z.

Step 7: Close the execution by drawing the 3D object.

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int i,j;
void tcube()
{
for(i=1;i<=10;i++)
{
cleardevice();
rectangle(50,100,400,300);
rectangle(200-(i*12),200,250-(i*12),250);
rectangle(225-(i*12),175,275-(i*12),225);
line(200-(i*12),200,225-(i*12),175);
line(250-(i*12),200,275-(i*12),175);
line(200-(i*12),250,225-(i*12),225);
line(250-(i*12),250,275-(i*12),225);
delay(400);
}
outtextxy(200,50,"*AFTER TRANSLATION*");
getch();
cleardevice();
}
void rcube()
{
for(i=1;i<=5;i++)
{
cleardevice();
rectangle(100,100,500,300);
rectangle(200+(i*5),200-(i*2),250-(i*5),250-(i*2));
rectangle(225+(i*5),175+(i*2),275-(i*5),225-(i*2));
line(200+(i*5),200-(i*2),225+(i*5),175+(i*2));
line(250-(i*5),200-(i*2),275-(i*5),175+(i*2));
line(200+(i*5),250-(i*2),225+(i*5),225+(i*2));
line(250-(i*5),250-(i*2),275-(i*5),225+(i*2));

}
j=2;
delay(200);
for(;i<=10;i++)
{
cleardevice();
rectangle(100,100,400,300);
rectangle(200+(i*5),200-(i*2),250-(i*5),250-(i*2));
rectangle(225+(i*5),175+(i*2),275-(i*5),225+(i*2));
line(200+((i-j)*5),200-(i*2),225+((i-j)*5),175+(i*2));
line(250-((i-j)*5),200-(i*2),275-((i-j)*5),175+(i*2));
line(200+((i-j)*5),250-(i*2),225+((i-j)*5),225+(i*2));
31
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

line(250-((i-j)*5),250-(i*2),275-((i-j)*5),225+(i*2));
delay(200);
j=j+2;
}
outtextxy(250,50,"*After Rotation*");
getch();
cleardevice();
}
void scube()
{
for(i=1;i<=10;i++)
{
cleardevice();
rectangle(100,80,500,300);
rectangle(200,200-(i*8),250+(i*8),250);
rectangle(225,175-(i*8),275+(i*8),225);
line(200,200-(i*8),225,175-(i*8));
line(250+(i*8),200-(i*8),275+(i*8),175-(i*8));
line(200,250,225,225);
line(250+(i*8),250,275+(i*8),225);
delay(200);
}
outtextxy(250,50,"*After scaling*");
getch();
cleardevice();
}
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\t\t 3D TRANSFORMATIONS\n\t\t*******************\n");
printf("\n\n\tMenu:");
printf("\n\n\t1.Translation\n\t2.Rotation\n\t3.Scaling\n\t4.Exit\n");
while(ch!=0)
{
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
tcube();
break;
case 2:
rcube();
break;
case 3:
scube();
break;
case 4:
exit(0);
}
}
}

32
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to perform 3D Transformations such as translation, scaling, and rotation is
executed and the output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

33
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 5 VISUALIZING PROJECTIONS OF 3D IMAGES

Date:

Aim:

To write a C program to visualize the projections of 3D images.

Algorithm:

Step 1: Start the program.

Step 2: Initialize the graphics system


.
Step 3: Choose the co-ordinates (x, y, and z) for all edges of the cube and store them in a 2D Array.

Step 4: Draw the cube using its co-ordinates.

Step 5: Input the axis along which projection is to be done,(ie) x or y or z axis.

Step 6: Enter the reference value about which the perspective projection is to be viewed.

Step 7: Calculate the projection points using the co-ordinates and the reference value.

Step 8: Display the perspective projection of the 3D image along the axis.

Step 9: Stop the Program

Program:

#include<conio.h>
#include<graphics.h>
#include<math.h>
int midx,midy,maxx,maxy;
void main()
{
int gd,gm,x,y,z;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\tc\\bgi");
setfillstyle(3,2);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);

34
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

bar3d(midx-250,midy+150,midx-150,midy+225,20,4);
printf("\n Enter the projection vector:\t");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx-(x*5),midy-(y*9),midx-(x*3),midy-(y*5),10*z,4);
getch();
Closegraph ();
}

Output:

Result:
Thus the program to visualize the projections of 3D images is executed and the output is
verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

35
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No : 6 (a) CONVERSION BETWEEN RGB TO HSV COLOR MODEL

Date :

Aim:

To write a C program to convert RGB Color model into HSV Color model.

Algorithm:

Step 1: Start the program.

Step 2: Input the R, G and B values.

Step 3: Find the maximum and minimum of the R, G, and B values.

Step 4: Assign the maximum value to V.

Step 5: If the maximum value is not 0, calculate S as S= (max-min)/max.

Step 6: If the maximum value is 0, then assign the value 0 to S, and -1 to H.

Step 7: If R is the maximum of R, G, and B calculate H as H = (G-B)/ (max-min).

Step 8: If Value of G is maximum, calculate H as H= 2+ (B-R)/ (max-min).

Step 9: If B is maximum, Calculate H as H = 4+(R-G)/ (max-min).

Step 10: Multiply H by 60.

Step 11: If H is less than 0, add 360 to H.

Step 12: Divide H by 360.

Step 13: Print the values of H, S and V.

Step 14: Stop the program.

Program:

#include <stdio.h>
#include <conio.h>
float MIN(float a, float b)
{
return (a < b ? a : b);
}

36
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

float MAX(float a, float b)


{
return (a > b ? a : b);
}
float NO_HUE = -1;
void rgbtohsv(float r, float g, float b, float *h, float *s, float *v)
{
float max = MAX(r, MAX(g, b)), min = MIN(r, MIN(g, b));
float delta = max - min;
*v = max;
if (max != 0.0)
*s = delta / max;
else
*s = 0.0;
if (*s == 0.0)
*h = NO_HUE;
else
{
if (r == max)
*h = (g - b) / delta;
else if (g == max)
*h = 2 + (b - r) / delta;
else if (b == max)
*h = 4 + (r - g) / delta;
*h *= 60.0;
if (*h < 0)
*h += 360.0;
*h /= 360.0;
}
}
int main()
{
float a, b, c, h, s, v;
clrscr();
printf("\n Enter the RGB values:\n");
scanf("%f%f%f", &a, &b, &c);
printf("\nThe HSV values:\n");
rgbtohsv(a, b, c, &h, &s, &v);
printf("\n H=%f\n S=%f\n V=%f", h, s, v);
getch();
return 0;
}

37
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to convert RGB Color model into HSV Color model is executed and the output
is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

38
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 6 (b) CONVERSION BETWEEN HSV TO RGB COLOR MODEL

Date :

Aim:

To write a C program to convert RGB Color model into HSV Color model.

Algorithm:

Step 1: Start the program.

Step 2: Input the H, S, and V values.

Step 3: If value of S is 0, assign the value of V to R, G and B.

Step 4: If S is not 0, perform the following steps,

• If H is 1, assign the value of 0 to H.


• Multiply H by 60. Round the value of H to the nearest integer as assign it to ‘i’.
• Calculate f as the difference between H and i.
• Calculate the following values:

aa =v (1-s)
bb =v (1-sf)
cc =v (1-s (1-f))

• Based on the value of I, assign the values of R, G and B with aa, bb, and cc values.

Step 5: Print the values of R, G, and B.

Step 6: Stop the program

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void hsvtorgb(float h,float s,float v)
{
float *r,*g,*b;
int i;
float aa,bb,cc,f;
if(s==0)
{
*r=*g=*b=v;
printf("R=%f\tG=%f\tB=%f\n",*r,*g,*b);
}
else
{
if(h==1.0)
39
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

h=0;
h*=6.0;
i=floor(h);
f=h-i;
aa=v*(1-s);
bb=v*(1-(s*f));
cc=v*(1-(s*(1-f)));
switch(i)
{
case 0:
*r=v;*g=cc;*b=aa;
printf("R=%f\nG=%f\nB=%f\n",*r,*g,*b);
break;
case 1:
*r=bb;*g=v;*b=aa;
printf("R=%f\nG=%f\nB=%f\n",*r,*g,*b);
break;
case 2:
*r=aa;*g=cc;*b=v;
printf("R=%f\nG=%f\nB=%f\n",*r,*g,*b);
break;
case 3:
*r=aa;*g=bb;*b=v;
printf("R=%f\nG=%f\nB=%f\n",*r,*g,*b);
break;
case 4:
*r=cc;*g=aa;*b=v;
printf("R=%f\nG=%f\nB=%f\n",*r,*g,*b);
break;
case 5:
*r=v;*g=aa;*b=bb;
printf("R=%f\nG=%f\nB=%f\n",*r,*g,*b);
break;
}
}
}
void main()
{
float a,b,c;
clrscr();
printf("\n Enter the HSV values:\n");
scanf("%f%f%f",&a,&b,&c);
printf("\nThe RGB values:\n");
hsvtorgb(a,b,c);
getch();
}

40
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to convert RGB Color model into HSV Color model is executed and the
output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

41
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 6(c) IMPLEMENTATION OF TEXT COMPRESSION ALGORITHM

Date :

Aim:

To write a C program to compress a Text file using Text Compression Algorithm.

Algorithm:

Step 1: Start the program.

Step 2: Open the source Text file to be compressed and the Destination file to store the compressed
version of the source.

Step 3: Read the characters from the source file one by one.

Step 4: Check for the redundancy of the same read character in the consecutive character stream till
different character is read.

Step 5: Write the character and the number of its consecutive occurrence in the destination file.

Step 6: Continue the process until the source reaches the end-of-File character.

Step 7: Close the source and destination files.

Step 8: Stop the program.

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50],dl;
int i,j,flag=0,countindex=0,index=-1,count=0,size;
FILE *ip,*op;
clrscr();
printf("\n\n Enter the text for input:");
scanf("%s",a);
size=strlen(a);
ip=fopen("source","w");
fprintf(ip,"%s",a);
printf("\n Input file length:");
printf("%d",ftell(ip));
fclose(ip);

42
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

printf("\n");
ip=fopen("compress.dat","w");
for(i=0;i<size;i++)
{
j=i+1;
if(a[j]==a[i])
count=count+1;
else
{
if(count>0)
{
b[++index]=a[i];
fprintf(ip,"%c",b[index]);
b[++index]='#';
fprintf(ip,"%c",b[index]);
b[++index]=count+1;
fprintf(ip,"%c",b[index]);
count=0;
}
else
{
b[++index]=a[i];
fprintf(ip,"%c",b[index]);
}
countindex=index;
}
}
fclose(ip);
op=fopen("compress.dat","r+");
printf("\n\n Compressed output\n ");
for(i=0;i<size;i++)
{
fscanf(op,"%c",b[i]);
if(b[i]=='#')
{
flag=1;
printf("%c",b[i]);
continue;
}
else if(flag==1)
{
printf("%d",b[i]);
flag=0;
}
else
printf("%c",b[i]);
}
printf("\n Compressed file length:");
printf("%d",ftell(op));

43
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

fclose(op);
flag=0;
op=fopen("compress.dat","r+");
ip=fopen("decompress.dat","w");
printf("\n\n\n Decompressed output:\n ");
for(i=0;i<=countindex;i++)
{
fscanf(op,"%c",b[i]);
if(b[i]=='#')
{
flag=1;
continue;
}
else if(flag==1)
{
for(j=0;j<b[i]-1;j++)
{
fprintf(ip,"%c",dl);
printf("%c",dl);
}
flag=0;
}
else if(b[i]!='\0')
{
fprintf(ip,"%c",b[i]);
printf("%c",b[i]);
dl=b[i];
}
}
printf("\n Decompressed Length:");
printf("%d",ftell(ip));
fcloseall();
getch();
}

44
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

Result:
Thus the program to compress a Text file using Text Compression Algorithm is executed and
the output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

45
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 7(a) SIMPLE PRESENTATION USING MOTION TWEEN

Date:

Aim:

To write a procedure to represent motion tween using Macromedia Flash.

Procedure:

• Create a view presentation using motion tween.

• Draw any object in the workspace and fix its critical place.

• The frames on the timeline are copied from the initial frame up to the frame by the right
clicking of mouse and choosing copy frames.

• The object is moved to its target position from its initial position as soon as the final frame
are being copied in the timeline.

• The object is provided with a motion or any movements anywhere the motion tween is
created for that object, so right click the mouse and choose create motion tween in the last
but one frame.

• This creates the arrow head that notifies the duration of animation frame. The first frame
position to the last frames position.

• Then press cntrl+Enter keys or select text scene from control menu to view the animation.

• Thus the motion tween is created.

46
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

47
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Result:
Thus the procedure to represent motion tween using Macromedia Flash is executed and the
output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

48
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 7(b) IMAGE EDITING AND MANIPULATION USING ADOBE PHOTOSHOP

Date :

Aim:

To Edit and Manipulate an Image using Adobe Photoshop.

Procedure:

1. Start ->Photoshop and open the image from the drive and try out the different tools.

1. Rotate
Image – Image Rotation – try out the different options.

2. Resize
Choose in the menu Image – Image Size – e.g. under Pixel Dimensions (e.g. percentage-
wise alteration) or Document Size (cm). You can change the resolution of the image in the
Resolution field.

3. Crop
Select the Crop Tool from the Tool-palette. Click and drag diagonally with the mouse
over the image, and double-click in the marked area, when you have the segment you wish to keep.

4. Zoom
Select the Zoom Tool from the Tool-palette. Zoom doesn’t change the actual size. Hold
down the ALT-key to zoom out again (- in the magnifying glass).

5. Brightness/Contrast
If the image is a little too dark, you can adjust the brightness and contrast under,
Adjustments in the menu Image, Brightness and Contrast or Shadow/Highlight.

6. Right the scene


If a scene is oblique you can right it by selecting the entire image Select - All. Next
choose Edit – Free Transform and right the scene by moving the mouse out of the selection square,
till the mouse changes it’s appearance to a bended arrow.

7. Filter
There are many different ways to transform pictures into artworks in Photoshop. Try one
of the many filters on the image such as Liquify, Noise, Texture, Render Difference clouds etc.

Color Change, Image Extraction and Merging of Images

i. Change color
a. Select in the menu Image – Adjustments – Replace Color.
b. Mark the button Image under the motif.
49
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

c. With the pipette (Eyedropper Tool) collect the most dominant color of the image.
d. Under the headline Replace, select a substitute color, by dragging the sliding bar in Hue,
Saturation and Lightness.
e. Then select (pipette with a +) and select the additional color shades and click OK.

ii. Extracting image from background


a. Select the Magic Wand Tool from the Tool-palette and set the Tolerance to 30 – a higher
value, will select a larger color area.
b. Click in the white area. Adjoining areas can be included in the marking by holding down the
SHIFT-key and simultaneously clicking with the Magic Wand in the area. If a too large area
has been marked, you can exclude an area by holding down the ALT-key.
c. Select the Background Eraser Tool and adjust the thickness and shape of the eraser. Erase the
background – pay attention to missed spots in the check pattern that appears. (An alternative
could be to select the Quick Selection Tool or the Magic Eraser Tool)

iii. Reduce the size of the image


a. Image – Image Size – use the adjustments of the width in % of the original. If the size is not as
wanted, select Undo from the Edit menu and try again.

iv. Copy one image to another image


a. Select the Rectangular Marquee Tool from the Tool-palette.
b. Draw a square around the motif.
c. Select in the menu Edit – Copy.
d. Click the other image and select Edit – Paste. The first image is now placed in a new layer.
e. Select the Move Tool in the Tool-palette and move the image to a required position.

Text on Images

a. Open the desired image.


b. Select the Horizontal Type Tool from the Tool-palette and try the different text attributes on
the Properties-palette.
c. Click (or drag to create a textbox) where you want the text to be placed, and type the desired
text.
d. Save all the images as when the images are edited and manipulated.

CREATION OF GIF ANIMATED IMAGES


Procedure:
1. Import images into Photoshop. (Open the image with Adobe Photoshop)
2. Create new layer and copy each image to the layer.
3. Adjustments can be made if necessary.
4. Save the file.
5. File->Jump To ->Adobe Image Ready 7.0 or by clicking the icon from Tools.
6. Open Animation pane from Window option.
7. Choose Make Frames from Layers option by clicking on the double right arrow on the top right
50
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

corner of the animation pane.


8. The layers created in Photoshop are converted to frames in Image Ready.
9. Next go to File->Save optimized As and save the file in gif format.
10. Open the file in browser.

IMAGE OPTIMIZATION USING ADOBE PHOTOSHOP

Procedure:
1. Open Adobe Photoshop
2. Go to File->Open and select any image from your computer.
3. Now go to File->Save for web option
4. Click the original tab to view the size, download, speed, quality of the original image
5. Click optimized tab to enter custom values for width and height on the right pane of the window
6. Go to 4-up and pick any optimized image ,its download option and click save button.
7. A save optimized dialog box will open, enter a new name and click save.

Output:

51
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

52
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

53
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

54
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

55
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

56
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

57
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Result:
Thus the program to implement and create 3D object and scene using OpenGL is executed and
the output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

58
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 7(c) MASKING, PENDULUM USING MACROMEDIA FLASH

Date:

Aim:

To perform Masking, Pendulum operations using macromedia flash.

Procedure:

Masking:
• Select the oval tool from the tool bar and draw a ball of any color.
• Select the destination frame in the frame in the layer and right click on it and give insert key
frame. Then right click and select Create motion Tween in order to enable the motion of the ball.
• In the same destination frame, move the ball to the desired location.
• Right click on Timeline and insert a new layer.
• Add some text in the new layer and select Mask by right clicking it.
• Check that layer 2 has text and layer 1 has the ball.
• Select layer1 and then press Enter or select play from Control.

Output:

59
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Pendulum:
• Select the oval tool from the tool bar to draw a circle.
• Draw a straight line using the line tool from the tool bar.
• Select both circle and line and then set the center point at the top of the line.
• Using transformation set the motion of the
• Select insert key frame at the destination frame and then select Create Motion tween.
pendulum.
• Press Enter or Select play from control.

Output:

Result:
Thus the Masking, Pendulum operations using macromedia flash is executed and the output is
verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

60
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Ex. No: 8 2D ANIMATION

Date :

Aim:

To develop programs for making simple animations using transformations like rotation, sealing and
translation. The simple animations are given below:

Steps:

i. For moving any object, we incrementally calculate the object coordinates and redraw the i. picture
to give a feel of animation by using for loop.
ii. Suppose if we want to move a circle from left to right means, we have to shift the position of
circle along x- direction continuously in regular intervals.
iii. The below programs illustrate the movement of objects by using for loop and also using
transformations like rotation, translation etc.
iv. For windmill rotation, we use 2D rotation concept and formulas.

Program for moving Car in different directions

#include <graphics.h
#include <dos.h>
#include <conio.h>
main()
int i,j 0, gd DETECT, gm;
initgraph(&gd&gm, "C:\TURBOC3 BGI");
settextstyle(DEFAULT FONT HORIZ DIR,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
setviewport(0,0,639,440,1);
for(i=0;i< 420;i=i+10, j++)
rectangle(50+1,275,150+1,400);
rectangle(150+1,350,200+ 1.400);
circle(75 +1,410,10); circle(175+1,410,10);
setcolor(j);
delay(100);
if(i 420)
break;
clearviewport();
getch();
cleardevice();
outtextxy(100,200,"Project by http://www.turboc.codeplex.com"):
delay(5000);
closegraph():
return 0;

61
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Output:

62
Department of Computer Science and Engineering 18PD14 Computer Graphics and Multimedia Laboratory

Result:
Thus the program to develop programs for making simple animations using transformations
like rotation, sealing and translation is executed and the output is verified.

PREPARATION 30

LAB PERFORMANCE 30

REPORT 40

TOTAL 100

INITIAL OF THE FACULTY

63

You might also like