CAD Practical 1
CAD Practical 1
EXPERIMENT – 1
Aim: To prepare a program for drawing lines and circle using algorithm.
Objectives: Identify the Line Drawing algorithms of computer graphics.
Outcomes: Implement computer graphics programs in C++ using the line drawing algorithms.
Introduction:
DDA (Digital Differential Analyzer) is a line drawing algorithm used in computer graphics to generate
a line segment between two specified endpoints. It is a simple and efficient algorithm that works by
using the incremental difference between the x-coordinates and y-coordinates of the two endpoints to
plot the line.
Input the two endpoints of the line segment, (x1,y1) and (x2,y2).
Calculate the difference between the x-coordinates and y-coordinates of the endpoints as dx
and dy respectively.
Calculate the slope of the line as m = dy/dx.
Set the initial point of the line as (x1,y1).
Loop through the x-coordinates of the line, incrementing by one each time, and calculate the
corresponding y-coordinate using the equation y = y1 + m(x – x1).
Plot the pixel at the calculated (x,y) coordinate.
Repeat steps 5 and 6 until the endpoint (x2,y2) is reached.
DDA algorithm is relatively easy to implement and is computationally efficient, making it suitable for
real-time applications. However, it has some limitations, such as the inability to handle vertical lines
and the need for floating-point arithmetic, which can be slow on some systems. Nonetheless, it remains
a popular choice for generating lines in computer graphics.
In any 2-Dimensional plane, if we connect two points (x0, y0) and (x1, y1), 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 the help of functions like putpixel(x, y, K) in C, where (x,y) is our co-ordinate and K denotes
some color.
Examples:
X = X0;
Y = Y0;
It deals with the rounding off operation and floating point arithmetic so it has high time
complexity.
As it is orientation-dependent, so it has poor endpoint accuracy.
Due to the limited precision in the floating point representation, it produces a cumulative error.
Given the coordinate of two points A(x1, y1) and B(x2, y2). The task is to find all the intermediate
points required for drawing line AB on the computer screen of pixels. Note that every pixel has integer
coordinates.
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 a decision parameter to decide whether to pick Yk + 1 or Yk as the next point. The idea is to
keep track of slope error from the 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 the error.
It is easy to implement.
It is fast and incremental.
It executes fast but less faster than DDA Algorithm.
The points generated by this algorithm are more accurate than DDA Algorithm.
It uses fixed points only.
This algorithm uses the key feature of circle that it is highly symmetric. So, for whole 360 degree of
circle we will divide it in 8-parts each octant of 45 degree. In order to do that we will use Bresenham’s
Circle Algorithm for calculation of the locations of the pixels in the first octant of 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 8 octants of the circle as shown below :
x++;
It is a simple algorithm.
It can be implemented easily
It is totally based on the equation of circle i.e. x2 +y2 =r2
Disadvantages: