0% found this document useful (0 votes)
16 views79 pages

CG Lab Manual

computer graphics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views79 pages

CG Lab Manual

computer graphics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Practical No.

1
Aim: Write a program to draw line using DDA algorithm.
Theory:
DDA stands for Digital Differential Analyzer. It is an incremental method of scan conversion
of line. In this method calculation is performed at each step but by using results of previous
steps.
Suppose at step i, the pixels is (xi,yi)
The line of equation for step i
yi=mxi+b......................equation 1
Next value will be
yi+1=mxi+1+b.................equation 2
m = ∆y/∆x
yi+1-yi=∆y.......................equation 3
yi+1-xi=∆x......................equation 4
yi+1=yi+∆y
∆y=m∆x
yi+1=yi+m∆x
∆x=∆y/m
xi+1=xi+∆x
xi+1=xi+∆y/m

Case1: When |M|<1 then (assume that x12)


x= x1,y=y1 set ∆x=1
yi+1=y1+m, x=x+1
Until x = x2

Case2: When |M|<1 then (assume that y12)


x= x1,y=y1 set ∆y=1
xi+1= 1/m, y=y+1
Until y → y2

Algorithm:
Step 1: Start the program.
Step 2: Input the line endpoints and store the left endpoint in (x1, y1) and right endpoint in (x2,
y2)
Step 3: Calculate the values of ∆x and ∆y using
∆x = xb – xa, ∆y = yb – ya

1|Page
Step 4: if the values of ∆x > ∆y assign values of steps as ∆x otherwise the values of steps as
∆y
Step 5: Calculate the values of X increment and Y increment and assign
the value x= xa and y = ya.
Step 6: for k=1 to steps do X = X + X increment , Y= Y + Y increment
Putpixel(ceil(x), ceil(y),15).
Step 7: Stop the Program.
Program:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

void main()

int i,steps,x1,x2,y1,y2;

float x,y,xinc,yinc,dx,dy;

char msg[86];

int gdriver = DETECT,gmode,errorcode;

clrscr();

initgraph(&gdriver,&gmode,"f:\\tc");

printf("\n Enter the co ordinates ");

scanf("%d%d%d%d",&x1,&x2,&y1,&y2);

cleardevice();

outtextxy(200,4,"Draw Using DDA");

line(x1,x2,y1,y2);

dx = x2 - x1;

dy = y2 - y1;

if(abs(dx) > abs(dy))

2|Page
steps = abs(dx);

else

steps = abs(dy);

xinc = (float)dx/steps ;

yinc = (float)dy/steps ;

y = y1;

x = x1;

putpixel(ceil(x),ceil(y),20);

for(i = 0;i <= steps ;i++)

x += xinc ;

y += yinc ;

putpixel(x,y,2);

delay(45);

getch();

Output:

3|Page
Conclusion:
Hence, we have successfully wrote a program to draw line using DDA algorithm.
Questions:
Q1. What is a DDA algorithm?
Ans:

Q2. What are the advantages of DDA algorithm?


Ans:

Q3. What is the equation of DDA algorithm?


Ans:

4|Page
Practical No. 2
Aim: To write a C program to draw a line using Bresenhams algorithm.
Theory:

This algorithm is used for scan converting a line. It was developed by Bresenham. It is an
efficient method because it involves only integer addition, subtractions, and multiplication
operations. These operations can be performed very rapidly so lines can be generated quickly.

In this method, next pixel selected is that one who has the least distance from true line.

The method works as follows:

Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our may to the night, one
pixel position at a time in the horizontal direction toward P 2'(x2',y2').

Once a pixel in choose at any step

The next pixel is

1. Either the one to its right (lower-bound for the line)


2. One top its right and up (upper-bound for the line)

The line is best approximated by those pixels that fall the least distance from the path between
P1',P2'.

To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1

5|Page
The actual y coordinates of the line at x = xi+1is
y=mxi+1+b

The distance from S to the actual line in y direction


s = y-yi
The distance from T to the actual line in y direction
t = (yi+1)-y
Now consider the difference between these 2 distance values
s-t
When (s-t) <0 ⟹ s < t
The closest pixel is S
When (s-t) ≥0 ⟹ s < t
The closest pixel is T
This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1

Substituting m by Bresenham's Line Algorithm and introducing decision variable


di=△x (s-t)
di=△x (2 Bresenham's Line Algorithm (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c
Where c= 2△y+△x (2b-1)
We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Since x_(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)

6|Page
Special Cases
If chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1
di+1=di+2△y-2△x
If chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi
di+1=di+2△y
Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]
Since mx1+b-yi=0 and m = Bresenham's Line Algorithm, we have
d1=2△y-△x
Algorithm:
Step 1: Start the program.
Step 2: Input the two endpoints (x1,y1) and (x2,y2).
Step 3: Plot the pixel value (x1,y1) with a specified color.
Step 4: Calculate the value of dx and dy and find the starting value of decision parameter
as dp=2*dy-dx.
Step 5: Calculate the values of s1 and s2 depending on (x1,y1) and (x2,y2) values.
Step 6: If dp<0, the next point to plot is (x,y+s2) and dp=+2*dy.
Step 7: Otherwise the next point to plot is (x+s1,y+s2) and
dp=dp+2*dx-2*dy.
Step 8: Repeat steps 5 and 6 dx times.
Step 9: Stop the program.
Program:
#include<stdio.h>

#include<math.h>
#include<conio.h>

#include<graphics.h>

void main()
{

int x1,x2,y1,y2;

7|Page
int gd=DETECT,gm;

void linebres(int,int,int,int);
printf("Enter the two end points:");

scanf("%d%d%d%d",&x1,&x2,&y1,&y2);

initgraph(&gd,&gm,"");
cleardevice();

linebres(x1,y1,x2,y2);

getch();
line(x1,y1,x2,y2);

getch();

closegraph();
}

void linebres(int x1,int y1,int x2,int y2)

{
int dx=abs(x1-x2),dy=abs(y1-y2);

int p,x,y,i,xend,yend;

if(dx!=0)
{

p=2*dy-dx;

if(x1>x2)
{

x=x2;

y=y2;
xend=x1;

}
else
{

x=x1;

y=y1;

8|Page
xend=x2;

}
putpixel(x,y,2);

for(i=x;i<xend;i++)

{
x+=1;

if(p<0)

p+=2*dy;
else

p+=2*(dy-dx);

}
putpixel(x,y,2);

else
{

p=2*dx-dy;

if(y1>y2)
{

x=x2;

y=y2;
yend=y2;

putpixel(x,y,2);
for(i=y;i<yend;i++)

{
y+=1;
if(p<0)

p+=2*dx;

else

9|Page
{

x+=1;
p+=2*(dx-dy);

putpixel(x,y,2);
}

}
Output:

Conclusion:
Hence, we have successfully wrote a C program to draw a line using Bresenhams algorithm.
Questions:
Q1. What is Bresenhams algorithm?
Ans:

10 | P a g e
Q2. What are the advantages of Bresenhams algorithm?
Ans:

Q3. What is the difference between DDA and Bresenhams algorithm?


Ans:

11 | P a g e
Practical No. 3
Aim: To write a C program to draw a circle using Bresenhams algorithm.
Theory:
Scan-Converting a circle using Bresenham's algorithm works as follows: Points are generated
from 90° to 45°, moves will be made only in the +x & -y directions as shown in fig:

The best approximation of the true circle will be described by those pixels in the raster that
falls the least distance from the true circle. We want to generate the points from

90° to 45°. Assume that the last scan-converted pixel is P1 as shown in fig. Each new point
closest to the true circle can be found by taking either of two actions.

1. Move in the x-direction one unit or


2. Move in the x- direction one unit & move in the negative y-direction one unit.

Let D (Si) is the distance from the origin to the true circle squared minus the distance to point
P3 squared. D (Ti) is the distance from the origin to the true circle squared minus the distance
to point P2 squared. Therefore, the following expressions arise.

D(Si)=(xi-1+1)2+yi-12 -r2
D (Ti)=(xi-1+1)2+(yi-1 -1)2-r2

Since D (Si) will always be +ve & D (Ti) will always be -ve, a decision variable d may be
defined as follows:

12 | P a g e
di=D (Si )+ D (Ti)
Therefore,
di=(xi-1+1)2+ yi-12 -r2+(xi-1+1)2+(yi-1 -1)2-r2
From this equation, we can drive initial values of di as
If it is assumed that the circle is centered at the origin, then at the first step x = 0 & y = r.
Therefore,
di=(0+1)2+r2 -r2+(0+1)2+(r-1)2-r2
=1+1+r2-2r+1-r2
= 3 - 2r
Thereafter, if d_i<0,then only x is incremented.
xi+1=xi+1 di+1=di+ 4xi+6
& if di≥0,then x & y are incremented
xi+1=xi+1 yi+1 =yi+ 1
di+1=di+ 4 (xi-yi)+10
Algorithm:
Step 1: Start the program.
Step 2: Input radius r and the midpoint of the circle (x,y) and obtain the first point on the
circumference for the circle as (0,r).
Step 3: Calculate the initial value of the decision parameter as p=1-r.
Step 4: At each position check the following conditions.
a) If p<0 then x=x+1 and p+=2*x+1
b) Else y=y-1 and p+=2*(x-y)+1.
Step 5: Determine symmetry points at the other seven octants.
Step 6: Move each calculated pixel position (x,y) onto the circular path centered on

13 | P a g e
(xc,yc) and plot the coordinate value as x=x+xc and y=y+yc.
Step 7: Repeat step 3 until x<y.
Step 8: Stop the program.
Program:
#include<stdio.h>
#include<conio.h>

#include<graphics.h>

void main()
{

int gd=DETECT,gm;

int x,y,r;
void cir(int,int,int);

printf("Enter the Mid points and Radious:");

scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"");

cir(x,y,r);

getch();
closegraph();

}
void cir(int x1,int y1,int r)
{

int x=0,y=r,p=1-r;

void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);

while(x<y)

{
x++;

if(p<0)
p+=2*x+1;

14 | P a g e
else

{
y--;

p+=2*(x-y)+1;

}
cliplot(x1,y1,x,y);

}
void cliplot(int xctr,int yctr,int x,int y)

putpixel(xctr +x,yctr +y,1);


putpixel(xctr -x,yctr +y,1);

putpixel(xctr +x,yctr -y,1);

putpixel(xctr -x,yctr -y,1);


putpixel(xctr +y,yctr +x,1);

putpixel(xctr -y,yctr +x,1);

putpixel(xctr +y,yctr -x,1);


putpixel(xctr -y,yctr -x,1);

getch();

}
Output:

15 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to draw a circle using Bresenham’s algorithm.
Questions:
Q1. What are the advantages of Bresenham's Algorithm?
Ans:

Q2. How many points required for circle drawing? Give example.
Ans:

Q3. Which method is used for draw the Circle?


Ans:

16 | P a g e
Practical No. 4
Aim: To write a C program to draw a ellipse using Bresenham’s algorithm.
Theory:
Bresenham’s ellipse algorithm plots(finds) points of an ellipse on the first quadrant by dividing
the quadrant into two regions.
Each point(x, y) is then projected into other three quadrants (-x, y), (x, -y), (-x, -y) i.e. it uses
4-way symmetry.
Function of ellipse:
fellipse(x, y)=ry2x2+rx2y2-rx2ry2
fellipse(x, y)<0 then (x, y) is inside the ellipse.
fellipse(x, y)>0 then (x, y) is outside the ellipse.
fellipse(x, y)=0 then (x, y) is on the ellipse.
Decision parameter:
Initially, we have two decision parameters p1 0 in region 1 and p20 in region 2.
These parameters are defined as : p10 in region 1 is given as : p10=ry2+1/4rx2-rx2ry
Algorithm:
Step 1: Start the program.
Step 2: Input rx , ry and the center of the ellipse (xc,yc )and obtain the first point on the
ellipse centered on the origin as (x0,y0) = (0,ry).
Step 3: Calculate the initial value of the decision parameter in region1 as P10 = ry 2 – rx 2 ry
+ ¼ rx 2 Step 4: At each position k x 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 + 2ry
2 xk+1 + ry 2
Step 5: Otherwise the next point along the ellipse is( xk+1 ,yk-1 ) and p1k+1 = p1k +2ry 2
xk+1 – 2 rx 2 yk+1 + ry 2 with 2ry 2 xk+1 = 2ry 2 xk+2ry 2 , 2rx 2 yk+1 = 2rx 2 yk – 2rx 2
and continue until 2ry 2 x ≥2rx 2 y.
Step 6: Calculate the initial position of the decision parameter in region 2 as P20 = ry2 2 + ry
2 ( y0 – 1 ) 2 - rx 2 ry 2 where (x0 ,y0) is the last position calculated in region 1.
Step 7: 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 – 2rx 2 yk+1
+ ry 2 .
Step 8: Otherwise the next point along the ellipse is ( xk + 1 ,yk -1) and P2k+1 = p2k – 2ry 2
xk+1 – 2rx 2 yk+1 + rx 2 .
Step 9: Using the same incremental values for x and y as in region 1 continue until y=0.
Step 10: For both regions determine symmetry points along the other three quadrants.
Step 11: Move each calculated pixel position (x,y) on to the elliptical path centered on (xc , yc
) and plot the co-ordinates values x = x + xc , y = y + yc .

17 | P a g e
Step 12: Display the output points.
Step 13: Stop the program .
Program:
#include<stdio.h>

#include<conio.h>
#include<math.h>

#include<graphics.h>

main()
{

int gd=DETECT,gm;

int xcenter,ycenter,rx,ry;
int p,x,y,px,py,rx1,ry1,rx2,ry2;

initgraph(&gd,&gm,"c:\\tc\\bgi");

printf("Enter The Radius Value:\n");


scanf("%d%d",&rx,&ry);

printf("Enter The xcenter and ycenter Values:\n");

scanf("%d%d",&xcenter,&ycenter);
ry1=ry*ry;

rx1=rx*rx;

ry2=2*ry1;
rx2=2*rx1;

/* REGION 1 */
x=0;
y=ry;

plotpoints(xcenter,ycenter,x,y);
p=(ry1-rx1*ry+(0.25*rx1));
px=0;

py=rx2*y;
while(px<py)

18 | P a g e
{

x=x+1;
px=px+ry2;

if(p>=0)

y=y-1;
py=py-rx2;

if(p<0)

p=p+ry1+px;
else

p=p+ry1+px-py;

plotpoints(xcenter,ycenter,x,y);
/* REGION 2*/

p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1);

while(y>0)
{

y=y-1;

py=py-rx2;
if(p<=0)

x=x+1;
px=px+ry2;

if(p>0)
p=p+rx1-py;

else
p=p+rx1-py+px;
plotpoints(xcenter,ycenter,x,y);

19 | P a g e
getch();

return(0);
}

int plotpoints(int xcenter,int ycenter,int x,int y)

{
putpixel(xcenter+x,ycenter+y,6);

putpixel(xcenter-x,ycenter+y,6);

putpixel(xcenter+x,ycenter-y,6);
putpixel(xcenter-x,ycenter-y,6);

Output:

Conclusion: Hence, we have successfully wrote a C program to draw a ellipse using


Bresenham’s algorithm.

20 | P a g e
Questions:
Q1. What is use of initgraph() method?
Ans:

Q2. How many points required for Ellipse drawing? Give example.
Ans:

Q3. Which method is used for draw the Ellipse?


Ans:

21 | P a g e
Practical No. 5
Aim: To write a C program to draw the various attributes of line, circle and ellipse.
Theory:
In a C program, first step is to initialize the graphics drivers on the computer. This is done using
the initgraph() method provided in graphics.h library. The graphics.h header file provides
access to a simple graphics library that makes it possible to draw lines, rectangles, ovals, arcs,
polygons, images, and strings on a graphical window.
void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);
It initializes the graphics system by loading the passed graphics driver then changing the system
into graphics mode. It also resets or initializes all graphics settings like color, palette, current
position etc, to their default values. Below is the description of input parameters of initgraph
function.
graphicsDriver: It is a pointer to an integer specifying the graphics driver to be used. It tells the
compiler that what graphics driver to use or to automatically detect the drive. In all our
programs we will use DETECT macro of graphics.h library that instruct compiler for auto
detection of graphics driver.
graphicsMode: It is a pointer to an integer that specifies the graphics mode to be used. If
*gdriver is set to DETECT, then initgraph sets *gmode to the highest resolution available for
the detected driver.
driverDirectoryPath: It specifies the directory path where graphics driver files (BGI files) are
located. If directory path is not provided, then it will search for driver files in current worki ng
directory directory. In all our sample graphics programs, you have to change path of BGI
directory accordingly where you Turbo C++ compiler is installed.
Algorithm:
Step 1: Start the program.

Step 2: Initialize the variables.


Step 3: Call the initgraph() function

Step 4: Set color for the output primitives.

Step 5: Using Outtextxy() display the chosen particular primitives.


Step 6: Include the various attributes of line, circle and ellipse.

Step 7: close the graph and run the program.

Step 8: stop the program


Program:
#include<stdio.h>

#include<conio.h>

22 | P a g e
#include<graphics.h>

#include<string.h>

void main()

char ch='y';

int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;

initgraph(&gd,&gm,"");

while(ch=='y')

cleardevice();

setbkcolor(9);

outtextxy(100,130,"Choose From The Following ");

outtextxy(100,150,"1. Line");

outtextxy(100,170,"2.Circle");

outtextxy(100,190,"3.Box");

outtextxy(100,210,"4.Arc");

outtextxy(100,230,"5.Ellipse");

outtextxy(100,250,"6.Rectangle");

outtextxy(100,270,"7.Exit");

ch=getch();

cleardevice();

switch(ch)

case '1':

line(100,200,300,400);

break;

case '2':

circle(200,200,100);

23 | P a g e
break;

case '3':

setfillstyle(5,4);

bar(100,300,200,100);

break;

case '4':

setfillstyle(5,4);

arc(200,200,100,300,100);

break;

case '5':

setfillstyle(5,4);

fillellipse(100,100,50,100);

break;

case '6':

settextstyle(DEFAULT_FONT,0,2);

outtextxy(120,140,"AMS COLLEGE ");

line(100,100,100,300);

line(300,300,100,300);

line(100,100,300,100);

line(300,100,300,300);

break;

case '7':

closegraph();

return;

ch='y';

getch();

24 | P a g e
}

Output:

25 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to draw the various attributes of line, circle
and ellipse.
Questions:
Q1. What is Output Primitive?
Ans:

Q2. What is attribute parameter?


Ans:

Q3. What are the basic line attributes?


Ans:

26 | P a g e
Practical No. 6
Aim: To write a C program to perform 2D transformations such as translation, rotation.

Theory:
Translation:
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (t x, ty) to the original coordinate X,Y to get the new
coordinate X′,Y′.

From the above figure, you can write that −


X’ = X + tx
Y’ = Y + ty
The pair (t x, ty) is called the translation vector or shift vector. The above equations can also be
represented using the column vectors.
P=[X]/[Y]
p' = [X′]/[Y′]
T = [tx]/[ty]
We can write it as −
P’ = P + T

Rotation:

In rotation, we rotate the object at particular angle θ thetatheta from its origin. From the
following figure, we can see that the point P X,Y is located at angle φ from the horizontal X
coordinate with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will
get a new point P’ X′,Y′.

27 | P a g e
Using standard trigonometric the original coordinate of point P X,Y can be represented as −
X=rcosϕ......(1)
Y=rsinϕ......(2)
Same way we can represent the point P’ X′,Y′ as −
x′=rcos(ϕ+θ)=rcosϕcosθ−rsinϕsinθ.......(3)
y′=rsin(ϕ+θ)=rcosϕsinθ+rsinϕcosθ.......(4)
Substituting equation 1 & 2 in 3 & 4 respectively, we will get
x′=xcosθ−ysinθ
y′=xsinθ+ycosθ
Representing the above equation in matrix form,

P’ = P . R
Where R is the rotation matrix

The rotation angle can be positive and negative.


For positive rotation angle, we can use the above rotation matrix. However, for negative angle
rotation, the matrix will change as shown below −

Algorithm:

Step 1: Start the program.

Step 2: Input the object coordinates

Step 3: For Translation

a) Enter the translation factors tx and ty.

b) Move the original coordinate position (x,y) to a new

position (x1,y1).ie. x=x+x1, y=y+y1.

c) Display the object after translation

Step 4: For Rotation

a) Enter the radian for rotation angle θ.

28 | P a g e
b) Rotate a point at position (x,y) through an angle θ about the

origin x1=xcos θ - ysin θ , y1=ycos θ + xsin θ.

c) Display the object after rotation

Step 5: Stop the Program.

Program:
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h>

#include<stdlib.h>

void menu();

void input();

void output();

void translation();

void rotation();

int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;

void menu()

printf("menu\n");

printf("1.Translation\n");

printf("2.rotation\n");

printf("3.exit\n");

printf("enter the choice:");

scanf("%d",&option);

switch(option)

case 1:

29 | P a g e
input();

translation();

break;

case 2:

input();

rotation();

break;

case 6:

exit(0);

break;

void input()

printf("enter the number of vertices:" );

scanf("%d",&n);

for(i=0;i<n;i++)

printf("enter the coordinates:");

scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);

void output()

cleardevice();

for(i=0;i<n;i++)

line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);

30 | P a g e
}

void translation()

output();

printf("enter the tranformation vertex tx,ty:\n");

scanf("%d%d",&tx,&ty);

for(i=0;i<=n;i++)

a[i][0]=a[i][0]+tx;

a[i][1]=a[i][1]+ty;

output();

delay(10);

menu();

void rotation()

output();

printf("enter the rotating angle:");

scanf("%d",&y);

printf("enter the pivot point:");

scanf("%d%d",&fx,&fy);

k=(y*3.14)/180;

for(i=0;i<=n;i++)

a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);

a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);

31 | P a g e
}

output();

delay(10);

menu();

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

menu();

getch();

Output:

32 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to perform 2D transformations such as
translation, rotation.

Questions:
Q1. What is 2D Transformation?
Ans:

Q2. What is a Translation?


Ans:

Q3. What is Rotation?


Ans:

33 | P a g e
Practical No. 7
Aim: To write a C program to perform 2D transformations such as scaling, reflection and
shearing.

Theory:
Scaling:
To change the size of an object, scaling transformation is used. In the scaling process, you
either expand or compress the dimensions of the object. Scaling can be achieved by multi plying
the original coordinates of the object with the scaling factor to get the desired result.
Let us assume that the original coordinates are X,Y the scaling factors are (SX, SY), and the
produced coordinates are X′,Y′. This can be mathematically represented as shown below −
X' = X . SX and Y' = Y . SY
The scaling factor SX, SY scales the object in X and Y direction respectively. The above
equations can also be represented in matrix form as below –

P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.

If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object.
If we provide values greater than 1, then we can increase the size of the object.

Reflection:

Reflection is the mirror image of original object. In other words, we can say that it is a rotation
operation with 180°. In reflection transformation, the size of the object does not change.
The following figures show reflections with respect to X and Y axes, and about the origin
respectively.

34 | P a g e
Shearing:

A transformation that slants the shape of an object is called the shear transformation. There are
two shear transformations X-Shear and Y-Shear. One shifts X coordinates values and other
shifts Y coordinate values. However, in both the cases only one coordinate changes its
coordinates and other preserves its values. Shearing is also termed as Skewing.
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes
the vertical lines to tilt right or left as shown in below figure.

The transformation matrix for X-Shear can be represented as −

35 | P a g e
Y' = Y + Shy . X
X’ = X
Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates which causes the
horizontal lines to transform into lines which slopes up or down as shown in the following
figure.

The Y-Shear can be represented in matrix from as −

X’ = X + Shx . Y
Y’ = Y
Algorithm:
Step 1: Start the program.

Step 2: Input the object coordinates

Step 3: For Scaling

a) Input the scaled factors sx and sy.

b) The transformed coordinates (x1,y1) , x1=x.sx and y1=y.sy.

c) Display the object after scaling

Step 4: For Reflection

Reflection can be performed about x axis and y axis.

36 | P a g e
a) Reflection about x axis: The transformed coordinates are x1=a and

y1=-y.

b) Reflection about y axis: The transformed coordinates are x1=x and

y1=y.

c) Display the object after reflection

Step 5: For Shearing

a) Input the shearing factors shx and shy.

b) Shearing related to x axis: Transform coordinates x1=x+shx*y

and y1=y.

c) Shearing related to y axis: Transform coordinates

x1=x and y1=y+shy*x.

d) Input the xref and yref values.

e) X axis shear related to the reference line y-yref is

x1=x+shx(y-yref) and y1=y.

f) Y axis shear related to the reference line x=xref is

x1=x

g) Display the object after shearing

Step 6: Stop the Program.

Program:
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h>

#include<stdlib.h>

void menu();

void input();

void output();

37 | P a g e
void scaling();

void shearing();

void reflection();

int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;

float sx,sy;

void menu()

printf("menu\n");

printf("1.scaling\n");

printf("2.shearing\n");

printf("3.reflection\n");

printf("4.exit\n");

printf("enter the choice:");

scanf("%d",&option);

switch(option)

case 1:

input();

scaling();

break;

case 2 :

input();

shearing();

break;

case 3:

input();

reflection();

break;

38 | P a g e
case 4:

exit(0);

break;

void input()

printf("enter the number of vertices:" );

scanf("%d",&n);

for(i=0;i<n;i++)

printf("enter the coordinates:");

scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);

void output()

cleardevice();

for(i=0;i<n;i++)

line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);

void scaling()

output();

printf("enter the scaling factor\n");

scanf("%f%f",&sx,&sy);

39 | P a g e
printf("enter the fixed point:");

scanf("%d%d",&fx,&fy);

for(i=0;i<=n;i++)

a[i][0]=a[i][0]*sx+fy*(1-sx);

a[i][1]=a[i][1]*sy+fy*(1-sy);

output();

delay(10);

menu();

void shearing()

output();

printf("enter the shear value:");

scanf("%d",&sh);

printf("enter the fixed point:");

scanf("%d%d",&fx,&fy);

printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:");

scanf("%d",&axis);

for(i=0;i<=n;i++)

if(axis==1)

a[i][0]=a[i][0]+sh*(a[i][1]-fy);

else

40 | P a g e
a[i][1]=a[i][1]+sh*(a[i][0]-fx);

output();

delay(10);

menu();

void reflection()

output();

for(i=0;i<=n;i++)

temp=a[i][0];

a[i][0]=a[i][1];

a[i][1]=temp;

output();

delay(10);

menu();

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

menu();

getch();

41 | P a g e
Output:

42 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to perform 2D transformations such as scaling,
reflection and shearing.

Questions:
Q1. Define Scaling.
Ans:

Q2. Define Reflection.


Ans:

Q3. Define Shear.


Ans:

43 | P a g e
Practical No. 8
Aim: To write a C++ program to perform composite 2D transformations such as translation,
Rotation.

Theory:
Composite Transformation:
As the name suggests itself Composition, here we combine two or more transformations into
one single transformation that is equivalent to the transformations that are performed one after
one over a 2-D object.
Algorithm:
Step 1: Start the program.

Step 2: Input the object coordinates.

Step 3: Translation

a) Enter the translation factors tx and ty.

b) Move the original coordinate position (x,y) to a new position (x1,y1).ie. x=x+x1, y=y+y1.

Step 4: Rotation

a) Enter the radian for rotation angle θ.

b) Rotate a point at position (x,y) through an angle θ about the origin x1=xcos θ - ysinθ ,
y1=ycosθ + xsinθ.

Step 5: Stop the Program.

Program:
#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

#include<stdlib.h>

void main()

int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,

sx,sy,shx,shy,xref,yref;

char d;

44 | P a g e
gd=DETECT;

initgraph(&gd,&gm,"");

cout<<"enter the no of points";

cin>>n;

for(i=0;i<n;i++)

cout<<"enter the coordinates"<<i+1;

cin>>xa[i]>>ya[i];

do

cout<<"menu";

cout<<"\n1.translation\n2.rotation\n3.exit";

cin>>op;

switch(op)

case 1:

cout<<"enter the translation vector";

cin>>tx>>ty;

for(i=0;i<n;i++)

xa1[i]=xa[i]+tx;

ya1[i]=ya[i]+ty;

cleardevice();

cout<<"before translation";

for(i=0;i<n;i++)

45 | P a g e
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);

cout<<"after translation";

for(i=0;i<n;i++)

line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);

getch();

cleardevice();

break;

case 2:

cout<<"enter the rotation angle";

cin>>theta;

theta=(theta*3.14)/180;

cout<<"enter the reference points";

cin>>xf>>yf;

for(i=0;i<n;i++)

xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);

ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);

cleardevice();

cout<<"before rotation";

for(i=0;i<n;i++)

line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);

cout<<"after rotation";

46 | P a g e
for(i=0;i<n;i++)

line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);

getch();

cleardevice();

break;

case 3:

exit(0);

break;

}while(op!=3);

Output:

47 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to perform composite 2D transformations
such as translation, Rotation.

Questions:
Q1. What is Composite 2D Transformation?
Ans:

48 | P a g e
Q2. What is viewing transformation?
Ans:

Q3. Define Homogeneous Transformation.


Ans:

49 | P a g e
Practical No. 9
Aim: Write a C++ program to perform composite 2D transformations such as scaling,
rotation and reflection.
Theory:
Composite Transformation:
As the name suggests itself Composition, here we combine two or more transformations into
one single transformation that is equivalent to the transformations that are performed one after
one over a 2-D object.
Algorithm:
Step 1: Start the program.
Step 2: Input the object coordinates.
Step 3: Scaling
a) Input the scaled factors sx and sy.
b) The transformed coordinates (x1,y1) , x1=x.sx and y1=y.sy.
Step 4: Reflection
Reflection can be performed about x axis and y axis.
a) Reflection about x axis : The transformed coordinates are x1=a and y1=-y.
b) Reflection about y axis : The transformed coordinates are x1=x and y1=y.
Step 5: Shearing
a) Input the shearing factors shx and shy.
b) Shearing related to x axis : Transform coordinates x1=x+shx*y and y1=y.
c) Shearing related to y axis : Transform coordinates x1=x and y1=y+shy*x.
d) Input the xref and yref values.
e) X axis shear related to the reference line y-yref is x1=x+shx(y-yref) and y1=y.
f) Y axis shear related to the reference line x=xref is x1=x and y1=y+shy(x-xref)
Step 6: Finally display the transformed object after the successive transformations.
Step 7: Stop the Program
Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>

50 | P a g e
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,
sx,sy,shx,shy,xref,yref;
char d;
gd=DETECT;
initgraph(&gd,&gm,””);
cout<<“enter the no of points”;
cin>>n;
for(i=0;i<n;i++)
{
cout<<“enter the coordinates”<<i+1;
cin>>xa[i]>>ya[i];
}

do
{
cout<<“menu”;
cout<<“\n1.scaling\n2.shearing\n3.reflection\n4.exit”;
cin>>op;
switch(op)
{
case 1:
cout<<“enter the scaling factor”;
cin>>sx>>sy;
cout<<“enter the reference point”;
cin>>rx>>ry;
for(i=0;i<n;i++)

51 | P a g e
{
xa1[i]=xa[i]*sx+rx*(1-sx);
ya1[i]=ya[i]*sy+ry*(1-sy);
}
cleardevice();
cout<<“before scaling”;
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<“after scaling”;
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 2:
cout<<“enter the shear value”;
cin>>shx>>shy;
cout<<“enter the reference point”;
cin>>xref>>yref;
cout<<“enter the shear direction x or y”;
cin>>d;
if(d==’x’)
{
for(i=0;i<n;i++)
{
xa1[i]=xa[i]+shx*(ya[i]-yref);

52 | P a g e
ya1[i]=ya[i];
}
}
cleardevice();
cout<<“before shearing”;
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<“after shearing”;
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 3:
cout<<“before reflection”;
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<“after reflection”;
for(i=0;i<n;i++)
{
line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);
}
getch();
cleardevice();

53 | P a g e
break;
case 4:
exit(0);
break;
}
}while(op!=4);
}
Output:

54 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to perform composite 2D transformations
such as scaling, rotation and reflection.

55 | P a g e
Questions:
Q1. Define Clipping.
Ans:

Q2. What is frame buffer?


Ans:

Q3. Write down applications of Transformation.


Ans:

56 | P a g e
Practical No. 10
Aim: To write a C program to clip a line using Cohen-Sutherland clipping algorithm.

Theory:
In the algorithm, first of all, it is detected whether line lies inside the screen or it is outside
the screen. All lines come under any one of the following categories:

1. Visible
2. Not Visible
3. Clipping Case

1. Visible: If a line lies within the window, i.e., both endpoints of the line lies within the
window. A line is visible and will be displayed as it is.

2. Not Visible: If a line lies outside the window it will be invisible and rejected. Such lines will
not display. If any one of the following inequalities is satisfied, then the line is considered
invisible. Let A (x1,y2) and B (x2,y2) are endpoints of line.

xmin,xmax are coordinates of the window.

ymin,ymax are also coordinates of the window.

x1>xmax
x2>xmax
y1>ymax
y2>ymax
x1<xmin
x2<xmin
y1<ymin
y2<ymin

3. Clipping Case: If the line is neither visible case nor invisible case. It is considered to be
clipped case. First of all, the category of a line is found based on nine regions given below. All
nine regions are assigned codes. Each code is of 4 bits. If both endpoints of the line have end
bits zero, then the line is considered to be visible.

The center area is having the code, 0000, i.e., region 5 is considered a rectangle window.

57 | P a g e
Following figure show lines of various types

Line AB is the visible case


Line OP is an invisible case
Line PQ is an invisible line
Line IJ are clipping candidates
Line MN are clipping candidate
Line CD are clipping candidate

Algorithm:
Step 1: Start the program.

Step 2: Enter the line end points and the window coordinates.

Step 3: Every line end point is assigned a code that identified the location of the point

relative to the boundaries of the clipping rectangle.

Step 4: Check whether the line lies inside the window then it is entirely drawn.

Step 5: Check whether the line lies outside the window then it is entirely clipped.

Step 6: Otherwise check whether the line intersects the window:

a) Calculate differences between end points and clip boundaries.

b) Determine the intersection point and how much of the line is to be discarded.

Step 7: Display the Output.

Step 8: Stop the program

Program:
#include<stdio.h>

#include<math.h>

#include<graphics.h>

#include<conio.h>

float cxl, cxr,cyt,cyb;

58 | P a g e
code(float,float);

void clip(float,float,float,float);

void rect(float,float,float,float);

void main()

float x1,y1,x2,y2;

int g=0,d;

initgraph(&g,&d,"c:\\tc\\bin");

settextstyle(1,0,1);

outtextxy(40,15,"BEFORE CLIPPING");

printf("\n\n\n please enter the left,bottom,right,top,of clip window");

scanf("%f%f%f%f",&cxl,&cyt,&cxr,&cyt);

rect(cxl,cyb,cxr,cyt);

getch();

printf("\n please enter the line(x1,y1,x2,y2):");

scanf("%f%f%f%f",&x1,&y1,&x2,&y2);

line(x1,y1,x2,y2);

getch();

cleardevice();

settextstyle(1,0,1);

outtextxy(40,15,"after clipping");

clip(x1,y1,x2,y2);

getch();

closegraph();

void clip(float x1,float y1,float x2,float y2)

int c,c1,c2;

59 | P a g e
float x,y;

c1=code(x1,y1);

c2=code(x2,y2);

getch();

while((c1!=0)||(c2!=0))

if((c1&c2)!=0)

goto out;

c=c1;

if(c==0)

c=c2;

if((c&1)==1)

y=y1+(y2-y1)*(cxl-x1)/(x2-x1);

x=cxl;

else

if((c&2)==2)

y=y1+(y2-y1)*(cxl-x1)/(x2-x1);

x=cxr;

else

if((c&8)==8)

x=x1+(x2-x1)*(cyb-y1)/(y2-y1);

y=cyb;

60 | P a g e
else

if((c&4)==4)

x=x1+(x2-x1)*(cyt-y1)/(y2-y1);

y=cyt;

if(c==c1)

x1=x;

y1=y;

c1=code(x,y);

else

x2=x;

y2=y;

c2=code(x,y);

out : rect(cxl,cyb,cxr,cyt);

line(x1,y1,x2,y2);

code(float x,float y)

int c=0;

if(x<cxl)

c=1;

else

61 | P a g e
if(x>cxr)

c=2;

if(y<cyb)

c=c|8;

else

if(y>cyt)

c=c|4;

return c;

void rect(float xl,float yb,float xr,float yt)

line(xl,yb,xr,yb);

line(xr,yb,xr,yt);

line(xr,yt,xl,yt);

line(xl,yt,xl,yb);

Output:

62 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to clip a line using Cohen-Sutherland
clipping algorithm.

Questions:
Q1. What is 3D Transformation?
Ans:

Q2. What is difference between 2D & 3D?


Ans:

63 | P a g e
Practical No. 11
Aim: To write a C program to perform 3D transformations such as translation, rotation.

Theory:

Translation:

It is the movement of an object from one position to another position. Translation is done using
translation vectors. There are three vectors in 3D instead of two. These vectors are in x, y, and
z directions. Translation in the x-direction is represented using Tx. The translation is y-direction
is represented using Ty. The translation in the z- direction is represented using Tz.

If P is a point having co-ordinates in three directions (x, y, z) is translated, then after translation
its coordinates will be (x1 y1 z1) after translation. Tx Ty Tz are translation vectors in x, y, and z
directions respectively.

x1=x+Tx
y1=y+Ty
z1=z+ Tz

Three-dimensional transformations are performed by transforming each vertex of the object. If


an object has five corners, then the translation will be accomplished by translating all five
points to new locations. Following figure 1 shows the translation of point figure 2 shows t he
translation of the cube.

Matrix for translation

Rotation:

It is moving of an object about an angle. Movement can be anticlockwise or clockwise. 3D


rotation is complex as compared to the 2D rotation. For 2D we describe the angle of rotation,

64 | P a g e
but for a 3D angle of rotation and axis of rotation are required. The axis can be either x or y or
z.

Following figures shows rotation about x, y, z- axis

Following figure show rotation of the object about the Y axis

Rotation about Arbitrary Axis

When the object is rotated about an axis that is not parallel to any one of co-ordinate axis, i.e.,
x, y, z. Then additional transformations are required. First of all, alignment is needed, and then
the object is being back to the original position. Following steps are required

65 | P a g e
1. Translate the object to the origin
2. Rotate object so that axis of object coincide with any of coordinate axis.
3. Perform rotation about co-ordinate axis with whom coinciding is done.
4. Apply inverse rotation to bring rotation back to the original position.

Matrix for representing three-dimensional rotations about the Z axis

Matrix for representing three-dimensional rotations about the X axis

Matrix for representing three-dimensional rotations about the Y axis

Following figure show the original position of object and position of object after
rotation about the x-axis

66 | P a g e
5. Apply inverse translation to bring rotation axis to the original position.

For such transformations, composite transformations are required. All the above steps
are applied on points P' and P".Each step is explained using a separate figure.

Step1: Initial position of P' and P"is shown

Step2: Translate object P' to origin

Step3: Rotate P" to z axis so that it aligns along the z-axis

67 | P a g e
Step4: Rotate about around z- axis

Step5: Rotate axis to the original position

Step6: Translate axis to the original position.

Algorithm:
Step 1: Start the program.

Step 2: Display the cube.

Step 3: input the translation vectortx,ty,tz.

Step 4: using the function line, display the object before and after translation.

Step 5: input the scaling factor and reference point.

Step 6: using the function line, display the object before and after scaling.

68 | P a g e
Step 7: input the rotation angle.

Step 8: using the function line,display the object before and after rotation.

Step 9: Stop the Program.

Program:
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

int maxx,maxy,midx,midy;

void axis()

getch();

cleardevice();

line(midx,0,midx,maxy);

line(0,midy,maxx,midy);

void main()

int gd,gm,x,y,z,o,x1,x2,y1,y2;

detectgraph(&gd,&gm);

initgraph(&gd,&gm," ");

setfillstyle(0,getmaxcolor());

maxx=getmaxx();

maxy=getmaxy();

midx=maxx/2;

midy=maxy/2;

axis();

bar3d(midx+50,midy-100,midx+60,midy-90,5,1);

69 | P a g e
printf("Enter Translation Factor");

scanf("%d%d%d",&x,&y,&z);

axis();

printf("after translation");

bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1);

axis();

bar3d(midx+50,midy+100,midx+60,midy-90,5,1);

printf("Enter Rotating Angle");

scanf("%d",&o);

x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);

y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);

x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);

y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);

axis();

printf("After Rotation about Z Axis");

bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);

axis();

printf("After Rotation about X Axis");

bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);

axis();

printf("After Rotation about Y Axis");

bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);

getch();

closegraph();

Output:

70 | P a g e
Conclusion:
Hence, we have successfully wrote a C program to perform 3D transformations such as
translation, rotation.

71 | P a g e
Questions:
Q1. What are the different types of Rotation in 3D?
Ans:

Q2. Define translation in 3D.


Ans:

72 | P a g e
Practical No. 12
Aim: To write a C program to perform 3D transformation such as scaling, reflection and
shearing.
Theory:

Scaling

Scaling is used to change the size of an object. The size can be increased or decreased. The
scaling three factors are required S x Sy and Sz.

Sx=Scaling factor in x- direction


Sy=Scaling factor in y-direction
Sz=Scaling factor in z-direction

Matrix for Scaling

Scaling of the object relative to a fixed point

Following are steps performed when scaling of objects with fixed point (a, b, c). It can be
represented as below:

• Translate fixed point to the origin


• Scale the object relative to the origin
• Translate object back to its original position.
• In figure (a) point (a, b, c) is shown, and object whose scaling is to done also shown in
steps in fig (b), fig (c) and fig (d).

73 | P a g e
1.

Reflection

It is also called a mirror image of an object. For this reflection axis and reflection of plane is
selected. Three-dimensional reflections are similar to two dimensions. Reflection is 180° about

74 | P a g e
the given axis. For reflection, plane is selected (xy,xz or yz). Following matrices show
reflection respect to all these three planes.

Reflection relative to XY plane

Reflection relative to YZ plane

Reflection relative to ZX plane

75 | P a g e
Shearing

It is change in the shape of the object. It is also called as deformation. Change can be in the x
-direction or y -direction or both directions in case of 2D. If shear occurs in both directions, the
object will be distorted. But in 3D shear can occur in three directions.

Matrx for shear

76 | P a g e
Algorithm:
Step 1: Start the program.

Step 2: Display the cube.

Step 3: input the translation vectortx,ty,tz.

Step 4: using the function line, display the object before and after translation.

Step 5: input the scaling factor and reference point.

Step 6: using the function line, display the object before and after scaling.

Step 7: input the rotation angle.

Step 8: using the function line,display the object before and after rotation.

Step 9: Stop the Program.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,” “);
setfillstyle(0,getmaxcolor());
maxx=getmaxx();

77 | P a g e
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf(“Enter Scaling Factor”);
scanf(“%d%d%d”,&x,&y,&z);
axis();
printf(“After Scaling”);
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
getch();
closegraph();
Output:

Conclusion:
Hence, we have successfully wrote a C program to perform 3D transformation such as
scaling, reflection and shearing.

78 | P a g e
Questions:
Q1. Write matrix representation for Scaling in 3D.
Ans:

Q2. Define Reflection in 3D


Ans:

79 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy