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

Computer Graphics Lab

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

DELHI TECHNOLOGICAL UNIVERSITY

(formerly Delhi College of Engineering)

Computer Graphics (CO 313) LAB


Session: 2019-20 – Odd Semester

Submitted to: Submitted by:


Ms. Pratima Shivangi Meena
Assistant Professor 2K17/CO/328
Table of Contents

S.No Name of the Program Date Signature & Remarks


Date
EXPERIMENT-1

AIM: Write a program to implement DDA line algorithm.

THEORY: A digital differential analyzer (DDA) is hardware or software used for interpolation
of variables over an interval between start and end point. DDAs are used for rasterization of lines,
triangles and polygons. They can be extended to nonlinear functions, such as perspective correct
texture mapping, quadratic curves, and traversing voxels. In its simplest implementation for linear
cases such as lines, the DDA algorithm interpolates values in interval by computing for each xi
the equations xi = xi−1 + 1, yi = yi−1 + m, where m is the slope of the line. This slope can be
expressed in DDA as follows:

A linear DDA starts by calculating the smaller of dy or dx for a unit increment of the other. A line is then sampled
at unit intervals in one coordinate and corresponding integer values nearest the line path are determined for the
other coordinate.
Considering a line with positive slope, if the slope is less than or equal to 1, we sample at unit x intervals (dx=1)
and compute successive y values as

Subscript k takes integer values starting from 0, for the 1st point and increases by 1 until the endpoint is reached.
y value is rounded off to the nearest integer to correspond to a screen pixel.
For lines with slope greater than 1, we reverse the role of x and y i.e. we sample at dy=1 and calculate consecutive
x values as

Similar calculations are carried out to determine pixel positions along a line with negative slope. Thus, if the
absolute value of the slope is less than 1, we set dx=1 x start < x end i.e. the starting extreme point is at the left.

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

∆y=yi+1-yi .......................equation 3

∆x=xi+1-xi ......................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

Case 1: When |m|<1 then

x= x1, y=y1, set ∆x=1

yi+1=yi +m , xi+1=xi+1

Until x = x2

Case2: When |m|>1 then

x= x1, y=y1, set ∆y=1

xi+1=xi+1/m, yi+1=yi+1

Until y = y2
PROGRAM:

#include<stdio.h>

#include<graphics.h>

void DDA(int X0, int Y0, int X1, int Y1){

int steps, dx , dy, i;

float Xinc,Yinc,X,Y;

dy = Y1 - Y0 , dx = X1 - X0 ;

steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

Xinc = dx / (float) steps ;

Yinc = dy / (float) steps;

X = X0 , Y = Y0; //Initialising values

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

putpixel (X,Y,12);

X += Xinc;

Y += Yinc;

int main()

int gd = DETECT, gm,X0,Y0,X1,Y1;

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

printf("ENTER LEFT POINT CORDINATES(X,Y): ");

scanf("%d,%d",&X0,&Y0);
printf("ENTER RIGHT POINT CORDINATES(X,Y): ");

scanf("%d,%d",&X1,&Y1);

DDA(X0, Y0, X1, Y1);

getch();

closegraph();

return 0;

OUTPUT:

LEARNING:

· The DDA algorithm is a faster method for calculating pixel positions than the direct use of
equation y = mx + b.

· It eliminates the multiplication in equation by making use of raster characteristics, so that


appropriate increments are applied in the x or y direction to step to pixel positions along the line
path.

· The accumulation of round off error in successive additions of the floating-point increment,
however, can cause the calculated pixel positions to drift away from the true line path for long line
segments.

· The rounding operations and floating-point arithmetic in procedure line DDA are still time
consuming.
EXPERIMENT-2

AIM: Write a program to implement Bresenham’s line generating algorithm


THEORY:

Bresenham’s line Algorithm is use to draw line same as DDA. It has following advantages over
DDA:

1. No use of round off function

2. Only work with integer, no dealing with flouting point number

3. Simple addition and subtraction are used

In this algorithm we choose pixel which nearer to the line and for that we define decision parameter
(Pk) associated with each pixel that tells where we should set next pixel.

Algorithm:

1. Take two input points A(x1,y1) and B(x2,y2)


2. compute ▲x=x2 - x1 & ▲y=y2 –y1
3. if ▲y<▲x than
• Calculate P0=2▲y-▲x and xk+1=xk+1
• if Pk<0 than yk+1=yk and Pk+1=Pk+2▲y
• else yk+1=yk+1 and Pk+1=Pk+2▲y-2▲x
4. else if ▲y<▲x than
• Calculate P0=2▲x-▲y and yk+1=yk+1
• if Pk<0 than xk+1=xk and Pk+1=Pk+2▲x
• else xk+1=xk+1 and Pk+1=Pk+2▲x-2▲y
5. else
• xk+1=xk+1 and yk+1=yk+1

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void Bresanham(int x1,int x2,int y1,int y2){
int x=x1,y=y1;
int dx=x2-x1,dy=y2-y1;
if(dx==0){
while(y!=y2){
y++;
putpixel(x,y,WHITE);
}
return;
}
float m=(float)dy/dx;

int p;
if(m<1){
p=2*dy-dx;
while(x!=x2){
x++;
int change=0;
if(p>=1){
y++;
change=1;
}
putpixel(x,y,WHITE);
p=p+2*dy-2*dx*change;
}
}
else if(m>1){
p=2*dx-dy;
while(y!=y2){
y++;
int change=0;
if(p>=1){
x++;
change=1;
}
putpixel(x,y,WHITE);
p=p+2*dx-2*dy*change;
}
}
else{
while(x!=x2){
x++;
y++;
putpixel(x,y,WHITE);
}
}

}
int main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Bresanham(x1,x2,y1,y2);
getch();
return 0;
}

OUTPUT:

LEARNING:
In this experiment, I learnt and successfully implemented Bresenham’s line algorithm.
EXPERIMENT-3
AIM: Write a program to implement Midpoint Circle algorithm.
THEORY:

As the name suggest, Mid-point circle algorithm is use for drawing circle. In this algorithm we
divide circle in eight partition. We just draw on partition for it and other partition can be drawn by
taking reflection.

To draw the circle, we choose mid-point of two possible pixel. If the point lie inside the circle than
outside pixel will be taken otherwise inside pixel will be taken.

Algorithm

1. Take input point A(a,b) as a center and radius r.

2. take (x0,y0)=(0,r)

3. compute P0=1-r and xk+1=xk+1

4. if Pk<0

· yk+1=yk and Pk+1=Pk+(2*xk+1+1)

5. else

· yk+1=yk+1 and Pk+1=Pk+(2*xk+1+1)-(2*yk+1);

Note :- On time of showing pixel on screen add a in x coordinate and b in y coordinate.


PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

void midPoint(int a,int b,int r,int t1,int t2,int t3){

int p=1-r;

int x=0,y=r;

int c1=x,c2=y;

if(t3==1){

int temp=c2;

c2=c1;

c1=temp;

if(t2==1){

c2=-c2;

if(t1==1){

c1=-c1;

putpixel(c1+a,c2+b,WHITE);

while(x<=y){

if(p<0){
x++;

int c1=x,c2=y;

if(t3==1){

int temp=c2;

c2=c1;

c1=temp;

if(t2==1){

c2=-c2;

if(t1==1){

c1=-c1;

putpixel(c1+a,c2+b,WHITE);

p=p+2*x+1;

}else{

x++;

y--;

int c1=x,c2=y;

if(t3==1){

int temp=c2;

c2=c1;
c1=temp;

if(t2==1){

c2=-c2;

if(t1==1){

c1=-c1;

putpixel(c1+a,c2+b,WHITE);

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

int main()

clrscr();

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

int x,y,r;

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

midPoint(x,y,r,0,0,0);

midPoint(x,y,r,0,0,1);

midPoint(x,y,r,0,1,1);
midPoint(x,y,r,0,1,0);

midPoint(x,y,r,1,1,0);

midPoint(x,y,r,1,1,1);

midPoint(x,y,r,1,0,1);

midPoint(x,y,r,1,0,0);

getch();

return 0;

OUTPUT:

LEARNING:

In this experiment, I learnt and successfully implemented Mid-point circle algorithm.


EXPERIMENT-4
AIM: Write a program to implement Midpoint Ellipse algorithm.
THEORY:
Given parameters (rx,ry) and (xc,yc), we determine points (x,y) for an ellipse in standard position
centred on the origin, after which we shift the points so that ellipse is centred at (xc,yc). If we wish
to display the ellipse in non-standard position, we could then rotate the ellipse about its centre
coordinates to reorient the major and minor axes.
Ellipse function can be defined as:
fellipse(x,y)=ry2x2+rx2y2-rx2ry2
According to this function, there are some properties which have been generated that are:
1. fellipse(x,y)<0 which means (x,y) is inside the ellipse boundary.
2. fellipse(x,y)>0 which means (x,y) is outside the ellipse boundary.
3. fellipse(x,y)=0 which means (x,y) is on the ellipse boundary.
I. REGION 1
• The initial decision parameter for region 1 is: p10=ry2+1/4rx2-rx 2ry
• For every xk position in region 1 :
If p1k<0 then the next point along the is (xk+1 , yk) and p1k+1=p1k+2ry2xk+1+ry2
Else, the next point is (xk+1, yk-1 ) and p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2

II. REGION 2
• The initial decision parameter for region 2 is [using the last point of region 1 as(x0, y0)]
is: p20=ry2(x0+1/2)2+rx2 (y0-1)2-rx2ry2
• At each yk in region 2,
If p2k<0 the next point is (xk, yk+1) and p2k+1=p2k-2rx2yk+1+rx2.
Else, the next point is (xk+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2
PROGRAM:

#include<graphics.h>

#include<iostream.h>

#include<conio.h>

void main()

int gd = DETECT, gm;

int xc, yc, x, y, rx, ry;

float p;

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

cout<<”Center:”;

cin>>xc >>yc;

cout<<endl <<”Enter x, y radius”;

cin>>rx >>ry;

p=ry*ry-rx*rx*ry+rx*rx/4;

x=0, y=ry;

while (ry*ry*x<= rx*rx*y)

if (p<0)

x++;

p+=2*ry*ry*x+ry*ry;

else
{

x++; y--;

p+=2*ry*ry*x-2*rx*rx*y-ry*ry;

putpixel(xc+x,yc+y,WHITE);

putpixel(xc+x,yc-y,WHITE);

putpixel(xc-x,yc+y,WHITE);

putpixel(xc-x,yc-y,WHITE);

p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;

while (y>0)

if (p<=0)

x++; y--;

p+=2*ry*ry*x-2*rx*rx*y+rx*rx;

else

y--;

p += -2*rx*rx*y+rx*rx;

putpixel(xc+x,yc+y,WHITE);

putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);

putpixel(xc-x,yc-y,WHITE);

getch();

closegraph();

return 0;

OUTPUT:

LEARNING:

· Symmetry considerations are used to reduce computations. An ellipse in standard position is


symmetric between quadrants, but unlike a circle, it is not symmetric between the two octants of
a quadrant.

· We must calculate pixel positions along the elliptical arc throughout one quadrant and then obtain
the positions in the other three quadrants through symmetry.

· Every quadrant is divided into two regions and different decision parameters are used for these
regions and accordingly points are calculated.
EXPERIMENT-5
AIM: Write a program to implement Flood Fill algorithm.
THEORY:

Sometimes we come across an object where we want to fill the area and its boundary with different
colors. We can paint such objects with a specified interior color instead of searching for particular
boundary color as in boundary filling algorithm.

Instead of relying on the boundary of the object, it relies on the fill color. In other words, it replaces
the interior color of the object with the fill color. When no more pixels of the original interior color
exist, the algorithm is completed.

In Flood Fill algorithm we start with some seed and examine the neighboring pixels; however
pixels are checked for a specified interior color instead of boundary color and are replaced by a
new color. It can be done using 4 connected or 8 connected region method.

PROGRAM:

#include<stdio.h>

#include<graphics.h>

#include<dos.h>

void floodFill(int x,int y,int oldcolor,int newcolor)

if(getpixel(x,y) == oldcolor)

putpixel(x,y,newcolor);

floodFill(x+1,y,oldcolor,newcolor);

floodFill(x,y+1,oldcolor,newcolor);

floodFill(x-1,y,oldcolor,newcolor);

floodFill(x,y-1,oldcolor,newcolor);

}
}

int main()

int gm,gd=DETECT,radius;

int x,y;

printf("Enter x and y positions for circle\n");

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

printf("Enter radius of circle\n");

scanf("%d",&radius);

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

circle(x,y,radius);

floodFill(x,y,0,15);

delay(5000);

closegraph();

return 0;

}
OUTPUT:

LEARNING:

· Flood fill algorithm has filled the desired circle completely.

· The flood fill algorithm takes three parameters: a start node, a target color, and a replacement
color. The algorithm looks for all nodes in the array which are connected to the start node by a
path of the target color, and changes them to the replacement color.

· The algorithm can be modified to reduce storage requirements of the stack by filling horizontal
scans i.e. we stack only the beginning positions for those pixel spans having old color. In this
modified version, starting at the first position of each span, the pixel values are replaced until a
value other than old color is encountered.

· We can also show an area bordered by several different color regions too.
EXPERIMENT-6
AIM: Write a program to implement Boundary Fill algorithm.
THEORY:
The boundary fill algorithm works as its name. This algorithm picks a point inside an object and
starts to fill until it hits the boundary of the object. The color of the boundary and the color that
we fill should be different for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire object. The boundary
fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.

Algorithm:

1. Create a function named as boundaryfill with 4 parameters (x,y,f_color,b_color).

void boundaryfill(int x,int y,int f_color,int b_color)

if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)

putpixel(x,y,f_color);

boundaryfill(x+1,y,f_color,b_color);

boundaryfill(x,y+1,f_color,b_color);

boundaryfill(x-1,y,f_color,b_color);

boundaryfill(x,y-1,f_color,b_color);

2. Call it recursively until the boundary pixels are reached.


3. Stop.
PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void fill_right(int x,int y);

void fill_left(int x,int y);

void main()

int gd=DETECT,gm,x,y,n,i;

clrscr();

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

printf("*** Boundary Fill algorithm ***");

/*- draw object -*/

line (50,50,200,50);

line (200,50,200,300);

line (200,300,50,300);

line (50,300,50,50);

/*- set seed point -*/

x=100; y=100;

fill_right(x,y);

fill_left(x-1,y);

getch();

}
void fill_right(int x,int y)

if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))

putpixel(x,y,RED);

fill_right(++x,y); x=x-1;

fill_right(x,y-1);

fill_right(x,y+1);

delay(1);

void fill_left(int x,int y)

if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))

putpixel(x,y,RED);

fill_left(--x,y); x=x+1;

fill_left(x,y-1);

fill_left(x,y+1);

delay(1);

}
OUTPUT:

LEARNING:

1. Boundary Fill is used for the coloring figures in computer graphics. Here, area gets
colored with pixels of a chosen color as boundary this giving the technique its name.
2. Boundary fill fills the chosen area with a color until the given colored boundary is found.
3. This algorithm is also recursive in nature as the function returns when the pixel to be
colored is the boundary color or is already the fill color.
EXPERIMENT-7
AIM: Write a program to implement translation of an object.
THEORY:

A translation process moves every point a constant distance in a specified direction. It can be
described as a rigid motion. A translation can also be interpreted as the addition of a constant
vector to every point, or as shifting the origin of the coordinate system.

Suppose, If point P(X, Y) is to be translated by amount Dx and Dy to a new location P’(X’, Y’)
then new coordinates can be obtained by adding Dx to X and Dy to Y as:

X’ = Dx + X

Y’ = Dy + Y

Or

P’ = T + P

where P’ = [X’,Y’] ; T = [Dx,Dy]; P = [X,Y];

Algorithm

1. Start

2. Initialize the graphics mode.

3. Construct a 2D object

4. Translation
a. Get the translation value tx, ty
b. Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
c. Plot (x’,y’)
PROGRAM:

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

void translateRectangle ( int P[][2], int T[])

/* init graph and rectangle() are used for

representing rectangle through graphical functions */

int gd = DETECT, gm, errorcode;

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

setcolor (2);

// rectangle (Xmin, Ymin, Xmax, Ymax)

// original rectangle

rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);

// calculating translated coordinates

P[0][0] = P[0][0] + T[0];

P[0][1] = P[0][1] + T[1];

P[1][0] = P[1][0] + T[0];

P[1][1] = P[1][1] + T[1];


translated rectangle (Xmin, Ymin, Xmax, Ymax)

setcolor(3);

rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);

closegraph();

getch();

int main()

// Xmin, Ymin, Xmax, Ymax as rectangle

// coordinates of top left and bottom right points

int P[2][2] = {100, 100, 200, 200};

int T[] = {50, 70}; // translation factor

translateRectangle (P, T);

return 0;

}
OUTPUT:

LEARNING:

Translation can be achieved by any given factor by summation of the translation factor to the
original coordinates of the object.

Advantages:

● Useful for coordinate transformations.


● Used extensively while performing 2D and 3D transformations.

● Change of Basis.
EXPERIMENT-8
AIM: Write a program to implement 2D translation of a triangle.
THEORY:
Translation refers to simply moving an object by a given distance. The translation parameter in x
direction is given by tx and in the y direction it is given by ty,
x’=x+tx y’=y+ty
It is a process which moves every point a constant distance in a specified direction. It can be
described as a rigid motion. A translation can also be interpreted as the addition of a constant
vector to every point, or as shifting the origin of the coordinate system.
Suppose, If point P(X, Y) is to be translated by amount Dx and Dy to a new location P’(X’, Y’)
then new coordinates can be obtained by adding Dx to X and Dy to Y as:
X’ = Dx + X
Y’ = Dy + Y
Or
P’ = T + P
where P’ = [X’,Y’] ; T = [Dx,Dy]; P = [X,Y];
For a triangle, there is a set of 3 points, on which the above translation is applied individually.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;

void draw();
void tri();

void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm,"..\\bgi ");
printf("Enter the 1st point for the
triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the
triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the
triangle:");
scanf("%d%d",&x3,&y3);

cleardevice();
draw();
getch();
tri();
getch();
}

void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void tri()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Transaction coordinates");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}

OUTPUT:
LEARNING:
Translation can be achieved by any given factor by summation of the translation factor to the
original coordinates of the object.
Advantages:
● Useful for coordinate transformations.
● Used extensively while performing 2D and 3D transformations.
● Change of Basis.
EXPERIMENT-9
AIM: Write a program to implement 2D rotation of an object.
THEORY:
In rotation, we rotate the object at particular angle θ (theta) 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’).

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 we have the Roation matrix R as,
PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

#include<math.h>

#define ROUND(a) ((int)(a+0.5))

void ddaline(int x1, int y1, int x2, int y2,int color)

{ float xsteps, ysteps, x=x1, y=y1;

int dx = x2-x1;

int dy = y2-y1;

int steps,k=1;

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

steps=abs(dx);

else steps=abs(dy);

xsteps= dx/(float)steps;

ysteps= dy/(float)steps;

putpixel(ROUND(x),ROUND(y),color);

while(k<=steps)
{

x+=xsteps;

y+=ysteps;

putpixel(ROUND(x), ROUND(y),color);

k++;

void rotate(int x1, int y1, int x2, int y2, float theta)

{ int xtmp, ytmp;

float th = (3.14 * theta )/180;

xtmp = x1 + ROUND ((x2-x1)*cos(th) - (y2-y1)*sin(th));

ytmp = y1 + ROUND ((x2-x1)*sin(th) + (y2-y1)*cos(th));

ddaline(x1,y1,x2,y2,10);

ddaline(x1,y1,xtmp,ytmp,12);

int main()

int x1, x2, y1, y2;

float theta;
int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

errorcode = graphresult();

if (errorcode != grOk)

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

printf("Enter start point\n");

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

printf("Enter end point\n");

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

printf("Enter value of angle to rotate line about starting point\n");

scanf("%f", &theta);

rotate(x1, y1, x2, y2, theta);

getch();

closegraph();
return 0;

OUTPUT:

LEARNING:

● A rotation matrix is a matrix that is used to perform a rotation in Euclidean space. To


perform the rotation using a rotation matrix R, the position of each point must be
represented by a column vector v, containing the coordinates of the point. A rotated vector
is obtained by using the matrix multiplication Rv.
● Rotation of a complete object can viewed as rotation of all its constituent lines about
a fixed point.
● Rotation matrices are square matrices, with real entries. More specifically, they can
be characterized as orthogonal matrices with determinant 1.

● Coordinate rotations are a natural way to express the orientation of a camera, or the
attitude of a spacecraft, relative to a reference axes-set. These are also used in various
image editing software to perform simple rotation of images.
EXPERIMENT-10
AIM: Write a program to implement 2D scaling of an object.
THEORY:

A scaling can be represented by a scaling matrix. To scale an object by a vector v = (vx, vy, vz),
each point p = (px, py, pz) would need to be multiplied with this scaling matrix:

As shown below, the multiplication will give the expected result:

Such a scaling changes the diameter of an object by a factor between the scale factors, the area by
a factor between the smallest and the largest product of two scale factors, and the volume by the
product of all three.

The scaling is uniform if and only if the scaling factors are equal (vx = vy = vz). If all except one
of the scale factors are equal to 1, we have directional scaling.

In the case where vx = vy = vz = k, the scaling is also called an enlargement or dilation by a factor
k, increasing the area by a factor of k2 and the volume by a factor of k3.

A scaling in the most general sense is any affine transformation with a diagonalizable matrix. It
includes the case that the three directions of scaling are not perpendicular. It includes also the case
that one or more scale factors are equal to zero (projection), and the case of one or more negative
scale factors. The latter corresponds to a combination of scaling proper and a kind of reflection.
Along lines in a particular direction we take the reflection in the point of intersection with a plane
that need not be perpendicular; therefore it is more general than ordinary reflection in the plane.

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.
PROGRAM:

#include <stdio.h>

#include <conio.h>

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#define ROUND(a) ((int)(a+0.5))

void thickline(int X0, int Y0, int X1, int Y1) {

// calculate dx & dy

int dx = X1 - X0;

int dy = Y1 - Y0;

// calculate steps required for generating pixels

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

// calculate increment in x & y for each steps

float Xinc = dx / (float) steps;

float Yinc = dy / (float) steps;

// Put pixel for each step

float X = X0;

float Y = Y0;
int i;

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

putpixel (ROUND(X),ROUND(Y),WHITE);// put pixel at (X,Y)

X += Xinc; // increment in x at each step

Y += Yinc; // increment in y at each step

delay(50); // for visualization of line-

// generation step by step

void scaling(int *x, int *y, int sx, int sy) {

*x =(*x)*sx;

*y = (*y)*sy;

int main() {

int x0 = 50, y0 = 50, x1 = 100, y1 = 100;

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "..\\");

errorcode = graphresult();

if(errorcode != grOk) {

printf("Graphics Error: %s\n",grapherrormsg(errorcode));


printf("Press key to halt");

getch();

exit(1);

thickline(x0,y0,x0,y1);

thickline(x0,y0,x1,y0);

thickline(x0,y1,x1,y1);

thickline(x1,y0,x1,y1);

delay(100);

closegraph();

clrscr();

scaling(&x0,&y0,2,2);

scaling(&x0,&y1,2,2);

scaling(&x1,&y0,2,2);

scaling(&x1,&y1,2,2);

gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver, &gmode, "..\\");

errorcode = graphresult();

if(errorcode != grOk) {

printf("Graphics Error: %s\n",grapherrormsg(errorcode));

printf("Press key to halt");

getch();
exit(1);

thickline(x0,y0,x0,y1);

thickline(x0,y0,x1,y0);

thickline(x0,y1,x1,y1);

thickline(x1,y0,x1,y1);

getch();

closegraph();

return 0;

}
OUTPUT:

LEARNING:

In projective geometry, often used in computer graphics, points are represented using
homogeneous coordinates. To scale an object by a vector v = (vx, vy, vz), each homogeneous
coordinate vector p = (px, py, pz, 1) would need to be multiplied with this projective transformation
matrix.

You might also like