Saturday 22 February 2014

computer graphics Line Drawing Algorithm



Line Drawing is done by calculating Intermediate positions along the line path Between specified paths. DDA LINE Algorithm is a simplest algorithm of drawing a line . In DDA Algorithm line is simply Draws by adding the increment to initial pixel values with respect to x and y direction . After drawing initial pixel whose x and y direction are entered by user or inputted by user Then the Next pixel is drawn by adding Change in x and y direction of new pixel to the initial pixel 

We will add the value of change in x and y axis to initial or previous x and y axis values to get New pixel plotted on screen and This process continues upto and end point that is also inputted by user at start of execution of This Algorithm

The Screen Locations are refrenced by integer values and if these values are in fraction like 10.25 and 21.4 :- (10.25,21.4)  where 10.25 is x coordinate in x-Axis and 21.4 is Y Coordinate in Y-Axis Then These Fractional Values will be converted into Integer values on Screen and interpreted as (10,21). This rounding of two integers cause lines to be displayed with stare steps appearance in Zagged line


To Display a pixel on Screen we use 

                                                          putpixel ( 10 , 11 )

How Line Drawn in DDA Line Algorithm

1. First user input the four values 

* Initial pixel value in x-direction            { let x1 }
* Initial pixel value in y-direction            { let y1 }
* End Point pixel value in x-direction     { let x2 }
* End Point pixel value in y-direction     { let y2 }

2. Now all inputs are  given , Now first input the initial pixels point

putpixel ( x1, y1 )

3. Now we will add the an increment value to initial x and y coordinates and this increment value is added upto End Point


4. The Increment value is calculated by 

dx = x2 - x1   // In x-Direction
dy = y2 - y1   // In y-Direction

Here dx is the how much total distance to cover in x-direction to reach the End coordinate in x-direction
Here dy is the how much distance to cover in y-direction to reach the End coordinate in y-direction

5. Now find the how must distance to cover at one time or at one loop run 

6. This is calculated by dividing the dx and dy by a step 

7. value of step will be value among dx or dy 

If dx > dy then
{
step = dx
}
else
{
step = dy
}

8. After calculating step change in x-direction let be cx will be 

cx =  dx/step 
This will give how much distance to cover in each loop run or at one time how much increment will takes place

Similarly in y-direction 
cy =  dy/step 
This will give how much distance to cover in each loop run or at one time how much increment will takes place

9. Now Start a loop and it will continue upto value in Step variable 

increment x and y as :- 

x= cx + x
y= cy + y
setpixel ( x , y )  // Put or set the New pixel 

 
10. Now at execution of Loop the line will be printed

For Any query feel free and post the question you will get feedback within 2 hours

Share this

5 Responses to "computer graphics Line Drawing Algorithm"

  1. Superb Explanation ..........please try to post more articles i like your simple way of explanation

    ReplyDelete
  2. I am unable to found proper simple explanation to line algorithm
    tutorial on internet .......good work

    ReplyDelete
  3. I blog quite often and I genuinely appreciate your content.
    This great article has really peaked my interest.
    I am going to bookmark your website and keep checking
    for new information about once a week. I subscribed to your Feed too.


    Also visit my web site :: solatube Cape Town

    ReplyDelete
  4. Execellent teaching method

    ReplyDelete
  5. Thanks to all for their great appreciation .......... This will give more courage to write more

    ReplyDelete