Line Drawing algorithm and program
Line Drawing algorithm and program
A straight line may be defined by two endpoints & an equation. In fig the two endpoints are
described by (x1,y1) and (x2,y2). The equation of the line is used to determine the x, y
coordinates of all the points that lie between these two endpoints.
Using the equation of a straight line, y = mx + b where m = & b = the y interrupt, we can
find values of y by incrementing x from x =x1, to x = x2. By scan-converting these calculated
x, y values, we represent the line as a sequence of pixels.
2. Lines should terminate accurately: Unless lines are plotted accurately, they may
terminate at the wrong place.
3. Lines should have constant density: Line density is proportional to the no. of dots
displayed divided by the length of the line.
Example: A line with starting point as (0, 0) and ending point (6, 18) is given. Calculate
value of intermediate points and slope of line.
x1=0
y1=0
x2=6
y2=18
put value of x from initial point in equation (1), i.e., (0, 0) x =0, y=0
0 = b ⟹ b=0
0=3x0+b
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12
Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15
Let x = 6 ⟹ y = 3 x 6 ⟹ y = 18
Step6: Calculate m =
Step8: Set (x, y) equal to starting point, i.e., lowest point and xendequal to largest value of x.
If dx < 0
then x = x2
y = y2
xend= x1
If dx > 0
then x = x1
y = y1
xend= x2
Step9: Check whether the complete line has been drawn if x=xend, stop
Step13: Go to Step9.
OUTPUT: