Graphics Lab Report - 1
Graphics Lab Report - 1
Institute of Engineering
Thapathali Campus, Thapathali
Submitted by:
Name: Atul Shreewastav
Roll No.: THA077BCT013
Submitted to:
Department of Electronics and Computer Engineering
Date: 13th July, 2023
DDA (Digital Differential Analyzer)
Title: To use DDA algorithm to draw a line between given points.
Theory:
In any 2-Dimensional plane if we connect two points (x1, y1) and (x2, y2), we get a line segment.
But in the case of computer graphics, we cannot directly join any two coordinate points, for that
we should calculate intermediate points’ coordinates and put a pixel for each intermediate point,
of the desired color with help of functions like putpixel(x, y, K) in C, where (x,y) is our co-ordinate
and K denotes some color.
For using graphics functions, our system output screen is treated as a coordinate system where the
coordinate of the top-left corner is (0, 0) and as we move down our y-ordinate increases and as we
move right our x-ordinate increases for any point (x, y).
Now, for generating any line segment we need intermediate points and for calculating them we
can use a basic algorithm called DDA (Digital Differential Analyzer) line generating algorithm.
DDA Algorithm:
Step 1: Start
Step 2: Take the coordinates of initial point (x1,y1) and the final point (x2,y2).
Step 3: Find the difference between the points by using the formula:
dx = x2-x1;
dy = y2-y1;
Step 4: Compare the absolute value of the differences to get the number of steps.
If abs(dx) > abs(dy)
steps = dx;
Else
steps = dy;
Step 5: Calculate the increment of x1 and y1 in each step.
dx = dx/steps;
dy = dy/steps;
Step 6: Put pixel in (x1,y1).
Step 7: Repeat step 5 while adding dx to x and dy to y until steps reaches zero.
while (steps != 0)
putpixel(x1,y1,K);
x1 = x1+dx;
y1 = y1+dy;
end while
Step 8: Stop
Code:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
int main(){
// Declaring Variable
int x1, y1, x2, y2, dx, dy, steps;
// initializing graphics
initgraph(&gd, &gm, NULL);
setbkcolor(WHITE); // set bg color to white
cleardevice();
Conclusion:
Thus, as shown in the program above, we can draw a line by drawing individual pixels with the
Digital Differential Analyzer (DDA) algorithm using the various functions available in the
graphics.h header file.
BLA (Bresenham’s Line Algorithm)
Title: To use BLA algorithm to draw a line between given points.
Theory:
The idea of Bresenham’s algorithm is to avoid floating point multiplication and addition to
compute mx + c, and then computing round value of (mx + c) in every step. In Bresenham’s
algorithm, we move across the x-axis in unit intervals.
1. We always increase x by 1, and we choose about next y, whether we need to go to y+1 or
remain on y. In other words, from any position (Xk, Yk) we need to choose between (Xk +
1, Yk) and (Xk + 1, Yk + 1).
2. We would like to pick the y value (among Yk + 1 and Yk) corresponding to a point that is
closer to the original line.
We need to a decision parameter to decide whether to pick Yk + 1 or Yk as next point. The idea is
to keep track of slope error from previous increment to y. If the slope 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.
How to avoid floating point arithmetic
The above algorithm still includes floating point arithmetic. To avoid floating point arithmetic,
consider the value below value m.
m = (y2 – y1)/(x2 – x1)
We multiply both sides by (x2 – x1). We also change slope error to slope error * (x2 – x1). To
avoid comparison with 0.5, we further change it to slope error * (x2 – x1) * 2. Also, it is generally
preferred to compare with 0 than 1.
Code:
//BLA Including preprocessing directive
#include <stdio.h>
#include <graphics.h>
#include <math.h>
// Main function
int main()
{
// Declaring variables
float x1,x2,y1,y2,m,dx,dy,p;
// Calculating dx and dy
dx=x2-x1;
dy=y2-y1;
delay(500000);
closegraph();
return 0;
}
Output:
Conclusion:
Thus, as shown in the program above, we can draw a line by drawing individual pixels with the
Bresenham’s Line Algorithm (BLA) using the various functions available in the graphics.h
header file.
Mid-point Circle Drawing Algorithm
Title: To use mid-point circle drawing algorithm to draw a circle.
Theory:
The mid-point circle drawing algorithm is an algorithm used to determine the points needed for
rasterizing a circle.
We use the mid-point algorithm to calculate all the perimeter points of the circle in the first octant
and then print them along with their mirror points in the other octants. This will work because a
circle is symmetric about its center.
The algorithm is very similar to the Mid-Point Line Generation Algorithm. Here, only the
boundary condition is different.
For any given pixel (x, y), the next pixel to be plotted is either (x, y+1) or (x-1, y+1). This can be
decided by following the steps below.
1. Find the mid-point p of the two possible pixels i.e. (x-0.5, y+1)
2. If p lies inside or on the circle perimeter, we plot the pixel (x, y+1), otherwise if it’s
outside we plot the pixel (x-1, y+1)
Boundary Condition : Whether the mid-point lies inside or outside the circle can be decided by
using the formula given below.
Given a circle centered at (0,0) and radius r and a point p(x,y):
F(p) = x2 + y2 – r2
if F(p)<0, the point is inside the circle
F(p)=0, the point is on the perimeter
F(p)>0, the point is outside the circle
We denote F(p) with P. The value of P is calculated at the mid-point of the two contending
pixels i.e. (x-0.5, y+1). Each pixel is described with a subscript k.
The first point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated as
follows:
P1 = (r – 0.5)2 + (0+1)2 – r2
= 1.25 – r
= 1 -r (When rounded off).
Code:
//Circle
#include<stdio.h>
#include<graphics.h>
#include<math.h>
//Main Function
int main(){
//Declaring variables
float x1, y1, x, y, radius, p;
x=0;
y=radius;
p=1-radius;
}
else if(p>=0){
x++;
y--;
putpixel(x+x1,y+y1,BLACK);
putpixel(y+x1,x+y1,BLACK);
putpixel(-y+x1,x+y1,BLACK);
putpixel(-x+x1,y+y1,BLACK);
putpixel(-x+x1,-y+y1,BLACK);
putpixel(-y+x1,-x+y1,BLACK);
putpixel(y+x1,-x+y1,BLACK);
putpixel(x+x1,-y+y1,BLACK);
p+=2*x-2*y+1;
}
}
Conclusion:
Thus, as shown in the program above, we can draw a circle by drawing individual pixels with the
mid-point circle drawing algorithm using the various functions available in the graphics.h header
file.