You are on page 1of 3

Computer Graphics Using OpenGL

Line Drawing Algorithms
Progress so far…
OpenGL is an open source Graphics library that is widely used in creation of Graphic Applications. We have so far covered basic implementation of a graphic applications, two and three dimensional object and object transformations.

Today’s Lab:
The objective of today’s lab is to implement basic line drawing algorithms like DDA and Bresenham’s Line Drawing Algorithm.

Submission Requirements:
    Submit your codes, only .cpp files, through LMS. Deadline: 5th March, 2012. Copy cases will be marked zero without any further clarification or discussion. Comments in your code can get you extra credit. Do add proper comments and always be prepared for viva.

Digital Differential Analyzer:
A line is an infinite number of dots between two different points. The basic problem is how you can find those points. Therefore, you use the basic equation of line.

Here you can find the coordinates of y for given x provided that slope of line is m and y intercept is b. On the computer screen, you can only plot points in the form of pixels. Pixels are present on screen in the form of rectangular grid. Actual line on screen should be displayed like this,

and obtain the starting value for the decision parameter as p0 = 2Δy – Δx 4. Perform step 4 Δx-1 times. If p k<0. Input two line end points and store the first end point in (x0. At each xk along the line. You cannot see the line on screen like this. 10) to p2 (150. 15) having slope 0. (Refer to Class Discussion for complete algorithm) Question 1: Draw a line from the point p1 (10. starting it k=0.100). but pixel by pixel illumination of points makes a line visible on screen. the next point to plot is (xk+1.2Δx. Question 2: Draw a line on screen starting from point p3 (10. Also calculate the time to draw that line.2Δx 5. . plot the first point. Δy. Calculate the constants Δx. y0). yk+1) and pk+1 = pk + 2Δy . Set the color for frame buffer position (x0.Computer Graphics Using OpenGL But as you can see all the pixels are not illuminated on screen. Although that line will not be straight but it will be visible in the form of straight line on screen. perform the following test. There are certain line drawing algorithms that calculate the pixels that should be illuminated on screen to draw a straight line. y0) that is. 2. 3.yk) and pk+1 = pk +2Δy Otherwise.75. 2Δy . the next point to plot is (xk+1. Bresenham’s Line Drawing Algorithm: 1. You can draw it till x=150. For that you can calculate the y coordinate for each increment in the value of x or vice versa.

Question 5: Draw a rectangle of height 100 and width 150 using both line drawing algorithms. Question 4: Draw a line on screen starting from point p3 (10. 10) to p2 (150.100).Computer Graphics Using OpenGL Question 3: Draw a line from the point p1 (10. You can draw it till x=150. Also calculate the time to draw that line.75. Also calculate the time elapsed to draw both rectangles. Evaluate both algorithms on the basis of your results. 15) having slope 0. . NOTE: Submit your codes with a report of your comments about both algorithms.