Computer Graphics: (CO 313) (Lab File)

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

COMPUTER GRAPHICS

(CO 313)
(LAB FILE)

Name: Aditya Sharma


Roll No: 2k17/CO/029
Batch: A1
INDEX

S.No. PROGRAM DATE SIGN

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.
EXPERIMENT 1

Aim:

Write a program to implement DDA Line Generation Algorithm.

Theory:

Line:

A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you
can draw a line.
There are three-line drawing algorithms:
• DDA
• Bresenham’s
• Mid-Point
Where,
We refer the one point of line as X0, Y0 and the second point of line as X1, Y1. The lines appear continuous but
they are actually made of pixels. The lines are drawn by a process known as rasterization.

Some terms associated with line generation:


Rasterization: Process of determining which pixels provide the best approximation to a desired line on the screen.
Scan Conversion: Combination of rasterization and generating the picture in scan line order. Following is the
explanation of DDA algorithm.

DDA Algorithm (Digital Differential Analyzer):

DDA was a mechanical device for numerical solution of differential equations-Line y=mx+h satisfies differential
equation dy/dx=m=(y2-y2)/(x2-x1).
The solution of the finite difference approximation is:
Xi+1=Xi+x
Yi+1=Yi+delta_y*(y2-y1)/(x2-x1)
DDA line generation algorithm uses repeated addition.
DDA line generation algorithm is the simple line generation algorithm which is explained step by step here:
Step 1 – Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2 – Calculate the difference between two end points.
dx=X1-X0
dy=Y1-Y0
Step 3 – Based on the calculated difference in step-2, you need to identify the number of steps to put pixels.
If dx>dy:
then you need more steps in x coordinate, otherwise in y coordinate.
If dx>dy:
Steps=absolute(dx)
Else:
Steps=absolute(dy)
Step 4 – Calculate the increment in x coordinate and y coordinate
X_inc=dx/(float)steps
Y_inc=dy/(float)steps
Step 5 – Put the pixel by successfully incrementing x and y coordinates accordingly and compete the drawing of the
line.
Code:

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

void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

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

printf("Enter the value of x1 and y1 : ");


scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);

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

if(dx>=dy)
step=dx;
else
step=dy;

dx=dx/step;
dy=dy/step;

x=x1;
y=y1;

i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}

closegraph();
}
Output:

Discussion:

Advantages of DDA algorithm:


• It is the simplest algorithm and it does not require special skills for implementation.
• It is faster method for calculating pixel positions than the direct use of equation y=mx+b.
• It eliminates the multiplication in the equation by making use of raster characteristics, so that appropriate increments
are applied in the x or y direction to find the pixel positions along the line path.

Disadvantages of DDA algorithm:


• Division logic is needed which switches it towards hardware logic.
• Floor integer values are used in place of normal integer values which may give different results.
• Floating point arithmetic is needed thus it is line consuming.

Findings and Learnings

1. With this experiment we got to learn about DDA’s line generation algorithm.
2. A C++ program was implemented which generated the line as expected.
3. We also got to know about the advantages and drawbacks of this algorithm.
EXPERIMENT 2

Aim:

Write a program to implement Bresenham Line Generation Algorithm.

Theory:

Line:

A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you
can draw a line.
There are three-line drawing algorithms:
• DDA
• Bresenham’s
• Mid-Point
Where,
We refer the one point of line as X0, Y0 and the second point of line as X1, Y1. The lines appear continuous but
they are actually made of pixels. The lines are drawn by a process known as rasterization.

Some terms associated with line generation:


Rasterization: Process of determining which pixels provide the best approximation to a desired line on the screen.
Scan Conversion: Combination of rasterization and generating the picture in scan line order.
Following is the explanation of Bresenham algorithm:

Bresenham Algorithm:

Bresenham’s line algorithm that determines the points of an n-dimensional raster that should be selected in order to
form a close approximation to a straight line between two points.it is commonly used to draw line primitives in a
bitmap image (e.g, on computer screen), as it uses only integer addition,subtraction & bit shifting ,all of which are
very cheap operations in standard computer architectures. It is an incremental error algorithm. It is one of the earliest
algorithms developed in the field of computer graphics.

Bresenham’s algorithm chooses the integer Y corresponding to the pixel center that is closest to the ideal(fraction) Y
for the same X; on successive columns Y can remain the same or increase by 1. The general equation of the line
through the endpoints is given by:
(Y - Y0) /( Y1 - Y0) = (X - X0) /( X1 - X0)
Since we know the column, X, the pixel’s row,y,is given by rounding this quantity to the nearest integer:

y= (y1-y0)*(x-x0)/(x1-x0) + y0

The slope △Y/△X depends on the endpoint coordinates only & can be precomputed , & the ideal Y for successive
integer values of X can be computed starting from & repeatedly adding the slope.
In practice, the algorithm does not keep track of the Y coordinate,which increases by m = △Y/△X each time
increases by one; it keeps an error bound at each stage,which represents the negative of the distance from (a) the
point where the line exits the pixel to (b) the top edge of the pixel.This value is first set to m-0.5 (due to using the
pixel’s center coordinates),and is incremented by m each time the x co-odinate is incremented by one .If error
becomes greater than 0.5 we know that the line has moved upwards one pixel,and that we must increment our y
coordinate and readjust the error to represent the distance from the top of the new pixel - which is done by
subtracting one from error.

Pseudocode :
plotline(x0,y0,x1,y1)
dx=x1-x0
dy=y1-y0
D=2*dy-dx
y=y0;
For x from x0 to x1
plot(x,y)
if(D>0)
y=y+1
D=D-2*dx
End if (D=D+2*dy)

CODE :

#include <iostream>
#include <graphics.h>
#include <math.h>
#include <dos.h>
using namespace std;
int main()
{
int x0, y0, x1, y1;
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;

initwindow(800,800);
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
delay(10);
x=x+1;
}
getch();
return 0;
}

OUTPUT :
Discussion

Advantages of Bresenham algorithm


1. It’s fast incremental algorithm.
2. It requires only integer calculation
3. It doesn’t have rounding and floating point operations
4. It provides high-speed

Disadvantages of Bresenham algorithm

The main advantage of Bresenham algorithm is its speed. The disadvantage of such a simple algorithm is that it is
meant for basic line drawing. The “advanced” topic of anti-aliasing is not a part of Bresenham algorithm, so to draw
smooth lines you’d want to use a different algorithm.

Findings and Learnings

4. With this experiment we got to learn about Bresenham’s line generation algorithm.
5. A C++ program was implemented which generated the line as expected.
6. We also got to know about the advantages and drawbacks of this algorithm.
EXPERIMENT 3

Aim: To write a program to implement a Midpoint Circle Algorithm

Theory:

A circle is a symmetrical figure Any circle-generating algorithm can take


advantage of the circle symmetric to plot right points for each value that the algorithm
calculate Eight-way symmetry is used by reflecting each calculate point around each
45-degree axis. For example, if we calculate one point seven more points could be found by reflection.

The eight points are :

P1 = (x,y)
P2 = (y.x)
P3 = (-y,x)
P4 = (-x, y)
P5 = (-x, -y)
P6 = (-y,x)
P7 = (y,-x)
P8 = (x,-y)

Equation of circle :
x2 + y2 = a2

We consider a circle in second octant from x = 0 to x y. We use the same concept of eight-way symmetry
to plot the entire circle
Principle: We have 2 pixels -
Upper-pixel (U) = (xi + 1 ,yi)
Lower-pixel (L) = (xi+1 , yi - 1)

We need to select, which of these pixels is closer to the true circle and is this algorithm we find the
midpoint between the two pixels. This mid-point, M is given as follows -
M(xi+1 , (yi+yi-1)/2)

l.e., the midpoint of L and U


Now, it depends on the midpoint, which pixel to select Like,

Midpoint is Plot

On the circle Ether U or L pixel

Outside the circle Plot U

Inside the circle Plot L

Where U and L are the Upper and Lower pixel respectively


To apply the midpoint method, we define a circle function –
F(x,y) x2+ y2-12= d
Any point (xy) on the boundary of the circle with radius r satisfies the equation
F(x )=0
Steps for Midpoint Circle Algorithm:

1. Input the radius, centre (xc,yc) and obtain the first point on the circumference of the circle. Centre
on the origin as :
(x0 , y0) = (0 , r) i.e. initialize starting point as x=0 and y=r.
2. Calculate the initial value of the decision parameter as
p = 1-r
3. Make a while loop as follows :
Do
{
Plot(x , y)
If (p<0){
x = x+1
y=y
p = p + 2*x + 1
}
else
{
x=x+1
y=y–1
p = p + 2*x – 2*y + 1
}
} while (x < y)
4. Determine symmetry points in other octants.
5. Stop.

Code:

#include <graphics.h>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float xc, yc,r;
cout<<”Enter centre points x and y : “;
cin>>xc>>yc;
cout<<”Enter radius of circle : “;
cin>>r;
initwindow(600,600);
float x=0, y=r;
float p=1-y;
while(x<=y)
{
putpixel(xc+x, yc+y, 5);
putpixel(xc-x, yc-y, 5);
putpixel(xc+x, yc-y, 5);
putpixel(xc-x, yc+y, 5);
putpixel(xc+y, yc+x, 5);
putpixel(xc-y, yc-x, 5);
putpixel(xc+y, yc-x, 5);
putpixel(xc-y, yc+x, 5);
delay(10);
if(p>=0)
{
p += 2*x – 2*y + 5;
y--;
}
else
{
p += 2*x+3;
}
x++;
}
getch();
return 0;
}

Output:

Discussion:

if radius r is specified as an integer since all increments are integers, then we


simply round d0 to (1-r) i.e.,
d0 = 1 – r
The error of being ¼ less than the precise value does not prevent d from getting the appropriate sign. It
does not even affect the rest of the scan-conversion process either because the decision variable is
updated with integer increments only in the subsequent steps.

As in Bresenham’s line algorithm, the midpoint method calculates pixel positions along the circumference
of a circle using integer additions and subtractions, assuming that the circle parameters are specified in
integer screen coordinate.

Findings and Learnings :

We have successfully implemented the Midpoint circle algorithm by writing a C++


program. Through the course of the implementation, we observed and learnt various pros and cons of
using the Midpoint circle algorithm. They are as follows:

• Advantages of Midpoint Circle Algorithm :

1. The midpoint method is used for deriving efficient scan-conversion algorithms to draw
geometric curves on raster displays
2. The method is general and is used to transform the nonparametric equation f(x,y) = 0, which
describes the curve, into an algorithms that draws the curve

• Disadvantages of Midpoint Circle Algorithm :

1. This method is very time consuming.


2. The distance between the pixels is not equal so we won't get smooth circle.
EXPERIMENT 4

Aim: Write a program to implement Mid-Point ellipse algorithm.

Theory

Midpoint ellipse algorithm is a method for drawing ellipses in computer graphics. This method is
modified from Bresenham’s algorithm so it is sometimes known as Bresenham's circle algorithm.
The advantage of this modified method is that only addition operations are required in the program
loops. This leads to simple and fast implementation in all processors.

Let us consider one quarter of an ellipse. The curve is divided into two regions. In region I, the slope on
the curve is greater than –1 while in region II less than –1.

Algorithm:

1. Read the radii rx and ry.


2. Initialize starting point as x=0 and y=ry.
3. Make Pixel Function
Void putpixel(int rx,int ry,int x,int y){

Putpixel(rx+x,ry+y);

Putpixel(rx+x,ry-y);

Putpixel(rx-x,ry+y);

Putpixel(rx-x,ry-y);

4. Calculating the initialize value of decision parameter region 1


P1=r^2y-r^2xr^2y+1/4r^2x

5. Initialize dx and dy as
Dx=2r^2y;

Dy=2r^2x;

6. Then,
do{
Putpixel(x,y,rx,ry);

If(p1<0){

x=x+1;

y=y;

dx=dx+2r^2y;

p1=p1+dx+r^2y;}

Else{

x=x+1;

y=y-1;

dx=dx+2r^2y;

dy=dy-2r^2x;

p1=p1+dx-dy+r^2y;}

}while(dx<dy)

7. Calculate the initial value of decision parameter for region 2


P2=(x+1/4)r^2y+r^2x(y-1)^2-r^2xr^2y

8. Then,
do{
Putpixel(x,y,rx,ry);

If(p2>0){

x=x;

y=y-1;

dy=dy-2r^2x;

p2=p2-dy+r^2x;}

Else{

x=x+1;

y=y-1;

dx=dx+2r^2y;

dy=dy-2r^2x;

p2=p2+dx-dy+r^2x;}

}while(y>0)
9. Determine Symmetrical Point in other three quadrants.

Code:

#include<graphics.h>

#include<stdlib.h>
#include<iostream>
#include <math.h>
//#include<conio.h>
using namespace std;
int main()
{
//clrscr();
//int gd = DETECT, gm;
int xc,yc,x,y;float p;
long rx,ry;

cout<<"Enter coordinates of centre : ";


cin>>xc>>yc;
cout<<"Enter x,y radius of ellipse: ";
cin>>rx>>ry;
initwindow(600,600);
//Region 1
p=ry*ry-rx*rx*ry+rx*rx/4;
x=0;y=ry;
while(2.0*ry*ry*x <= 2.0*rx*rx*y)
{
if(p < 0)
{
x++;
p = p+2*ry*ry*x+ry*ry;
}
else
{
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;
}
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
}

//Region 2
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 = p+2*ry*ry*x-2*rx*rx*y+rx*rx;
}
else
{
y--;
p = p-2*rx*rx*y+rx*rx;
}
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
}
getch();
return 0;
}

Output:
Discussion:

In this algorithm we first calculate the p1 then we started loop till dx<dy. In this loop if(p1<0) then
x=x+1,dx=dx+r^2y and p1=p1+dx+r^2y else y=y-1,dy=dy-r^2x and p1=p1+dx-dy+r^2y

Then, for second decision parameter we calculate p2 as p2=(x+1/4)r^2y+r^2x(y-1)^2-r^2xr^2y

We start a loop till (y>0), if (p2>0) then y=y-1,dy=dy-2r^2x and p2=p2-dy+r^2x else, x=x+1,y=y-
1,dx=dx+2r^2y,dy=dy-2r^2x and p2=p2+dx-dy-r^2x.

Findings and Learnings:

• The mid-point method for deriving efficient scan conversion algorithms to draw geometric
curves on raster displays in described. The method is general and is used to transform the non-
parametric equation f(x,y)=0, which describes the curve, into an algorithm that draws the curve.
• Time consumption is high.
• The distance between the pixels is not equals so we won’t get smooth circle.
EXPERIMENT 5

Aim: To write a program to implement circular arc algorithm

Theory: Circular arc algorithm is based on the principle of midpoint circle drawing
algorithm but difference is that here we don't determine symmetry point in all other octants.
In computer graphics, the midpoint circle algorithm is an algorithm used to determine the
points needed for rasterizing a circle. Bresenham's circle algorithm is derived from the midpoint
circle algorithm. The algorithm can be generalized to conic sections.
The objective of the algorithm is to find a path through the pixel grid using pixels which are as
close as possible to solutions of x2 + y2 = r2. At each step, the path is extended by choosing the
adjacent pixel which satisfies x2+ y2 <= r2 but maximizes x2 + y2. Since the candidate pixels are
adjacent, the arithmetic to calculate the latter expression is simplified, requiring only bit shifts
and additions. This algorithm draws only one quadrant, starting from each cardinal direction (0°)
and extends both ways to reach the nearest multiple of 45° (45°). It can determine where to stop
because when y = x, it has reached 45°. The reason for using these angles is shown in the
above picture: As y increases, it does not skip nor repeat any y value until reaching 45°. So
during the while loop, y increments by 1 each iteration, and x decrements by 1 on occasion,
never exceeding 1 in one iteration. This changes at 45° because that is the point where the
tangent is rise=run. Whereas rise>run before and rise<run after.

Algorithm:

Step 1 : Input radius(r) and point(xc,yc).


Step 2 : Make pixel function
void pixel(int xc,int yc,int x,int y) {
putpixel(xc+x,yc+y,7);
putpixel(xc+y,yc+x,7); }
Step 3 : Set x = 0 and y =r .
Step 4 : Calculate pk as pk = 1 — r
pixel(xc,yc,x,y)
Step 5 : while(x<y) {
if(pk<0) { pk=pk+(2*x)+1;}
else { y=y-1; pk=pk+(2*x)-(2*y)+1; }
x=x+1; pixel(xc,yc,x,y); }

Flowchart:

Code:

#include <graphics.h>
#include <iostream>
#include <math.h>
#include <dos.h>
using namespace std;

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


{
putpixel(xc+y,yc-x,9);
putpixel(xc+x,yc-y,9);
delay(20);
}
int main( )
{
int r,xc,yc,x,y;
int i;
cout<<"Enter the value of radius : ";
cin>>r;
cout<<"Enter the value of centre points xc and yc: ";
cin>>xc>>yc;
initwindow(800,800);
x=0, y=r;
float p=1-r;
pixel(xc,yc,x,y);

while(x<y)
{
if(p<0)
p += 2*x + 1;
else
{
y--;
p += 2*x - 2*y + 1;
}
x++;
pixel(xc,yc,x,y);
}
getch();
closegraph();
return 0;
}

Output:
Discussion:
In this algorithm we first calculate the value of pk as 1-r then we start the loop and run it
while(x<y). In this if (pk<0) then x=x+1 and pk = pk + (2*x) + 1; else x=x+1, y=y-1
and pk = pk + (2*x)+ (2*y) +1 and then called function Pixel(xc,yc,x,y) to plot points.
Findings and Learnings :

• The midpoint method for deriving efficient scan - conversion algorithms to draw
geometric curves on raster displays in described. The method is general and is used to
transform the nonparametric equation f(x,y) = 0, which describes the curve, into an
algorithms that draws the curve.
• Time Consumption is high.
• The distance between the pixels is not equals so we wont get smooth circular arc.

You might also like