Вы находитесь на странице: 1из 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.

Todays Lab:
The objective of todays lab is to implement basic line drawing algorithms like DDA and Bresenhams 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,

Computer Graphics Using OpenGL

But as you can see all the pixels are not illuminated on screen. You cannot see the line on screen like this, but pixel by pixel illumination of points makes a line visible on screen. For that you can calculate the y coordinate for each increment in the value of x or vice versa. There are certain line drawing algorithms that calculate the pixels that should be illuminated on screen to draw a straight line. Although that line will not be straight but it will be visible in the form of straight line on screen. (Refer to Class Discussion for complete algorithm) Question 1: Draw a line from the point p1 (10, 10) to p2 (150,100). Also calculate the time to draw that line. Question 2: Draw a line on screen starting from point p3 (10, 15) having slope 0.75. You can draw it till x=150.

Bresenhams Line Drawing Algorithm:


1. Input two line end points and store the first end point in (x0, y0). 2. Set the color for frame buffer position (x0, y0) that is, plot the first point. 3. Calculate the constants x, y, 2y - 2x, and obtain the starting value for the decision parameter as p0 = 2y x

4. At each xk along the line, starting it k=0, perform the following test. If p k<0, the next point to plot is (xk+1,yk) and pk+1 = pk +2y Otherwise, the next point to plot is (xk+1, yk+1) and pk+1 = pk + 2y - 2x 5. Perform step 4 x-1 times.

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

Вам также может понравиться