*65*

# Scan Converting a Straight Line

A straight line may be defined by two endpoints & an equation. In fig the two endpoints are described by (x_{1},y_{1}) and (x_{2},y_{2}). 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 =x_{1}, to x = x_{2}. By scan-converting these calculated x, y values, we represent the line as a sequence of pixels.

## Properties of Good Line Drawing Algorithm:

**1. Line should appear Straight:** We must appropriate the line by choosing addressable points close to it. If we choose well, the line will appear straight, if not, we shall produce crossed lines.

The lines must be generated parallel or at 45° to the x and y-axes. Other lines cause a problem: a line segment through it starts and finishes at addressable points, may happen to pass through no another addressable points in between.

**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.

To maintain constant density, dots should be equally spaced.

**4. Line density should be independent of line length and angle:** This can be done by computing an approximating line-length estimate and to use a line-generation algorithm that keeps line density constant to within the accuracy of this estimate.

**5. Line should be drawn rapidly:** This computation should be performed by special-purpose hardware.

## Algorithm for line Drawing:

- Direct use of line equation
- DDA (Digital Differential Analyzer)
- Bresenham’s Algorithm

## Direct use of line equation:

It is the simplest form of conversion. First of all scan P_{1} and P_{2} points. P_{1} has co-ordinates (x_{1}‘,y_{1}‘) and (x_{2}‘ y_{2}‘ ).

Then m = (y_{2}‘,y_{1}‘)/( x_{2}‘,x_{1}‘) and b =

If value of |m|≤1 for each integer value of x. But do not consider

If value of |m|>1 for each integer value of y. But do not consider

**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.

**Solution:** P_{1} (0,0) P_{7} (6,18)

x_{1}=0

y_{1}=0

x_{2}=6

y_{2}=18

We know equation of line is

y =m x + b

y = 3x + b…………..equation (1)

put value of x from initial point in equation (1), i.e., (0, 0) x =0, y=0

0 = 3 x 0 + b

0 = b ⟹ b=0

put b = 0 in equation (1)

y = 3x + 0

y = 3x

Now calculate intermediate points

Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3

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

So points are P_{1} (0,0)

P_{2} (1,3)

P_{3} (2,6)

P_{4} (3,9)

P_{5} (4,12)

P_{6} (5,15)

P_{7} (6,18)

## Algorithm for drawing line using equation:

**Step1:** Start Algorithm

**Step2:** Declare variables x_{1},x_{2},y_{1},y_{2},dx,dy,m,b,

**Step3:** Enter values of x_{1},x_{2},y_{1},y_{2}.

The (x_{1},y_{1}) are co-ordinates of a starting point of the line.

The (x_{2},y_{2}) are co-ordinates of a ending point of the line.

**Step4:** Calculate dx = x_{2}– x_{1}

**Step5:** Calculate dy = y_{2}-y_{1}

**Step6:** Calculate m =

**Step7:** Calculate b = y_{1}-m* x_{1}

**Step8:** Set (x, y) equal to starting point, i.e., lowest point and x_{end}equal to largest value of x.

If dx < 0

then x = x_{2}

y = y_{2}

x_{end}= x_{1}

If dx > 0

then x = x_{1}

y = y_{1}

x_{end}= x_{2}

**Step9:** Check whether the complete line has been drawn if x=x_{end}, stop

**Step10:** Plot a point at current (x, y) coordinates

**Step11:** Increment value of x, i.e., x = x+1

**Step12:** Compute next value of y from equation y = mx + b

**Step13:** Go to Step9.

## Program to draw a line using LineSlope Method

**OUTPUT: **

Enter Starting and End Points Enter (X1, Y1, X2, Y2) 200 100 300 200