Here is the Digital Differential Analyzer method for obtaining a rasterized straight line:

1. Read end points (x1, y1 ) and (x2, y2).

If x1= x2 and y1=y2.

Plot Point.

Exit.

2. Δx= |x2-x1| and Δy= |y2-y1|

3. If (Δx≥ Δy)

length = Δx

else

length = Δy

end if

4. Δx= (x2-x1)/length

Δy= (y2-y1)/length

5. x= x1 + 0.5 *Sign(Δx)

y= y1 + 0.5 *Sign(Δx)

[Sign function lets the algorithm to work in all quadrants. Factor 0.5 is used to round the values in Integer function.]

6. i=1

While (i≤ length)

{

Plot (Integer (x), Integer (y))

x=x + Δx

y=y + Δy

i=i+1

7. Stop.

Advantages of Digital Differential Analyzer Algorithm:

- It is a fast method for calculating pixel positions.
- It is a simple algorithm.

Disadvantages of Digital Differential Analyzer Algorithm:

- End point accuracy is poor.
- Floating point calculation is time consuming.

William Davis says

thank you I needed this formula for a vb.net program that plots the window on the screen.

shraddha says

why 0.5 is used to round integers