Lec6 RasterAlgo Line Drawing Algorithm
Lec6 RasterAlgo Line Drawing Algorithm
2D Graphics Pipeline
Clipping
Object window to
Object Applying
subset viewport
World Coordinates world window
mapping
Object
Display Rasterization
Screen coordinates
Four Major Tasks
• Four major task for rendering geometric objects
– modeling
– geometric processing Modeling
• transformations
• clipping Geometry
• shading
• hidden-surface removal
Rasterize
• projection
– Rasterization (Scan Conversion)
Display
– display
Computer Graphics 7
Scan Conversion
• Rasterization or Scan Conversion is defined as the
process of representing continuous graphic object as a
collection of discrete pixels.
– Various graphic objects are
• Point
• Line
• Rectangle, Square
• Circle, Ellipse
• Sector, Arc
• Polygons……
– Hardware, Software, Firmware Implementation
Z
Y
Illuminate pixels as close to the true path
as possible, consider bi-level display only
Pixels are either lit or not lit
19
What is an ideal line?
Must appear straight and continuous
Only possible axis-aligned and 45o lines
Must interpolate both defining end
points
Must have uniform density and intensity
Consistent within a line and over all lines
What about antialiasing?
Must be efficient, drawn quickly
Lots of them are required!!!
20
Line Drawing Algorithm
Programmer specifies (x,y) values of end pixels
Need algorithm to figure out which intermediate
pixels are on line path
Pixel (x,y) values constrained to integer values
Actual computed intermediate line values may be
floats
Rounding may be required. E.g. computed point
Based on slope-intercept algorithm from
algebra:
y = mx + b ( (also Ax + By + C = 0)
Simple approach:
increment x, solve for y
Floating point arithmetic required
22
The Problem of Scan Conversion
A line segment in a scene is defined by
the coordinate positions of the line end-
points y
(7, 5)
(2, 2)
x
The Problem of Scan Conversion
But what happens when we try to draw
this on a pixel based display?
Slope-intercept line
yend equation:
y =m ⋅ x +b
y0 where:
y end − y 0
m=
x0 xend
x
x end − x 0
b= y 0 − m⋅ x 0
For any given x intervals dx along a line,
we can compute the corresponding y if
interval dY from eqn
dy=m dx
Similarly, we can obtain the x interval dx
corresponding to a specified dy as
dx = dy/m
A Very Simple Solution (cont…)
7 7
6 6
5 5
4 4
3 3
2 2
1 1
0 0
Increment of x or y depends on slope
Sharp Slope Case:
If m>1, increment y and find x
dy can be set propos ional to a small vertical deflection
voltage and the corresponding horizontal deflection
voltage set propos ional to dx as calculated from eqn.
Slant Slope Case:
If m<1, increment x and find y
dx can be set proposional to a small horizontal deflection
voltage and the corresponding vertical deflection voltage
set propos ional to dy as calculated from eqn
m = -1 m=1
m = -1/2 m = 1/2
m = -1/3 m = 1/3
m=0 m=0
A Very Simple Solution
We could simply work out the
corresponding y coordinate for each unit x
coordinate
Let’s consider
y the following example:
(7, 5)
5
2
(2, 2)
x
2 7
A Very Simple Solution (cont…)
y
(7, 5)
LINE EQUATION : y= mx+b
5
First work out m and b:
5−2 3
m= =
2
7−2 5
(2, 2)
3 4
b=2 − ∗2=
2 3 4 5 6 7
x 5 5
Now for each x value work out the y value:
3 4 3 3 4 1
y ( 3 ) = ⋅3 + =2 y ( 4 ) = ⋅ 4+ =3
5 5 5 5 5 5
3 4 4 3 4 2
y ( 5 ) = ⋅5+ =3 y ( 6 )= ⋅ 6+ =4
5 5 5 5 5 5
A Very Simple Solution (cont…)
Now just round off the results and turn on
these pixels to draw our line
3
7 y ( 3 ) =2 ≈ 3
6 5
5 1
y ( 4 ) =3 ≈ 3
4 5
3
4
2 y ( 5 ) =3 ≈ 4
1 5
0 2
y ( 6 )=4 ≈ 4
0 1 2 3 4 5 6 7 8
5
A Very Simple Solution (cont…)
However, this approach is just way too
slow
In particular look out for:
The equation y = mx + b requires the
multiplication of m by x
Rounding off the resulting y coordinates
We need a faster solution
Issues with Line Drawing
Line drawing is such a fundamental algorithm that it must be
done fast
Use of floating point calculations does not facilitate speed
Furthermore, lines must be drawn without gaps
Gaps look bad
If you try to fill a polygon made of lines with gaps the fill will
leak out into other portions of the display
Eliminating gaps through direct implementation of any of
the standard line equations is difficult
Finally, we don’t want to draw individual points more than once
That’s wasting valuable time
DDA Algorithm
DDA = Digital Differential Analyser
Finite differences
Start with starting and ending coordinates of the line:
(x0, y0) and (x1, y1)
Color first pixel (round to nearest integer)
Suppose x1-x0 > y1-y0 (gentle slope)
There will be x1-x0 steps (# pixels to be colored)
Set x=x0, y=y0
At each step,
increment x by (x1-x0)/numsteps, and
increment y by (y1-y0)/numsteps
For each step, round off x and y to nearest integer, and
color pixel
DDA Pseudo-code
// assume that slope is gentle
DDA(float x0, float x1, float y0, float y1) {
float x, y; Q: For each step, how many floating
int dx = x1-x0, dy = y1-y0; point operations are there?
int numsteps = max(abs(dx),abs(dy));
A: 2
float xinc, yinc;
Q: For each step, how many integer
xinc = dx / numsteps; operations are there?
yinc = dy / numsteps; A: 2
x = x0;
y = y0;
ColorPixel(Round(x),Round(y));
}
DDA Example
dx = 12 – 2 = 10, dy = 8-3 =5
numsteps = max(abs(dx),abs(dy)=max(10,5)=10
Suppose we want to xinc = dx/numsteps = 10/10 = 1.0
yinc = dy/numsteps = 5/10 = 0.5
draw a line starting at t x y R(x) R(y)
pixel (2,3) and ending 0 2 3 2 3
at pixel (12,8). 1 3 3.5 3 4
What are the values 2 4 4 4 4
of the variables x and 3 5 4.5 5 5
y at each timestep? 4 6 5 6 5
What are the pixels 5 7 5.5 7 6
colored, according to 6 8 6 8 6
P(x) P(y) 9
2 3
8
3 4
4 4 7
5 5
6
6 5
7 6 5
8 6
4
9 7 3
10 7
2
11 8
12 8 1
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13
DDA Algorithm (continued)
Y_inc
X_inc
dupper
dlower
Bresenham’s Line Algorithm
What it comes down to is computing how close the
midpoint (between the two grid points) is to the
actual line
(
F ( m )=d m=F x p +1 , y p +
1
2 )
(
F ( m )=d m = A ⋅ ( x p +1 ) + B ⋅ y p +
1
2
+C)
if (dm > 0) choose the upper point (pixel), otherwise
if (dm <0), i.e, negative then choose the lower point (pixel)
Decision parameter dm is referred in book as Pk
w
Bresenham’s Line-Drawing
Algorithm
What Pixels to turn on or off?
(x1,y1)
Consider pixel midpoint M(Mx, My)
M = (x0 + 1, Y0 + ½)
(12,8). 0 0 2 3
What are the values of 1 -10 3 4
p0, dx and dy? 2 0 4 4
What are the values of 3 -10 5 5
the variable p at each 4 0 6 5
timestep? 5 -10 7 6
What are the pixels 6 0 8 6
colored, according to 7 -10 9 7
Bresenham’s algorithm?
8 0 10 7
9 -10 11 8
10 0 12 8
10
P(x) P(y) 9
2 3
8
3 4
4 4 7
5 5
6
6 5
7 6 5
8 6
4
9 7 3
10 7
2
11 8
12 8 1
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Summary of mid-point algorithm
Choose between 2 pixels at each step
based upon the sign of a decision
variable.
Update the decision variable based
upon which pixel is chosen.
Start point is simply first endpoint (x1,y1).
Need to calculate the initial value for Pk
64
Summary of line drawing so far.
Explicit form of line
Inefficient, difficult to control.
Parametric form of line.
Express line in terms of parameter t
DDA algorithm
Implicit form of line
Only need to test for ‘side’ of line.
Bresenham algorithm.
Can also draw circles.
65
Problems with Bresenham algorithm
Pixels are drawn as a single line
unequal line intensity with change in angle.
Pixel density = 2.n pixels/mm
Can draw lines in darker
colours according to line
direction.
- Better solution : antialiasing !
- (eg. Gupta-Sproull algorithm)
Pixel density = n pixels/mm
66