Department of Master of Computer Application: M.C.A. First Year (II Semester)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Tulsiramji Gaikwad-Patil College of Engineering & Technology

Wardha Road, Nagpur-441 108

Department of Master of Computer Application


M.C.A. First Year (II Semester)
1T4: Computer Graphics (Unit II)
Topic: Line Drawing Algorithm
By
Mr. Roshan Chandekar
Assistant Professor
Master in Computer Application
TGPCET, Nagpur.
1
Line drawing algorithms
The Cartesian slope-intercept equation for a straight line is

with m representing the slope of the line and b as they intercept. Given that the two
endpoints of a line segment are specified at positions (x1, y1,) and (x2, y2), as shown in
Fig., we can determine values for the slope m and y intercept b with the following
calculations:

Figure : Line path between endpoint positions (x1, y1) and


(x2, y2)

2
Department of Master of Computer Application
Line drawing algorithms
•Algorithms for displaying straight lines are based on the line equation and the calculations
given in Eqs.
•For any given s interval dx along a line, we can compute the corresponding y interval dy
from above equation.

•Similarly, we can obtain the x interval dx corresponding to a specified dy as

3
Department of Master of Computer Application
Line drawing algorithms
•These equations form the basis for determining deflection voltages in analog
devices.
•For lines with slope magnitudes |m | < 1, dx can be set proportional to a small
horizontal deflection voltage and the corresponding vertical deflection is then set
proportional to dy as calculated from Eq. For lines whose slopes have magnitudes
|m | < 1, dy can be set proportional to a small vertical deflection voltage with the
corresponding horizontal deflection voltage set proportional to dx, calculated
from Eq. For lines with m = 1, dx = dy and the horizontal and vertical deflections
voltages are equal. In each case, a smooth line with slope m is generated between
the specified endpoints.
•On raster systems, lines are plotted with pixels, and step sizes in the horizontal
and vertical directions are constrained by pixel separations. That is, we must
"sample" a line at discrete positions and determine the nearest pixel to the line at
each sampled position. Ths scan conversion process for straight lines is illustrated
in Fig. , for a near horizontal line with discrete sample positions along the x axis.
4
Department of Master of Computer Application
Digital Differential Analyzer (DDA)

We know that the slope of a straight line is given as

----------- (1)

The above differential equation ca be used to obtain a rasterized straight line, For any given x interval
dx along a line, we can compute the corresponding y interval dy for the equation (1) as

---------------(2)
Similarly, we can obtain the x interval dx corresponding to a specific dy as

---------------------(3)

5
Department of Master of Computer Application
Digital Differential Analyzer (DDA)
Once the intervals are known the values for next a and next y on the straight
line can be obtained as follows

--------------- (4) and

--------------- (5)

The equation 4 and 5 represents a recursion for successive values of x and y along the required line.
Such a way of rasterizing a line is called a Digital differential analyzer (DDA).

6
Department of Master of Computer Application
Digital Differential Analyzer (DDA)
Let us see the DDA algorithm 5. x = x1 + 0.5 * (sign (dx)
1. Read the line end points (x1, y1) and (x2,y2) such that
they are not equal. y = y1 + 0.5 * sign (dy)
2. dx= | x2 – x1| and dy = | y2 – y1| [ Here, sign function makes the algorithm work in all quadrant.
3. if (dx >= dy) then It returns -1, 0, 1 depending on whether its arguments is <0,
=0,>0 respectively. The factor 0.5 makes it possible to round
length = dx the values in the integer function rather than truncating them]
else 6. i =1
length = dy
end if While (i <= length)
4. dx = (x2 – x1) / length {
Plot (Integer (x), integer (y))
dy = (y2 – y1) / length X = x + dx
Y = y + dy
[ This makes a either dx or dy equal to 1 because length is I=i+1
either | x2 – x1| or | y2 – y1|. Therefore, the incremental }
value for either x or y is one] 7. Stop

7
Department of Master of Computer Application

You might also like