Computer Graphics Lab
Computer Graphics Lab
Computer Graphics Lab
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.
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
yi+1=yi +m , xi+1=xi+1
Until x = x2
xi+1=xi+1/m, yi+1=yi+1
Until y = y2
PROGRAM:
#include<stdio.h>
#include<graphics.h>
float Xinc,Yinc,X,Y;
dy = Y1 - Y0 , dx = X1 - X0 ;
putpixel (X,Y,12);
X += Xinc;
Y += Yinc;
int main()
scanf("%d,%d",&X0,&Y0);
printf("ENTER RIGHT POINT CORDINATES(X,Y): ");
scanf("%d,%d",&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.
· 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
Bresenham’s line Algorithm is use to draw line same as DDA. It has following advantages over
DDA:
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:
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
2. take (x0,y0)=(0,r)
4. if Pk<0
5. else
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
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:
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()
float p;
cout<<”Center:”;
cin>>xc >>yc;
cin>>rx >>ry;
p=ry*ry-rx*rx*ry+rx*rx/4;
x=0, y=ry;
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:
· 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>
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;
scanf("%d%d",&x,&y);
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:
· 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:
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);
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
int gd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
delay(1);
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
Algorithm
1. Start
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>
setcolor (2);
// original rectangle
setcolor(3);
closegraph();
getch();
int main()
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:
● 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)
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>
void ddaline(int x1, int y1, int x2, int y2,int color)
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)
ddaline(x1,y1,x2,y2,10);
ddaline(x1,y1,xtmp,ytmp,12);
int main()
float theta;
int gdriver = DETECT, gmode, errorcode;
errorcode = graphresult();
if (errorcode != grOk)
getch();
exit(1);
scanf("%f", &theta);
getch();
closegraph();
return 0;
OUTPUT:
LEARNING:
● 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:
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>
// calculate dx & dy
int dx = X1 - X0;
int dy = Y1 - Y0;
float X = X0;
float Y = Y0;
int i;
*x =(*x)*sx;
*y = (*y)*sy;
int main() {
errorcode = graphresult();
if(errorcode != grOk) {
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);
errorcode = graphresult();
if(errorcode != grOk) {
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.