ComputerGraphicsPRACTICALS1
ComputerGraphicsPRACTICALS1
Viewport
3. Composite 2D Transformations
4. Line Clipping
7. Creating 3D Scenes.
8. Image Editing and Manipulation - Basic Operations on image using any image editing
REFERENCE:
spoken-tutorial.org
2D Geometric transformations
Composite 2D transformations
Line Clipping
3D Transformations
3D Projections
Creating 3D scenes.
2D Animation
EX.NO: 1 IMPLEMENTATION OF ALGORITHMS FOR DRAWING 2D
PRIMITIVES
DATE:
AIM:
To write a C program for draw a line using DDA line Algorithm.
ALGORITHM:
1. Start the program.
2. Enter the starting and ending point of the line.
3. Call the initgraph() function.
4. Invoke the function draw, and calculate the absolute value of dx and dy and
check if abs(dx)>abs(dy).
5. If true assign step size as abs(dx), or else assign as abs(dy).
6. Calculate the x in c, y in c and plot the start point use the loop, until k is less
than or equal to step size.
7. Calculate the x and y for each steps and plot the corresponding pixels and let the
line be displayed.
8. Stop the graphics driver.
9. Stop the program.
PROGRAM:
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
# include <ctype.h>
# include <math.h>
# include <stdlib.h>
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(abs(x),abs(y),111);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(abs(x),abs(y),111);
}
}
OUTPUT:
Enter the x and y value for starting point: 50 60
Enter the x and y value for ending point: 70 90
RESULT:
Thus the program in C to draw the line by using DDA algorithm was written and
executed successfully.
b) BRESENHAM’S ALGORITHM FOR LINE DRAWING
AIM:
To write a program in C to draw a line using Bresenham’s algorithm.
ALGORITHM:
1. Start the program.
2. Initialize the variables.
3. Call the initgraph() function.
4. Input the two line end-points and store the left end-point in (x0, y0).
5. Plot the point (x0, y0).
6. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the
decision parameter as:
7. 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
8. Otherwise, the next point to plot is (xk+1, yk+1) and Repeat step 4 (Δx – 1) times.
xypΔ−Δ=20yppkkΔ+=+21xyppkkΔ−Δ+=+221
9. Stop the graphics driver.
10. Stop the program.
PROGRAM:
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main( )
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2dy-dx);
while(x<=x2)
{
if(p<0)
{
x=x+1;
p=2*x-dx;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();
}
OUTPUT:
Enter the x-coordinate of the first point ::40
Enter the y-coordinate of the first point ::50
Enter the x-coordinate of the second point ::60
Enter the y-coordinate of the second point ::80
RESULT:
Thus the program in C to draw the line by using Bresenham’s algorithm was done
successfully.
c) BRESENHAM’S ALGORITHM TO DRAW A CIRCLE
AIM:
To write a program in C to draw a circle using Bresenham’s Algorithm.
ALGORITHM:
1. Start the program.
2. Initialize the variables.
3. Call the initgraph() function.
4. Input radius r and circle centre (xc, yc), then set the coordinates for the first
point on the circumference of a circle centred on the origin as:
5. Calculate the initial value of the decision parameter as:
6. Starting with k = 0 at each position xk, perform the following test. If pk < 0,
the next point along the circle centred on (0, 0) is (xk+1, yk) and rp−=450),
7. Otherwise the next point along the circle is (xk+1, yk-1) and
8. Determine symmetry points in the other seven octants
9. Move each calculated pixel position (x, y) onto the circular path centred at
(xc, yc) to plot the coordinate values:
10. Repeat steps 3 to 5 until x >= y
11. Stop the graphics driver.
12. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlepoints(int,int);
void main( )
{
int x,y,p,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
clrscr();
printf("Enter the radius");
scanf("%d",&r);
x=0;y=r;p=1-r;
while(x<y)
{
x++;
if(p>0)
{
p=p+2*(x-y)+1;
y--;
}
else
p=p+2*x+1;
circlepoints(x,y);
}
getch();
closegraph();
}
OUTPUT:
Enter the radius 50
RESULT:
Thus the program in C to draw the circle by using Bresenham’s algorithm was
done successfully.
VIVA QUESTIONS AND ANSWERS
2. Define pixel.
Pixel is shortened forms of picture element. Each screen point is referred to as pixel or
pel.
5. Define Circle.
Circle is defined by its center xc, yc and its radius in user coordinate units. The equation
of the circle is (x-xc) +(y-yc) = r2.
6. Define Ellipse.
An ellipse can use the same parameters xc, yc ,r as a circle, in addition to the eccentricity
e. the eqn of an ellipse is: (x-xc)2/a2 + (y-yc)2/b2 = 1
Paint Programs: It used to draw common shapes such as lines, circle and ovals
Computer games: Vector Graphics may use some programs such as Adobe Illustrator for
making computer games
Interactive graphical user interfaces (Mouse Pointer).
Graphic Design.
EX.NO: 2 IMPLEMENTATION OF 2D GEOMETRIC
TRANSFORMATIONS
DATE:
AIM:
To implement the following 2D Geometric Transformations
a. Translation b. Rotation c. Scaling d. Reflection e. Shearing
f. Window-View Port
ALGORITHM:
1. Get the coordinates of triangle (x1, y1, x2, y2, x3, y3).
2. Draw the original triangle.
3. Print the menu for choosing 2D Geometric Transformation.
a. If users choose translation then get the translation factors(x, y) and draw the translated triangle
in the following coordinates (x1+x, y1+y, x2+x, y2+y, x3+x, y3+y).
b. (i) If user choose rotation then get the rotation angle (t) and reference point of the rotation
(rx, ry).
(ii) Change the t value to t = t * (3.14 / 180) and calculate the rotated coordinates by the
following formulae rx1 = rx + (x1 - rx) * cos (t) - (y1 - ry) * sin (t); ry1 = ry + (x1 - rx) *
sin (t) + (y1 - ry) *cos (t);
(iii) Similarly calculate the coordinates rx2, ry2, rx3, ry3 and draw the rotated triangle in the
following coordinates (rx1, ry1, rx2, ry2, rx3, ry3).
c. If user choose scaling then get the scaling factors(x, y) and draw the scaleded triangle in the
following coordinates (x1*x, y1*y, x2*x, y2*y, x3*x, y3*y).
d. If user choose reflection then rotate the triangle in 1800 at (x2, y2) and draw the rotated
triangle (which is reflected triangle).
e. If user choose shearing then get the shear value and draw the sheared triangle in the following
coordinates (x1, y1, x2+x, y2, x3, y3).
f. (i) if user choose window-view port then draw the rectangle in window port coordinates (w1,
w2, w3, w4) and draw the original triangle.
(ii) Calculate the x, y and view port coordinates by following formulae
x = (v3 - v1) / (w3 - w1);
y = (v4 - v2) / (w4 - w2);
vx1 = v1 + floor (((x1 - w1) * x) + 0.5);
vy1 = v2 + floor (((y1 - w2) * y) + 0.5);
(iii) Similarly calculate the coordinates vx2, vy2, vx3, vy3
(iv) Draw the rectangle in view port coordinates (v1, v2, v3, v4) and draw the triangle in view
Port by the following coordinates (vx1, vy1, vx2, vy2, vx3, vy3)
PROGRAM:
#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
float x1, y1, x2, y2, x3, y3, x, y, rx1, ry1, rx2, ry2, rx3, ry3, t, vx1, vy1, vx2, vy2, vx3, vy3;
floatw1 = 5, w2 = 5, w3 = 635, w4 = 465, v1 = 425, v2 = 75, v3 = 550, v4 = 250;
intgd, gm, ch;
void original ();
void triangle (float, float, float, float, float, float);
void rotate (float, float, float);
void main ()
{
clrscr ();
cout<< "\n\t\t ***** 2D Geometric Transformations *****";
cout<< "\n\n Enter the coordinates of triangle (x1,y1,x2,y2,x3, y3): \n";
cin>> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
original ();
closegraph ();
do
{
cout<< "\n\n Choose any one Transformation : ";
cout<< "\n\t 1.Translation \n\t 2.Rotation \n\t 3.Scaling \n\t 4.Reflection";
cout<< "\n\t 5.Shearing \n\t 6.Window-Viewport";
cout<< "\n\n Enter your choice : \t";
cin>>ch;
switch (ch)
{
case 1:
cout<< "\n Enter the translation factors (x,y): \t\t";
cin>> x >> y;
original ();
triangle (x1+x, y1+y, x2+x, y2+y, x3+x, y3+y);
closegraph ();
break;
case 2:
cout<< "\n Enter the angle of rotation : \t\t";
cin>> t;
cout<< "\n Enter the reference point of rotation (rx,ry) : \t";
cin>> x >> y;
original ();
rotate (t, x, y);
closegraph ();
break;
case 3:
cout<< "\n Enter the scaling factors (x,y): \t\t";
cin>> x >> y;
original ();
triangle (x1*x, y1*y, x2*x, y2*y, x3*x, y3*y);
closegraph ();
break;
case 4:
original ();
rotate (180, x2, y2);
closegraph ();
break;
case 5:
cout<< "\n Enter the Shear Value : \t\t";
cin>> x;
original ();
triangle (x1, y1, x2+x, y2, x3, y3);
closegraph ();
break;
case 6:
initgraph (&gd, &gm, "C:\\TC\\BGI");
rectangle (w1, w2, w3, w4);
outtextxy (300, 10, "Window Port");
triangle (x1, y1, x2, y2, x3, y3);
x = (v3 - v1) / (w3 - w1);
y = (v4 - v2) / (w4 - w2);
vx1 = v1 + floor (((x1 - w1) * x) + 0.5);
vy1 = v2 + floor (((y1 - w2) * y) + 0.5);
vx2 = v1 + floor (((x2 - w1) * x) + 0.5);
vy2 = v2 + floor (((y2 - w2) * y) + 0.5);
vx3 = v1 + floor (((x3 - w1) * x) + 0.5);
vy3 = v2 + floor (((y3 - w2) * y) + 0.5);
rectangle (v1, v2, v3, v4);
outtextxy (450, 85, "View Port");
triangle (vx1, vy1, vx2, vy2, vx3, vy3);
closegraph ();
}
} while (ch<= 6);
getch ();
}
void original ( )
{
initgraph (&gd, &gm, "C:\\TC\\BGI");
triangle (x1, y1, x2, y2, x3, y3);
}
void triangle (float x1, float y1, float x2, float y2, float x3, float y3)
{
line (x1, y1, x2, y2);
line (x2, y2, x3, y3);
line (x3, y3, x1, y1);
getch ();
}
OUTPUT:
(If we choose 6 then we get)
(If we choose the number is in greater than 6 then we exit)
RESULT:
Thus the implementation of the 2D Geometric Transformations (translation, rotation,
scaling, reflection, shearing, and window-view port) has been implemented and the output was
verified.
VIVA QUESTIONS
1. What is Transformation?
Transformation is the process of introducing changes in the shape size and orientation of the
object using scaling rotation reflection shearing & translation etc.
2. What is translation?
Translation is the process of changing the position of an object in a straight-line path from
one coordinate location to another. Every point (x, y) in the object must undergo a
displacement to (x´,y´). the transformation is:
x´ = x + tx
y´ = y+ty
3. What is rotation?
A 2-D rotation is done by repositioning the coordinates along a circular path, in X = rcos (q +
f) and Y = r sin (q + f).
4. What is scaling?
The scaling transformations changes the shape of an object and can be carried out by
multiplying each vertex (x,y) by scaling factor Sx,Sy where Sx is the scaling factor of x and
Sy is the scaling factor of y.
5. What is shearing?
The shearing transformation actually slants the object along the X direction or the Y direction
as required.ie; this transformation slants the shape of an object along a required plane.
6. What is reflection?
The reflection is actually the transformation that produces a mirror image of an object. For
this use some angles and lines of reflection.
AIM:
To implement the following Composite 2D Transformations in the following order:
a. Shearing b. Rotation c. Translation d. Reflection
e. Scaling f. Window - View Port Transformation
ALGORITHM:
1. Get the coordinates of triangle (x1, y1, x2, y2, x3, y3).
2. Draw the original triangle.
3. Get the shear value and draw the sheared triangle in the following coordinates (x1, y1, x2+x,
y2, x3, y3).
4. Get the rotation angle (t) and reference point of the rotation (rx, ry)
a) Change the t value to t = t * (3.14 / 180) and calculate the rotated coordinates by the following
formulae
rx1 = rx + (x1 - rx) * cos (t) - (y1 - ry) * sin (t);
ry1 = ry + (x1 - rx) * sin (t) + (y1 - ry) * cos (t);
b) Similarly calculate the coordinates rx2, ry2, rx3, ry3
c) Draw the rotated triangle in the following coordinates (rx1, ry1, rx2, ry2, rx3, ry3).
5. Get the translation factors(x, y) and draw the translated triangle in the following coordinates
(x1+x, y1+y, x2+x, y2+y, x3+x, y3+y).
6. Rotate the triangle in 1800 at (x2, y2) and draw the rotated triangle (which is reflected
triangle).
7. Get the scaling factors(x, y) and draw the scaled triangle in the following coordinates (x1*x,
y1*y, x2*x, y2*y, x3*x, y3*y).
8. (i) Draw the rectangle in window port coordinates (w1, w2, w3, w4) and draw the original
triangle.
(ii) Calculate the x, y and view port coordinates by following formulae:
x = (v3 - v1) / (w3 - w1);
y = (v4 - v2) / (w4 - w2);
vx1 = v1 + floor (((x1 - w1) * x) + 0.5);
vy1 = v2 + floor (((y1 - w2) * y) + 0.5);
(iii) Similarly calculate the coordinates vx2, vy2, vx3, vy3.
(iv) Draw the rectangle in view port coordinates (v1, v2, v3, v4) and draw the triangle in
View port by the following coordinates (vx1, vy1, vx2, vy2, vx3, vy3).
PROGRAM:
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<math.h>
float x1,y1,x2,y2,x3,y3,x,y,rx1,ry1,rx2,ry2,rx3,ry3,rx,ry,t,w1=5,w2=5,w3=635,w4=465,v1=425,
v2=75,v3=550,v4=250;
int gd,gm,i;
void triangle(float,float,float,float,float,float);
void rotate(float,float,float);
void main()
{
clrscr();
cout<<"\n\t\t***** Composite 2D Transformations *****";
cout<<"\n\nEnter the coordinates of triangle (x1,y1,x2,y2,x3,y3) : ";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
initgraph(&gd,&gm,"C:\\TC\\BGI");
triangle(x1,y1,x2,y2,x3,y3);
closegraph();
cout<<"\n\nEnter the Shear Value : \t\t";
cin>>x;
initgraph(&gd,&gm,"C:\\TC\\BGI");
triangle(x1,y1,x2,y2,x3,y3);
x2=x2+x;
triangle(x1,y1,x2,y2,x3,y3);
closegraph();
cout<<"\n\nEnter the angle of rotation : \t\t";
cin>>t;
cout<<"\nEnter the reference point of rotation (rx,ry) : \t";
cin>>rx>>ry;
initgraph(&gd,&gm,"C:\\TC\\BGI");
triangle(x1,y1,x2,y2,x3,y3);
rotate(t,rx,ry);
closegraph();
cout<<"\n\nEnter the translation factors (x,y): \t\t";
cin>>x>>y;
initgraph(&gd,&gm,"C:\\TC\\BGI");
triangle(x1,y1,x2,y2,x3,y3);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;
x3=x3+x;
y3=y3+y;
triangle(x1,y1,x2,y2,x3,y3);
closegraph();
initgraph(&gd,&gm,"C:\\TC\\BGI");
triangle(x1,y1,x2,y2,x3,y3);
rotate(180,x2,y2);
closegraph();
cout<<"\n\nEnter the scaling factors (x,y): \t\t";
cin>>x>>y;
initgraph(&gd,&gm,"C:\\TC\\BGI");
triangle(x1,y1,x2,y2,x3,y3);
x1=x1*x;
y1=y1*y;
x2=x2*x;
y2=y2*y;
x3=x3*x;
y3=y3*y;
triangle(x1,y1,x2,y2,x3,y3);
closegraph();
initgraph(&gd,&gm,"C:\\TC\\BGI");
rectangle(w1,w2,w3,w4);
outtextxy(300,10,"Window Port");
triangle(x1,y1,x2,y2,x3,y3);
x=(v3-v1)/(w3-w1);
y=(v4-v2)/(w4-w2);
x1=v1+floor(((x1-w1)*x)+0.5);
y1=v2+floor(((y1-w2)*y)+0.5);
x2=v1+floor(((x2-w1)*x)+0.5);
y2=v2+floor(((y2-w2)*y)+0.5);
x3=v1+floor(((x3-w1)*x)+0.5);
y3=v2+floor(((y3-w2)*y)+0.5);
rectangle(v1,v2,v3,v4);
outtextxy(450,85,"View Port");
triangle(x1,y1,x2,y2,x3,y3);
}
void triangle(float x1,float y1,float x2,float y2,float x3,float y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
}
OUTPUT:
RESULT:
Thus the program to implementation of composite 2D transformations has been
implemented and the output was verified.
VIVA QUESTIONS
4. What is Transformation?
Transformation is the process of introducing changes in the shape size and orientation of the
object using scaling rotation reflection shearing & translation etc.
5. What is translation?
Translation is the process of changing the position of an object in a straight-line path from
one coordinate location to another. Every point (x, y) in the object must undergo a
displacement to (x´,y´). the transformation is:
x´ = x + tx
y´ = y+ty
6. What is rotation?
A 2-D rotation is done by repositioning the coordinates along a circular path, in X = rcos (q +
f) and Y = r sin (q + f).
7. What is scaling?
The scaling transformations changes the shape of an object and can be carried out by
multiplying each vertex (x,y) by scaling factor Sx,Sy where Sx is the scaling factor of x and
Sy is the scaling factor of y.
8. What is shearing?
The shearing transformation actually slants the object along the X direction or the Y direction
as required.ie; this transformation slants the shape of an object along a required plane.
APPLICATIONS
• Video Games
• Simulators (flight, driving)
• Virtual reality
• Web Design
• Advertising
EX.NO: 4 LINE CLIPPING
DATE:
AIM:
To write a program to implement the line clipping.
ALGORITHM:
1. Get the clip window coordinates.
2. Get the line end points.
3. Draw the window and the line.
4. Remove the line points which are plotted in outside the window.
5. Draw the window with clipped line.
PROGRAM:
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
float x1,y1,x2,y2,wx1,wy1,wx2,wy2,m;
int gd,gm;
void main()
{
clrscr();
cout<<"\nEnter the clip window coordinates : ";
cin>>wx1>>wy1>>wx2>>wy2;
cout<<"\nEnter the line end points : ";
cin>>x1>>y1>>x2>>y2;
m=(y2-y1)/(x2-x1);
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"\n\n\t\t\t\tBefore Clipping";
rectangle(wx1,wy1,wx2,wy2);
line(x1,y1,x2,y2);
getch();
closegraph();
if(x1<wx1)
{
y1=y1+(wx1-x1)*m;
x1=wx1;
}
if(x2>wx2)
{
y2=y2-(x2-wx2)*m;
x2=wx2;
}
if(y2>wy2)
{
x2=x2-((y2-wy2)/m);
y2=wy2;
}
if(y1<wy1)
{
x1=x1+(wy1-y1)/m;
y1=wy1;
}
if(x2<wx1)
{
y2=y2+(wx1-x2)*m;
x2=wx1;
}
if(x1>wx2)
{
y1=y1-(x1-wx2)*m;
x1=wx2;
}
if(y2<wy1)
{
x2=x2+((wy1-y2)/m);
y2=wy1;
}
if(y1>wy2)
{
x1=x1-((y1-wy2)/m);
y1=wy2;
}
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"\n\n\t\t\t\tAfter Clipping";
rectangle(wx1,wy1,wx2,wy2);
line(x1,y1,x2,y2);
getch();
}
OUTPUT:
Enter the clip window coordinates: 200 200 400 400
Enter the line end points: 150 150 350 500
RESULT:
Thus the program to implementation of the line clipping has been implemented and the
output was verified.
VIVA QUESTIONS
1. Define clipping
Clipping is the method of cutting a graphics display to neatly fit a predefined graphics region
or the view port.
2. What are the types of clipping?
• Point clipping
• Line clipping
• Area clipping
• Curve clipping
• Text clipping
3. Define Line clipping?
The concept of line clipping is same as point clipping. In line clipping, we will cut the
portion of line which is outside of window and keep only the portion that is inside the
window.
4. What is the use of clipping?
Clipping in computer graphics is to remove objects, lines or line segments that are
outside the viewing volume.
5. List the different types of text clipping methods available?
• All-or-none string clipping - if all of the string is inside a clip window, keep it otherwise
discards.
• All-or-none character clipping – discard only those characters that are not completely
inside the
window. Any character that either overlaps or is outside a window boundary is clipped.
• Individual characters – if an individual character overlaps a clip window boundary, clip
off the parts of the character that are outside the window.
6. How will you clip a point?
Assuming that the clip window is a rectangle in standard position, we save a point P=(x,y)
for display if the following inequalities are satisfied:
xwmin ≤ x≤ xwmax ywmin ≤ y≤ ywmax
7. Define Exterior clipping
This is just opposite to clipping. This removes the lines coming inside the windows and
displays the remaining. Covering is mainly used to make labels on the complex pictures.
8. What is the need of homogeneous coordinates?
To perform more than one transformation at a time, use homogeneous coordinates or
matrixes. They reduce unwanted calculations intermediate steps saves time and memory and
produce a sequence of transformations.
9. Define text clipping.
Various techniques are used to provide text clipping in a computer graphics. It
depends on the methods used to generate characters and the requirements of a
particular application.
10. Define point clipping
Point clipping tells us whether the given point (X, Y) is within the given window
or not; and decides whether we will use the minimum and maximum coordinates of
the window.
APPLICATIONS
• Drawing and Painting operations: It allows part of a picture to be selected for copying, moving,
erasing or duplicating depending on the application.
• Computer Aided Design (CAD)
• Picture enhancements
• Graphic design
EX.NO: 5 IMPLEMENTATION OF 3D TRANSFORMATIONS
DATE:
AIM:
To implement the following 3D Transformations:
a. Translation b. Rotation c. Scaling
ALGORITHM:
1. Assign the value of edge [20] [3] as a coordinates of cube.
2. Print the menu for choosing 3D Transformation.
3. If user choose translation then get the translation factor (tx, ty, tz) and draw the translated cube.
4. If user choose rotation then print the menu for choosing rotation axis and get the rotation angle.
a) If user choose rotation about X axis then rotate the cube along X axis and draw the rotated
cube.
b) If user choose rotation about Y axis then rotate the cube along Y axis and draw the rotated
cube.
c) If user choose rotation about Z axis then rotate the cube along Z axis and draw the rotated
cube.
5. If user choose scaling then get the scaling factor (sx, sy, sz) and draw the scaled cube.
PROGRAM:
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<math.h>
int gd,gm,i,ch1,ch2;
double x1,x2,y1,y2,x,y,z,theta,temp,temp1;
void draw(double[20][3]);
void main()
{
do
{
double edge[20][3]={ 100,0,0,
100,100,0,
0,100,0,
0,100,100,
0,0,100,
0,0,0,
100,0,0,
100,0,100,
100,100,100,
100,100,100,
100,100,100,
100,100,0,
100,100,100,
100,100,100,
100,100,100,
0,100,100,
0,100,0,
0,0,0,
0,0,100,
100,0,100
};
clrscr();
cout<<"Choose any one 3D Transformation : ";
cout<<"\n\t1. Translation ";
cout<<"\n\t2. Rotation ";
cout<<"\n\t3. Scaling ";
cout<<"\nEnter your choice : \t";
cin>>ch1;
switch(ch1)
{
case 1:
cout<<"\nEnter the translation factors(tx,ty,tz) : \t";
cin>>x>>y>>z;
draw(edge);
for(i=0;i<20;i++)
{
edge[i][0]+=x;
edge[i][1]+=y;
edge[i][2]+=z;
}
draw(edge);
break;
case 2:
cout<<"\n\n\t1.Rotation about X Axis ";
cout<<"\n\t2. Rotation about Y Axis ";
cout<<"\n\t3. Rotation about Z Axis ";
cout<<"\nEnter your choice : \t";
cin>>ch2;
cout<<"\n\nEnter the angle of rotation : \t";
cin>>theta;
theta=(theta*3.14)/180;
switch(ch2)
{
case 1:
draw(edge);
for(i=0;i<20;i++)
{
temp=edge[i][1];
temp1=edge[i][2];
edge[i][1]=temp*cos(theta)-temp1*sin(theta);
edge[i][2]=temp*sin(theta)+temp1*cos(theta);
}
draw(edge);
break;
case 2:
draw(edge);
for(i=0;i<20;i++)
{
temp=edge[i][0];
temp1=edge[i][2];
edge[i][0]=temp*cos(theta)+temp1*sin(theta);
edge[i][2]=-temp*sin(theta)+temp1*cos(theta);
}
draw(edge);
break;
case 3:
draw(edge);
for(i=0;i<20;i++)
{
temp=edge[i][0];
temp1=edge[i][1];
edge[i][0]=temp*cos(theta)-temp1*sin(theta);
edge[i][1]=temp*sin(theta)+temp1*cos(theta);
}
draw(edge);
break;
}
break;
case 3:
cout<<"\n\nEnter the scaling factors (sx,sy,sz) : \t";
cin>>x>>y>>z;
draw(edge);
for(i=0;i<20;i++)
{
edge[i][0]*=x;
edge[i][1]*=y;
edge[i][2]*=z;
}
draw(edge);
break;
}
}while(ch1<4);
}
OUTPUT:
Case 1:
Choose any one 3D Transformation:
1. Translation
2. Rotation
3. Scaling
Enter your choice: 1
Enter the translation factors (tx,ty,tz) : 100 50 150
Case 2 (a):
Choose any one 3D Transformation:
1. Translation
2. Rotation
3. Scaling
1. Translation
2. Rotation
3. Scaling
1. Translation
2. Rotation
3. Scaling
1. Translation
2. Rotation
3. Scaling
5. What is Transformation?
Transformation is the process of introducing changes in the shape size and orientation of
the object using scaling rotation reflection shearing & translation etc.
6. What is translation?
Translation is the process of changing the position of an object in a straight-line path
from one coordinate location to another. Every point (x, y) in the object must undergo a
displacement to (x´,y´). the transformation is:
x´ = x + tx
y´ = y+ty
7. What is rotation?
A 2-D rotation is done by repositioning the coordinates along a circular path, in X = rcos
(q + f) and Y = r sin (q + f).
8. What is scaling?
The scaling transformations changes the shape of an object and can be carried out by
multiplying each vertex (x,y) by scaling factor Sx,Sy where Sx is the scaling factor of x
and Sy is the scaling factor of y.
9. What is shearing?
The shearing transformation actually slants the object along the X direction or
the Y direction as required.ie; this transformation slants the shape of an object
along a required plane.
APPLICATIONS
Video game
Image Processing: Medical field
Entertainment: It mostly used for making videos, motion pictures, cartoon
animation films.
Computer Aided Design (CAD)
EX.NO: 6 3D PROJECTIONS - PARALLEL AND PERSPECTIVE
DATE:
AIM:
To write a program to implement the following 3D projections:
a. Parallel Projection b. Perspective Projection
ALGORITHM:
1. Get the coordinate to draw the cube.
2. Print the menu : 1. Parallel Projection 2. Perspective Projection
3. If user choose Parallel Projection then find the coordinates of following views and draw
them:
a) Side View
b) Front View
c) Top View
4. If user choose Perspective Projection then simply draw the cube using bar3d( )
function.
PROGRAM:
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
int gd,gm,x1,y1,x2,y2,dep,ch;
void main()
{
cout<<"\n Enter the TOP-LEFT and BOTTOM-RIGHT CORNER:";
cin>>x1>>y1>>x2>>y2;
cout<<"\n Enter the depth along z axis:";
cin>>dep;
do
{
cout<<"Choose any one projection:\n\t1.Parallel Projection\n\t2.Perspective
Projection\nEnter your choice:";
cin>>ch;
initgraph(&gd,&gm,"C:\\TC\\BGI");
switch(ch)
{
case 1:
rectangle(x2+100,y1,x2+100+dep,y2);
outtextxy(x2+100,y1-10,"SIDE VIEW");
rectangle(x1,y1,x2,y2);
outtextxy(x1,y1-10,"FRONT VIEW");
rectangle(x1,y1-(y2-y1),x2,x1+dep-(y2-y1));
outtextxy(x1,y1-(y2-y1)-10,"TOP VIEW");
getch();
closegraph();
break;
case 2:
bar3d(x1,y1,x2,y2,dep,1);
getch();
closegraph();
break;
}
}while(ch<3);
}
OUTPUT:
RESULT:
Thus the implementations of 3D projections (Parallel and Perspective) has been created
and the output was verified.
VIVA QUESTIONS
2. Define projections?
The process of displaying 3D into a 2D display unit is known as projection. The projection
transforms 3D objects into a 2D projection plane. The process of converting the description
of objects from world coordinates to viewing coordinates is known as projection
AIM:
To create the 3D Scene in C++.
ALGORITHM:
PROGRAM:
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
intgd,gm;
void main()
{
initgraph(&gd,&gm,"C:\\TC\\BGI");
setbkcolor(BLUE);
setcolor(RED);
rectangle(150,150,250,200);
line(150,200,120,180);
line(120,180,120,130);
setcolor(LIGHTRED);
line(120,130,150,150);
line(150,150,200,100);
line(200,100,250,150);
fillellipse(200,125,10,10);
line(120,130,170,80);
line(170,80,200,100);
setcolor(BROWN);
rectangle(190,170,210,200);
line(190,170,200,180);
line(200,180,200,200);
setcolor(CYAN);
line(190,200,190,270);
line(210,200,210,250);
line(190,270,300,270);
line(210,250,300,250);
setcolor(LIGHTGRAY);
circle(230,260,5);
circle(270,260,5);
setcolor(BROWN);
line(230,259,270,259);
line(230,261,270,261);
line(230,230,230,261);
line(232,230,232,260);
line(227,230,237,230);
line(227,231,237,231);
line(270,260,250,245);
line(271,260,251,245);
line(250,245,230,245);
line(250,246,230,246);
line(248,245,248,240);
line(247,245,247,240);
fillellipse(248,238,3,1);
setcolor(LIGHTGRAY);
line(252,245,265,245);
line(252,246,265,246);
getch();
}
OUTPUT:
RESULT:
Thus the 3D scene has been created and the output was verified.
VIVA QUESTIONS
5. What is texture?
The realism of an image is greatly enhanced by adding surface texture to various
faces of a mesh object. The basic technique begins with some texture function,
texture(s,t) in texture space , which has two parameters s and t.
6. Define rendering.
Rendering is the process of generating an image from a model (or models in what
collectively could be called a scenefile), by means of computer programs. Also, the results of
such a model can be called a rendering.
8. Define shading
Shading is a process used in drawing for depicting levels of darkness on paper by applying
media more densely or with a darker shade for darker areas, and less densely or with a lighter
shade for lighter areas.
APPLICATIONS
• Making 3D movies.
• Special Effects for cinema.
• Computer Arts
• Scientific and Business Visualization
• Video Game.
EX.NO: 8 IMAGE EDITING AND MANIPULATION
DATE:
AIM:
To perform the following operations,
PROCEDURE:
iv. Move that selected portion to another image by using Move Tool.
v. Eliminate the unwanted portion of the image by using History Brush Tool.
i. Draw the images in different scenes to animate. Save all the images in jpg
format.
iv. Drop all the images in center bottom portion of the window.
v. Save the animated file in gif format.
RESULT:
Thus the operations (Basic Operations on image, creating gif animated image, optimize
the image) has been performed and the output was verified.
VIVA QUESTIONS
2. Define frame.
One of the shape photographs that a film or video is made of is known as frame.
5. What is Fractals?
A Fractal is an object whose shape is irregular at all scales.
6. What is graftals?
Graftals are applicable to represent realistic rendering plants and trees. A tree is represented
by a String of symbols 0, 1.
8. What is hue?
The perceived light has a dominant frequency (or dominant wavelength). The dominant
frequency is also called as hue or simply as color.
APPLICATIONS
• Making 3D movies.
• Making animated movies and images.
• Special Effects for cinema: To improve the picture quality
• Education field.
EX.NO: 9 2D ANIMATION
DATE:
AIM:
PROCEDURE:
4. Insert a new frame and change the scene for new frame.
RESULT:
Thus the interactive 2D Animation has been created and the output was verified.
VIVA QUESTIONS
1. Define frame.
One of the shape photographs that a film or video is made of is known as frame.
2. What is tweening?
It is the process, which is applicable to animation objects defined by a sequence of points,
and that change shape from frame to frame.
6. What is graftals?
Graftals are applicable to represent realistic rendering plants and trees. A tree is represented
by a String of symbols 0, 1.
APPLICATIONS