Computer Graphics: Graphics Output Primitives
Line Drawing Algorithms
By: A. H. Abdul Hafez
March 5, 2016 1 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU
Outlines
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 2
1. Basic concept of lines in OpenGL
2. Line Equation
3. DDA Algorithm
4. DDA Algorithm implementation
5. End
Basic concept of lines in OpenGL
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 3
To display the line on a raster monitor, the graphics system must first project the
endpoints to integer screen coordinates
Next, it determines the nearest pixel positions along the line path between the
two endpoints.
Then the line color is loaded into the frame buffer at the corresponding pixel
coordinates.
Basic concept of lines in OpenGL
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 4
Reading from the frame buffer, the video controller plots the screen pixels.
This process digitizes the line into a set of discrete integer positions that, in
general, only approximates the actual line path.
This rounding of coordinate values to integers causes all but horizontal and
vertical limes to be displayed with a stair-step appearance ("the jaggies").
Line Equation
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 5
The Cartesian slope-intercept equation for a straight line is
we can determine values for the slope m and y intercept b with the following calculations:
For any given x interval δx along a line, we can compute the corresponding y interval δy
On raster systems, lines are plotted with pixels. That is, we must "sample" a line at discrete positions and determine the nearest pixel to the line at each sampled position.
Discrete sample positions along the x axis is shown.
DDA Algorithm
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 6
A line is sampled at unit intervals in one coordinate and the corresponding integer values nearest the line path are determined for the other coordinate.
We consider first a line with positive slope; start is at left:
1. If the slope is less than or equal to 1, we sample at unit x intervals (δx = 1) and compute successive y values as
2. For lines with a positive slope greater than 1, we reverse the roles of x and y. That is, we sample at unity intervals (δy = 1) and calculate consecutive x values as
In this case, each computed x value is rounded to the nearest pixel position along the current y scan line.
DDA Algorithm
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 7
We consider second a line with positive slope; start is at right:
If the slope is less than or equal to 1, we sample at unit x intervals (δx = -1)
and compute successive y values as
For lines with a slope greater than 1, we sample at unity intervals (δy = -1)
and calculate consecutive x values as
If the slop is negative, we consider similarly to the two above
cases. See next slide for generalization.
DDA Algorithm. General case of slope
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 8
If the absolute value of the-slope-is less than 1 and the starting
endpoint is at the left, we set δx = 1 and calculate y values with
When the starting endpoint is at the right (for the same slope), we set δx = -1
and obtain y positions using
If the absolute value greater than 1 and the starting point at the left,
we use δy = -1 and
When the starting point is at the right, we use δy = 1 and
DDA Algorithm implementation
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 9
This algorithm is summarized in the following procedure, which accepts as input two integer screen positions for the endpoints of a line segment.
Horizontal and vertical differences between the endpoint positions are assigned to parameters dx and dy. The difference with the greater magnitude determines the value of parameter steps.
Starting with pixel position (x0, y0), we determine the offset needed at each step to generate the next pixel position along the line path.
We loop through this process steps times.
1. If the magnitude of dx is greater than the magnitude of dy and x0 is less than xEnd, the values for the increments in the x and y directions are 1 and m=dy/dx, respectively.
2. If the greater change is in the x direction, but x0 is greater than xEnd, then the decrements -1 and –m=dy/dx are used to generate each new point on the line.
3. Otherwise, we use a unit increment (or decrement) in the y direction and an x increment (or decrement) of 1/m=dx/dy.
Bresenham's Line Algorithm
March 6, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 11
Motivation:
A section of the screen showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope 0 < m < 1.
Assuming we have determined that
the pixel at (xk, yk) is to be displayed, we next need to decide which pixel to plot in column xk+l = xk + 1. Our choices are the pixels at positions (xk + 1, yk) and (xk + 1, yk+1).
Bresenham's Line Algorithm
March 6, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 12
To determine which of the two
pixels is closest to the line path, we
can set up an efficient test that is
based on the difference between the
two pixel separations, and defining
the decision parameter as
Vertical distances between pixel
positions and the line y coordinate
at sampling position xk + 1.
Bresenham's Line Algorithm
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 14
EXAMPLE 3-1 from the text
Bresenham's Line Algorithm
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 15
EXAMPLE 3-1 cont.
Bresenham's Line Algorithm
March 5, 2016 CG, by Dr. A.H. Abdul Hafez, CE Dept. HKU 16
Pixel positions along the line path between endpoints (20, 10) and
(30, 18), plotted with Bresenham’s line algorithm.