0% found this document useful (0 votes)
14 views

Graphics Lab Report - 1

The document describes implementations of three algorithms: DDA line drawing algorithm, Bresenham's line algorithm, and midpoint circle drawing algorithm. Code is provided to draw lines and circles using these algorithms. The algorithms are explained theoretically and outputs of the code are shown.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Graphics Lab Report - 1

The document describes implementations of three algorithms: DDA line drawing algorithm, Bresenham's line algorithm, and midpoint circle drawing algorithm. Code is provided to draw lines and circles using these algorithms. The algorithms are explained theoretically and outputs of the code are shown.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Tribhuvan University

Institute of Engineering
Thapathali Campus, Thapathali

Subject: Computer Graphics


LAB #1
Implementation of Line Drawing Algorithms and
Circle Drawing Algorithm

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;

// Asking for initial points


printf("Enter initial points: ");
scanf("%d %d",&x1, &y1);

// Asking for final point


printf("Enter the final point: ");
scanf("%d %d",&x2,&y2);

// Calculating the differences between initial and final points


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

// determining the number of steps


if (dx>=dy){
steps = dx;
}
else{
steps = dy;
}

// determining the increment per step


dy /= steps;
dx /= steps;

// initializing graphics variables


int gd = DETECT, gm;

// initializing graphics
initgraph(&gd, &gm, NULL);
setbkcolor(WHITE); // set bg color to white
cleardevice();

// drawing individual pixels in a loop


for (int i=0; i<steps; i++){
putpixel(x1, y1, BLACK);
x1 += dx;
y1 += dy;
}

// concluding the program


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
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;

// Asking for starting and ending points


printf("Enter the co-ordinates of starting point: ");
scanf("%f %f",&x1,&y1);

printf("Enter the co-ordinates of ending point: ");


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

// Calculating dx and dy
dx=x2-x1;
dy=y2-y1;

// Calculating slope of the line


m=dy/dx;

// Initiating graph function


int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
setbkcolor(WHITE);
cleardevice();

// Putting the first pixel


putpixel(x1,y1,BLACK);

// Checking whether the slope is positive or negative


if (m>0)
{
// Checking if the slope is greater or less than 1
if (fabs(m)<=1)
{
// Calculating value of p0
p=2*dy-dx;

while (x1!=x2 && y2!=y1)


{
// Comparing p with 0
if (p<0)
{
x1+=1;
y1=y1;
p+=2*dy;
}
else if (p>=0)
{
x1+=1;
y1+=1;
p+=(2*dy-2*dx);
}
putpixel(x1,y1,BLACK);
}
}
else if (fabs(m)>1)
{
// Calculating value of p0
p=2*dx-dy;

while (x1!=x2 && y2!=y1)


{
// Comparing p with 0
if (p<0)
{
x1=x1;
y1+=1;
p+=2*dx;
}
else if (p>=0)
{
x1+=1;
y1+=1;
p+=(2*dx-2*dy);
}
putpixel(x1,y1,BLACK);
}
}
}
else if (m<0)
{
// Checking if the slope is greater or less than 1
if (fabs(m)<=1)
{
// Calculating value of p0
p=2*dy-dx;

while (x1!=x2 && y2!=y1)


{
// Comparing p with 0
if (p<0)
{
x1-=1;
y1=y1;
p+=2*dy;
}
else if (p>=0)
{
x1-=1;
y1+=1;
p+=(2*dy-2*dx);
}
putpixel(x1,y1,BLACK);
}
}
else if (fabs(m)>1)
{
// Calculating value of p0
p=2*dx-dy;

while (x1!=x2 && y2!=y1)


{
// Comparing p with 0
if (p<0)
{
x1=x1;
y1+=1;
p+=2*dx;
}
else if (p>=0)
{
x1-=1;
y1+=1;
p+=(2*dx-2*dy);
}
putpixel(x1,y1,BLACK);
}
}
}

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.

Pk = (Xk — 0.5)2 + (yk + 1)2 – r2


Now,
xk+1 = xk or xk-1 , yk+1= yk +1
∴ Pk+1 = (xk+1 – 0.5)2 + (yk+1 +1)2 – r2
= (xk+1 – 0.5)2 + [(yk +1) + 1]2 – r2
= (xk+1 – 0.5)2 + (yk +1)2 + 2(yk + 1) + 1 – r2
= (xk+1 – 0.5)2 + [ – (xk – 0.5)2 +(xk – 0.5)2 ] + (yk + 1)2 – r2 + (yk + 1) + 1
= Pk + (xk+1 – 0.5)2 – (xk – 0.5)2 + 2(yk + 1) + 1
= Pk + (x2k+1 – x2k)2 + (xk+1 – xk)2 + 2(yk + 1) + 1
= Pk + 2(yk +1) + 1, when Pk <=0 i.e. the midpoint is inside the circle (xk+1 = xk)
= Pk + 2(yk +1) – 2(xk – 1) + 1, when Pk>0 i.e. the mid-point is outside the
circle (xk+1 = xk-1)

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;

//Asking for input from user


printf("Enter the coordinate of center of circle: ");
scanf("%f%f", &x1, &y1);

printf("Enter the radius of circle: ");


scanf("%f", &radius);

x=0;
y=radius;
p=1-radius;

//Initializing graph function


int gd = DETECT,gm;
initgraph(&gd, &gm, NULL);
setbkcolor(WHITE);
cleardevice();

//Putting the first pixel


putpixel(x+x1,y+y1,BLACK);

//Checking whether the slope is positive or negative


while(x<=y){
//Checking if the slope is greater or less than 1
if(p<0){
x++;
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+1;

}
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;
}
}

//Concluding the program


delay(500000);
closegraph();
return 0;
}
Output:

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.

You might also like