Computer Graphics Labmanual

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

Experiment – 1

Digital Differential Analyzer Algorithm


1 :Digital differential analyzer (DDA) is used for linear interpolation of variables over an interval between
given start, end points and for rasterization of lines, triangles and polygons. Using DDA Algorithm, Write
a C-Program to draw a line segment between two given points?

Aim: To implement DDA Algorithm for drawing a line segment between two given end points A (x1, y1)
and B(x2, y2).

Description: DDA algorithm is an incremental scan conversion method. Here we perform calculations
at each step using the results from the preceding step. The characteristic of the DDA algorithm is to take
unit steps along one coordinate and compute the corresponding values along the other coordinate. The
unit steps are always along the coordinate of greatest change, e.g. if dx = 10 and dy = 5, then we would
take unit steps along x and compute the steps along y.
In DDA we need to consider two cases;
One is slope of the line less than or equal to one (|m| ≤1) and slope of the line greater than one (m|
> 1).
1) When |m| ≤ 1 means y2-y1 = x2-x1 or y2-y1 <x2-x1.In both these cases we assume x to be the
major axis. Therefore we sample x axis at unit intervals and find the y values corresponding to each x
value. We have the slope equation as
∆y=m∆x
y2-y1 = m (x2-x1)
In general terms we can say that y i+1 - yi = m(x i+1 - xi ). But here ∆ x = 1; therefore the equation
reduces to y i+1= yi + m = yi + dy/dx.
2) When | m| > 1 means y2-y1> x2-x1 and therefore we assume y to be the major axis. Here we sample
y axis at unit intervals and find the x values corresponding to each y value. We have the slope equation
as
∆y=m∆x
y2-y1 = m (x2-x1)
Algorithm:
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare
gdriver=DETECT, mode.
3. Initialize the graphic mode with the path location in TurboC3 folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1, y1) into the frame buffer; that is, plot the first point. put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs (dx) > abs (dy), do s=abs(dx).
8. Otherwise s= abs(dy).
9. Then xi=dx/s and yi=dy/s.
10. Start from k=0 and continuing till k<s,the points will be
i. x=x+xi.
ii. Y=y+yi.
11. Plot pixels using putpixel at points (x,y) in specified colour.
12. Close Graph and stop.

SDHR Degree & PG College


Program:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
float round(float a);
void main()
{
int gd=DETECT,gm;
// gd=graphics driver (detects best graphics driver and assigns it as default, gm=graphics
mode.
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1);
printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\turboc3\\BGI");//initializes the graph
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincr=dx/steps;
yincr=dy/steps;
x=x1;
y=y1;
for(k=1;k<=steps;k++)
{
delay(100);//for seeing the line drawing process slowly.
x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"DDA"); // for printing text at desired screen location.
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph(); // closes the graph and comes back to previous graphic mode.
}
float round(float a)
{
int b=a+0.5;
return b;
}

SDHR Degree & PG College


Output:

SDHR Degree & PG College


Experiment 2
Bresenham’s Line Drawing Algorithm

2: Bresenham’s line algorithm is an algorithm which determines which order to form a close
approximation to a straight line between two given points. Write a C program for determining pixel
activation list between two given points in order to draw line segment using bresenham’s Line
drawing algorithm?

Aim: To implement Bresenham’s line drawing algorithm for drawing a line segment between two
given endpoints A (x1, y2) and B(x2, y2).

Description:
Basic Concept:
⚫ Move across the x axis in unit intervals and at each step choose between two
different y coordinates

⚫ For example, from position (2, 3) we have to choose between (3, 3) and (3, 4). We
would like the point that is closer to the original line

⚫ So we have to take decision to choose next point. So next pixels are selected based
on the value of decision parameter p. The equations are given in below algorithm.

SDHR Degree & PG College


Algorithm:

BRESENHAM’S LINE DRAWING ALGORITHM

1. Input the two line end-points, storing the left end-point in (x0, y0)

2. Plot the point (x0, y0)

3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision
parameter as:

p0  2  y   x
4. 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:

p k  1  p k  2  y

Otherwise, the next point to plot is (xk+1, yk+1) and:

p k  1  p k  2  y  2  x
5. Repeat step 4 (Δx – 1) times

NOTE: The algorithm and derivation above assumes slopes are less than 1. For other slopes
we need to adjust the algorithm slightly

SDHR Degree & PG College


Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\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=(2*dy-dx);
while(x<=x2)
{
if(p<0)
{
x=x+1;
p=p+2*dy;
}
else
{
x=x+1;
y=y+1;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,7);
}
getch();
closegraph();
}

SDHR Degree & PG College


Output:

SDHR Degree & PG College


Experiment-3
Midpoint Circle Generation Algorithm

3: Using Midpoint circle generation algorithm which is a variant of Bresenham's line algorithm, write a C-
Program to generate pixel activation list for drawing a circle with a given centre of circle P(x, y) and a
radius r?

Aim: To implement midpoint circle generation algorithm or bresenham’s circle algorithm for drawing a circle
of given center (x, y) and radius r.

Description:

Circles have the property of being highly symmetrical, which is handy when it comes to
drawing them on a display screen.
 We know that there are 360 degrees in a circle. First we see that a circle is symmetrical about the x
axis, so only the first 180 degrees need to be calculated.
 Next we see that it's also symmetrical about the y axis, so now we only need to calculate the first 90
degrees.
 Finally we see that the circle is also symmetrical about the 45 degree diagonal axis, so we only need
to calculate the first 45 degrees.
 We only need to calculate the values on the border of the circle in the first octant. The other values
may be determined by symmetry.

Bresenham's circle algorithm calculates the locations of the pixels in the first 45 degrees. It assumes that
the circle is centered on the origin. So for every pixel (x, y) it calculates, we draw a pixel in each of the eight
octants of the circle. This is done till when the value of the y coordinate equals the x coordinate. The pixel
positions for determining symmetry are given in the below algorithm.

⚫ Assume that we have just plotted point (xk, yk)

⚫ The next point is a choice between (xk+1, yk)and (xk+1, yk-1)

⚫ We would like to choose the point that is nearest tothe actual circle

⚫ So we use decision parameter here to decide.

SDHR Degree & PG College


Algorithm:

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

( x 0 , y 0 )  ( 0 , r )
2. Calculate the initial value of the decision parameter as:

p  5  r
0
4
3. 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:

p k  1  p k  2 x k  1  1
Otherwise the next point along the circle is (xk+1, yk-1) and:

p k  1  p k  2 x k  1  1  2 y k  1
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the
coordinate values:
6. Repeat steps 3 to 5 until x >= y

x  x  xc y y  y c

SDHR Degree & PG College


Symmetric pixel positions:

 putpixel(xc+x,yc-y,GREEN); //For pixel (x,y)


 putpixel(xc+y,yc-x, GREEN); //For pixel (y,x)
 putpixel(xc+y,yc+x, GREEN); //For pixel (y,-x)
 putpixel(xc+x,yc+y, GREEN); //For pixel (x,-y)
 putpixel(xc-x,yc+y, GREEN); //For pixel (-x,-y)
 putpixel(xc-y,yc+x, GREEN); //For pixel (-y,-x)
 putpixel(xc-y,yc-x, GREEN); //For pixel (-y,x)
 putpixel(xc-x,yc-y, GREEN); //For pixel (-x,y)

SDHR Degree & PG College


Program:

#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int,int,int);
void symmetry(int,int,int,int);
void main()
{
int xc,yc,R;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the center of the circle:\n");
printf("Xc =");
scanf("%d",&xc);
printf("Yc =");
scanf("%d",&yc);
printf("Enter the radius of the circle :");
scanf("%d",&R);
draw_circle(xc,yc,R);
getch();
closegraph();
}

void draw_circle(int xc,int yc,int rad)


{
int x = 0;
int y = rad;
int p = 1-rad;
symmetry(x,y,xc,yc);
for(x=0;y>x;x++)
{
if(p<0)
p += 2*x + 3;
else
{
p += 2*(x-y) + 5;
y--;
}
symmetry(x,y,xc,yc);
delay(50);
}
}

void symmetry(int x,int y,int xc,int yc)


{
putpixel(xc+x,yc-y,GREEN); //For pixel (x,y)
delay(50);
putpixel(xc+y,yc-x, GREEN); //For pixel (y,x)
delay(50);
putpixel(xc+y,yc+x, GREEN); //For pixel (y,-x)

10

SDHR Degree & PG College


delay(50);
putpixel(xc+x,yc+y, GREEN); //For pixel (x,-y)
delay(50);
putpixel(xc-x,yc+y, GREEN); //For pixel (-x,-y)
delay(50);
putpixel(xc-y,yc+x, GREEN); //For pixel (-y,-x)
delay(50);
putpixel(xc-y,yc-x, GREEN); //For pixel (-y,x)
delay(50);
putpixel(xc-x,yc-y, GREEN); //For pixel (-x,y)
delay(50);
}

Output:

SDHR Degree & PG College


Experiment-4
Ellipse Generation Algorithm

4: Using Midpoint ellipse generation algorithm which is a variant of Bresenham's line algorithm, write a C-
Program to generate pixel activation list for drawing a ellipse?

Aim: To implement the Ellipse Generation Algorithm for drawing an ellipse of given center(x, y) and radius rx
and ry.

Description:

Basic Concept: In Ellipse,

Symmetry between quadrants exists

Not symmetric between the two octants of a quadrant

Thus, we must calculate pixel positions along the elliptical arc through one quadrant and then we obtain
positions in the remaining 3 quadrants by symmetry

The next pixel is chosen based on the decision parameter. The required conditions are given in following
algorithm.

SDHR Degree & PG College


Algorithm:

1. Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered on the
origin as

(x0, y0) = (0, ry)

2. Calculate the initial parameter in region 1 as

p1  r 2  r 2 r  1
r2
0 y x y 4 x

3. At each xi position, starting at i = 0, if p1i < 0, the next point along the ellipse centered on (0,
0) is (xi + 1, yi) and p1  p1  2 r 2 x  r2
i 1 i y i 1 y

Otherwise, the next point is (xi + 1, yi – 1) and

p1  p1  2 r 2 x  2r 2 y  r2
i 1 i y i 1 x i 1 y

and continue until 2r 2


x  2r 2
y
y x

4. (x0, y0) is the last position calculated in region 1. Calculate the initial parameter in region 2 as

p 2 0  r y2 ( x 0  12 ) 2  rx2 ( y 0  1) 2  rx2 r y2

5. At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse centered on (0,
0) is (xi, yi – 1) and

p2  p 2  2r 2 y  r2
i1 i x i 1 x


 p 2 i  2 r y x i 1  2 r x y i 1  rx2
2 2
Otherwise, the next point is (xi + 1, yi – 1) and p 2 i 1

Use the same incremental calculations as in region 1. Continue until y = 0.

6. For both regions determine symmetry points in the other three quadrants.

7. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot
the coordinate values

x = x + xc , y = y + yc

13

SDHR Degree & PG College


Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int rx,ry;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the center point :");
scanf("%d%d",&xc,&yc);
printf("Enter the value for Rx and Ry :");
scanf("%d%d",&rx,&ry);
x=0;
y=ry;
disp();
p1=(ry*ry)-(rx*rx*ry)+(rx*rx)/4;
while((2.0*ry*ry*x)<=(2.0*rx*rx*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*ry*ry*x)+(ry*ry);
else
{
y--;
p1=p1+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(ry*ry);
}
disp();
x=-x;
disp();
x=-x;
}
x=rx;
y=0;
disp();
p2=(rx*rx)+2.0*(ry*ry*rx)+(ry*ry)/4;
while((2.0*ry*ry*x)>(2.0*rx*rx*y))
{
y++;
if(p2>0)
p2=p2+(rx*rx)-(2.0*rx*rx*y);
else
{
x--;

14

SDHR Degree & PG College


}
getch();

OUTPUT:

SDHR Degree & PG College


}
disp();y=-y; disp();y=-y;

closegraph();
}
vvoid disp()
{

SDHR Degree & PG College


Experiment 5
Two Dimensional Transformations

4: Write a C-program for performing the basic 2D transformations such as translation, Scaling, Rotation,
shearing and reflection for a given 2D object?

Aim: To apply the basic 2D transformations such as translation, Scaling, Rotation, shearing and reflection for
a given 2D object.

Description: We have to perform 2D transformations on 2D objects. Here we perform transformations on a


line segment.

1. Translation: Translation is defined as moving the object from one position to another position along
straight line path.

We can move the objects based on translation distances along x and y axis. tx denotes translation distance
along x-axis and ty denotes translation distance along y axis.

Translation Distance: It is nothing but by how much units we should shift the object from one location to
another along x, y-axis.
Consider (x,y) are old coordinates of a point. Then the new coordinates of that same point (x’,y’) can be
obtained as follows:
X’=x+tx,Y’=y+ty
We denote translation transformation as P. we express above equations in matrix form as:

SDHR Degree & PG College


2. Scaling: scaling refers to changing the size of the object either by increasing or decreasing.
We willincrease or decrease the size of the object based on scaling factors along x and y-axis.

If (x, y) are old coordinates of object, then new coordinates of object after applying scaling
transformation are obtained as:
x’=x*sx
y’=y*sy.
sx and sy are scaling factors along x-axis and y-axis. we express the above equations in matrix form as:
 x sx 0   x 
    0 
y s  y 
   y  

Scaling Matrix

3. Rotation: A rotation repositions all points in an object along a circular path in the plane centered at the
pivot point. We rotate an object by an angle theta.

New coordinates after rotation depend on both x and y


• x’ = xcosθ -y sinθ
• y’ = xsinθ+ ycosθ
• or in matrix form:
P' = R • P, cos   sin  
R   
R-rotation matrix.  sin  cos  



25

SDHR Degree & PG College


4. Reflection: Reflection is nothing but producing mirror image of an object. Reflection can be done just by
rotating the object about given axis of reflection with an angle of 180 degrees.

5. Shear:

1. Shear is the translation along an axis by an amount that increases linearly with another axis (Y). It
produces shape distortions as if objects were composed of layers that are caused to slide over
each other.
2. Shear transformations are very useful in creating italic letters and slanted letters from regular
letters.
3. Shear transformation changes the shape of the object to a slant position.

4. Shear transformation is of 2 types:

a. X-shear: changing x-coordinate value and keeping y constant

x’=x+shx*y
y’=y
b.Y-shear: changing y coordinates value and keeping x constant
x’=x
y’=y+shy*x
shx and shy are shear factors along x and y-axis.

26

SDHR Degree & PG College


1. Program for translation:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point of line
segment:");scanf("%d %d",&x1,&y1);
printf("Enter the ending point of line
segment:");scanf("%d %d",&x2,&y2);
printf("Enter translation distances tx,ty:\n");
scanf("%d%d",&tx,&ty);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
setcolor(7);
line(x3,y3,x4,y
4);
outtextxy(x4+2,y4+2,"Line after translation");
getch();
}
OUTPUT:

SDHR Degree & PG College


1. P <
r c
o o
g n
r i
a o
m .
h
f >
o
r #
i
s n
c c
a l
l u
i d
n e
g <
: g
r
# a
i p
n h
c i
l c
u s
d .
e h
< >
s
t #
d i
i n
o c
. l
h u
> d
e
# <
i m
n a
c t
l h
u .
d h
e >

SDHR Degree & PG College


void ,x4,y4;
main() initgraph(&gd,&gm,"
{ C:\\TurboC3\\BGI");
i
n printf("Enter the starting point
t
coordinates:");scanf("%f
g %f",&x1,&y1);
d printf("Enter the ending point
= coordinates:");scanf("%f
D %f",&x2,&y2);
E printf("Enter scaling
T factors sx,sy:\n");
E
scanf("%f%f",&sx,&sy
C
T );
, s
g et
m c
; ol
f o
l r(
o 5)
a ;
t li
n
x e(
1 x
, 1,
y y
1 1,
, x
x 2,
2 y
, 2)
y ;
2 outtextxy(x2+2,y2+2,"
, Original line");
s x3=x1*sx;
x y
, 3
s =
y y
, 1
x *
3 s
, y
y ;
3

SDHR Degree & PG College


x y
4 4
= )
x ;
2 o
* u
s t
x t
; e
x
y t
4 x
= y
y (
2 x
* 3
s +
y 2
; ,
y
s 3
e +
t 2
c ,
o ”
l L
o i
r n
( e
7
) a
; f
l t
i e
n r
e s
( c
x a
3 l
, i
y n
3 g
, ”
, )
x ;
4 g
, e

SDHR Degree & PG College


t
line(x3,y3,x4,y4);
c
outtextxy(x3+2,y3+2,"Line after scaling");
;
getch();

Output:

SDHR Degree & PG College


Experiment-6
Coloring the Pictures

5: By using the concept of flood fill algorithm, Write a C- program for filling a given rectangle object
with color?

Aim: To implement flood fill algorithm for filling a rectangle with given color.

Description:

Sometimes we want to fill in (recolor) an area that is not defined within a single color boundary. We
paint such areas by replacing a specified interior color instead of searching for a boundary color value.
This approach is called a flood-fill algorithm.
1. We start from a specified interior pixel (x, y) and reassign all pixel values that are currently set to a
given interior color with the desired fill color.
2. If the area has more than one interior color, we can first reassign pixel values so that all interior pixels
have the same color.
3. Using either 4-connected or 8-connected approach, we then step through pixel positions until all
interior pixels have been repainted.

4-connected approach 8-connected approach

SDHR Degree & PG College


Algorithm:

4- connected approach:

1. We can implement flood fill algorithm by using recursion.

2. First all the pixels should be reassigned to common color. here common color is black.

3. Start with a point inside given object, check the following condition:

if(getpixel(x,y)==old_col)---old_col is common color

4. If above condition is satisfied, then following 4 steps are followed in filling the object.

//Recursive 4-way floodfill.

putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);

8- connected approach:

1. We can implement flood fill algorithm by using recursion.

2. First all the pixels should be reassigned to common color. here common color is black.

3. Start with a point inside given object, check the following condition:

if(getpixel(x,y)==old_col)---old_col is common color

4. If above condition is satisfied, then following 8 steps are followed in filling the object.

//Recursive 4-way floodfill.

putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
flood(x + 1, y - 1, fill_col, old_col);
flood(x + 1, y + 1, fill_col, old_col);
flood(x - 1, y - 1, fill_col, old_col);
flood(x - 1, y + 1, fill_col, old_col);

SDHR Degree & PG College


Program for 4-connected flood fill:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void flood(int,int,int,int);

void main()
{
int gd,gm=DETECT;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
rectangle(50,50,100,100);
flood(55,55,9,0);
getch();
}

void flood(int x,int y, int fill_col, int old_col)


{
if(getpixel(x,y)==old_col)
{
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
}
}
Output:

SDHR Degree & PG College


SDHR Degree & PG College

You might also like