5.1 si23_03 si23 introduction to computer graphics lecture 5 – drawing a line
TRANSCRIPT
5.1Si23_03
SI23Introduction to Computer
Graphics
SI23Introduction to Computer
Graphics
Lecture 5 – Drawing A Line
5.2Si23_03
Course OutlineCourse Outline
ImageDisplay
URLGIMP
colour
Vector graphics– Line drawing– Area filling
Graphical interaction
SVGViewer
2D vectorgraphics
URLlines.,areas
graphicsalgorithms
interaction
5.3Si23_03
Line DrawingLine Drawing
Line drawing is a fundamental operation in computer graphics
Demanding applications require very fast drawing speeds - say, 1 million lines per second
Hence the algorithm for converting a line to a set of pixels will be implemented in hardware - and efficiency is vital
– Want to use integer arithmetic– Want to use additions rather than
multiplications
5.4Si23_03
Line Drawing - The Problem
Line Drawing - The Problem
To draw a line from one point to another on a display
- which pixels should be illuminated?
Suppose pt1 = (0,1)and pt2 = (5,4)
0 1 2 3 4 5 6
0
12345
Where to draw the line?
5.5Si23_03
Line Drawing - By HandLine Drawing - By Hand
In a simple example, one can do by eye
0 1 2 3 4 5 6
0
12345
... but we would likean algorithm!
5.6Si23_03
Equation of a LineEquation of a Line
Line equation is:
y = m x + c
If line joins (x1,y1) to (x2,y2), then
m = (y2-y1)/(x2-x1) and c = y1 - m x1
Suppose pt1 = (0,1)and pt2 = (5,4)
0 1 2 3 4 5 6
0
12345
Then m = (4-1)/(5-0) ie 0.6and c = 1
Thus: y = 0.6 x + 1
5.7Si23_03
Drawing a Line From Its Equation
Drawing a Line From Its Equation
We can use the equation to draw the pixels
y = 0.6 x + 1
x = 0 -> y =1 y =1x = 1 -> y = 1.6 y = 2x = 2 -> y = 2.2 y = 2x = 3 -> y = 2.8 y = 3x = 4 -> y = 3.4 y = 3
Calculate y for x = 0,1,2,3,4,5and round to nearest integer
0 1 2 3 4 5 6
0
12345
5.8Si23_03
The Line DrawnThe Line Drawn
y = 0.6 x + 1
x = 0 -> y =1 y =1x = 1 -> y = 1.6 y = 2x = 2 -> y = 2.2 y = 2x = 3 -> y = 2.8 y = 3x = 4 -> y = 3.4 y = 3
Calculate y for x = 0,1,2,3,4,5and round to nearest integer
0 1 2 3 4 5 6
0
12345
This gives us (after 1 multiplication, 1 addition,and 1 rounding operation per step):
How do we make more efficient?
5.9Si23_03
DDA AlgorithmDDA Algorithm
We can do this more efficiently by simply incrementing y at each stage
y* = y + q where q = (y2-y1)/(x2-x1)
0 1 2 3 4 5 6
0
12345 y = 1.0 y = 1
y = 1.0 + 0.6 = 1.6 y = 2y = 1.6 + 0.6 = 2.2 y = 2y = 2.2 + 0.6 = 2.8 y = 3y = 2.8 + 0.6 = 3.4 y = 3
Here q = 0.6
One addition, one rounding
5.10Si23_03
Slope of LineSlope of Line
We have assumed so far that slope of line (m) lies between -1 and 1
When slope of line is greater than 1 in absolute value, then we increment y by 1 at each step, and x by:
d = (x2-x1)/(y2-y1)
0 1 2 3 4 5 6
0
12345
Exercise: calculate thepixels to draw line from(1,0) to (4,5)
5.11Si23_03
EfficiencyEfficiency
The DDA (Digital Differential Analyser)algorithm just described has the problem:– floating point operations (real numbers)
are expensive - add and round at each step
0 1 2 3 4 5 6
0
12345
Yet really all we need todecide is whether to movehorizontally or diagonally (for a line of slope < 1).
Can we do this in integerarithmetic?
5.12Si23_03
Bresenham’s AlgorithmBresenham’s Algorithm
One of the classic algorithms in computer graphics is the Bresenham line drawing algorithm
– Published in 1965– Still widely used– Excellent example
of an efficient algorithm Jack Bresenham
5.13Si23_03
Bresenham’s Algorithm - First Step
Bresenham’s Algorithm - First Step
Look in detail at first step:
0 1 2
We have choice of amove to (1,1) or (1,2)
Exact y-value at x=1 is:y* = 0.6 + 1 = 1.6
Since this is closer to 2than 1, we choose (1,2)
0
1
2d2
d1
Decision based on d1>d2, ie d1-d2>0
0.6
0.4
5.14Si23_03
Bresenham’s AlgorithmBresenham’s Algorithm
In general, suppose we are at (xk,yk) - slope of line, m, is between 0 and 1
Next move will be to (xk+1, yk) or (xk+1, yk+1)
d2
d1
xk xk+1
yk
yk+1
d1 = [m(xk + 1)+c] - yk d2 = yk + 1 - [m(xk + 1)+c]
d1 - d2 =2m(xk + 1) - 2yk + 2c -1
>0 indicates (xk+1, yk+1)<0 indicates (xk+1, yk)
5.15Si23_03
Bresenham’s AlgorithmBresenham’s Algorithm
d1 - d2 = 2m(xk + 1) - 2yk + 2c -1 : d1-d2 >0 diagonal
Let m = Dy/Dx and multiply both sides by Dx, settingDx(d1 – d2) = pk
pk = 2Dy xk - 2Dx yk + b (b a real constant)
Decision is still pk>0 - but can we work only in integers?
How does pk change at next step to pk+1? We knowthat xk increases by 1, and yk either increases by 1 orstays the same. So ….
5.16Si23_03
Bresenham’s AlgorithmBresenham’s Algorithm
pk = 2Dy xk - 2Dx yk + b
pk+1 = pk + 2Dy - 2Dx - if y increasespk+1 = pk + 2Dy - if y stays the same
Thus we have a decision strategy that only involves Integers, no multiplication and no rounding
.. we start things off withp0 = 2Dy - Dx
5.17Si23_03
Bresenham’s Algorithm - A Summary
Bresenham’s Algorithm - A Summary
To draw from A to B with slope 0<m<1 – let (x0,y0) = A and plot (x0,y0) – calculate Dx, Dy
– calculate p0 = 2Dy - Dx– for k = 0,1,2,.. until B reached:
If pk < 0 then plot (xk+1 , yk) and set
pk+1 = pk + 2 Dy
If pk > 0 then plot (xk+1, yk+1) and set
pk+1 = pk + 2 Dy - 2 Dx
5.18Si23_03
Bresenham’s Algorithm - Example
Bresenham’s Algorithm - Example
0 1 2 3 4 5 6
0
12345
–(x0,y0) = (0,1)
–Dx = 5, Dy = 3
- p1 = p0 + 2Dy -2Dx
= -3 <0
hence plot (2,2)
–p0 = 2Dy - Dx = 1 > 0
–hence plot (1,2)
And so on …
5.19Si23_03
Bresenham’s Algorithm - Other Slopes and Other
Shapes
Bresenham’s Algorithm - Other Slopes and Other
Shapes
There are straightforward variations for lines with slopes:
m > 1-1 < m < 0
m < -1 There are similar algorithms for
drawing circles and ellipses efficiently - see Hearn & Baker textbook
5.20Si23_03
Other Ways of Drawing Lines
Other Ways of Drawing Lines
Over past 38 years, researchers have strived to improve on Bresenham’s incremental algorithm
– for example, think how to parallelise the algorithm
Alternative structural approach comes from recognition that horizontal (0) and diagonal (1) moves come in runs
We had:– 10101
One of the moves occurs singly, and is evenly spread within the sequence - the other occurs in up to two lengths (consecutive numbers)
– 01001000100100010– ie 1 occurs singly, 0
occurs in twos and threes– decision is now which
length of run0 1 2 3 4 5 6
0
12345
5.21Si23_03
AliasingAliasing
Lines on raster displays suffer from aliasing effects - especially noticeable with lines near to horizontal
This is caused because we are `sampling’ the true straight line at a discrete set of positions.Called `aliasing’ because many straight lineswill have the same raster representation.
More noticeable on screen than laser printer - why?
5.22Si23_03
Supersampling – Bresenham on Finer Grid
Supersampling – Bresenham on Finer Grid
0 1 2
0
1
2
3
Each pixel isdivided into9 sub pixels.
Either 0,1,2 or 3sub-pixels can beset in any pixel.
This can be usedto set the intensitylevel of each pixel.
5.23Si23_03
SupersamplingSupersampling
0 1 2 3 4 5 6
0
12345
2 132
This shows the intensitylevels for the pixels inthe previous slide.
We are really usingintensity levels tocompensate for lackof resolution.
5.24Si23_03
Antialiased LineAntialiased Line
0 1 2 3 4 5 6
0
12345
The end result isa thicker line, with theintensity spread outover a larger area.
To the eye, the lineappears much sharperand straighter.
5.25Si23_03
d1 - d2 = 2m(xk + 1) - 2yk + 2c -1 : d1-d2 >0 diagonal
Let m = Dy/Dx and multiply both sides by Dx, settingDx(d1 – d2) = pk
pk = 2Dy xk - 2Dx yk + b (b a constant)
Decision is still pk>0 - but can we work only in integers?
pk+1 = 2Dy xk+1 - 2Dx yk+1 + b ... So
pk+1 = pk + 2Dy - 2Dx(yk+1 - yk) - where (yk+1 - yk) = 0 or 1
Thus we have a decision strategy that only involves integers.. we start things off with
p0 = 2Dy - Dx
Bresenham’s AlgorithmBresenham’s Algorithm