Transcript SI23 Introduction to Computer Graphics Lecture 5 – Drawing A Line
Si23_03
SI23 Introduction to Computer Graphics
Lecture 5 – Drawing A Line 5.1
Course Outline
Vector graphics
– –
Line drawing Area filling Graphical interaction Si23_03 URL 2D vector graphics SVG Viewer lines., areas graphics algorithms interaction colour Image Display GIMP URL 5.2
Si23_03
Line 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.3
Line Drawing - The Problem
Si23_03
To draw a line from one point to another on a display - which pixels should be illuminated?
5 4 3 2 1 0 0 1 2 3 4 5 6 Suppose pt1 = (0,1) and pt2 = (5,4)
Where to draw the line?
5.4
Si23_03
Line Drawing - By Hand
In a simple example, one can do by eye
5 4 3 2 1 0 0 1 2 3 4 5 6 ... but we would like an algorithm!
5.5
Equation of a Line
Line equation is: y = m x + c If line joins (x 1 ,y 1 ) to (x 2 ,y 2 ), then m = (y 2 -y 1 )/(x 2 -x 1 ) and c = y 1 - m x 1 Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6 Suppose pt1 = (0,1) and pt2 = (5,4) Then m = (4-1)/(5-0) ie 0.6
and c = 1 Thus: y = 0.6 x + 1
5.6
Drawing a Line From Its Equation
We can use the equation to draw the pixels
y = 0.6 x + 1 Calculate y for x = 0,1,2,3,4,5 and round to nearest integer
Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6 x = 0 -> y =1 x = 1 -> y = 1.6
x = 2 -> y = 2.2
x = 3 -> y = 2.8
x = 4 -> y = 3.4
y =1 y = 2 y = 2 y = 3 y = 3
5.7
The Line Drawn
This gives us (after 1 multiplication, 1 addition, and 1 rounding operation per step): y = 0.6 x + 1
Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6 Calculate y for x = 0,1,2,3,4,5 and round to nearest integer x = 0 -> y =1 x = 1 -> y = 1.6
x = 2 -> y = 2.2
x = 3 -> y = 2.8
x = 4 -> y = 3.4
y =1 y = 2 y = 2 y = 3 y = 3
How do we make more efficient?
5.8
DDA Algorithm
We can do this more efficiently by simply incrementing y at each stage y* = y + q where q = (y 2 -y 1 )/(x 2 -x 1 ) Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6 Here q = 0.6
y = 1.0
y = 1 y = 1.0 + 0.6 = 1.6 y = 2 y = 1.6 + 0.6 = 2.2 y = 2 y = 2.2 + 0.6 = 2.8 y = 3 y = 2.8 + 0.6 = 3.4 y = 3 One addition, one rounding
5.9
Slope of Line
Si23_03
5 4 3 2 1 0
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 = (x 2 -x 1 )/(y 2 -y 1 )
Exercise: calculate the pixels to draw line from (1,0) to (4,5) 0 1 2 3 4 5 6
5.10
Efficiency
The DDA (Digital Differential Analyser)algorithm just described has the problem:
–
floating point operations (real numbers) are expensive - add and round at each step Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6 Yet really all we need to decide is whether to move horizontally or diagonally (for a line of slope < 1).
Can we do this in integer arithmetic?
5.11
Si23_03
Bresenham’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.12
Bresenham’s Algorithm - First Step
2 1 0
Si23_03
0
Look in detail at first step:
We have choice of a move to (1,1) or (1,2) d2 d1
0.4
0.6
Exact y-value at x=1 is: y* = 0.6 + 1 = 1.6
Since this is closer to 2 than 1, we choose (1,2) 1 2
Decision based on d1>d2, ie d1-d2>0 5.13
Bresenham’s Algorithm
y k +1 y k
In general, suppose we are at (x k ,y k ) slope of line, m, is between 0 and 1 Next move will be to (x k +1, y k ) or (x k +1, y k +1)
d1 = [m(x k + 1)+c] - y k d2 = y k + 1 - [m(x k + 1)+c] d2 d1 d1 - d2 = 2m(x k + 1) - 2y k + 2c -1
Si23_03
x k x k +1 >0 indicates (x k +1, y k +1) <0 indicates (x k +1, y k )
5.14
Bresenham’s Algorithm
d1 - d2 = 2m(x k + 1) - 2y k + 2c -1 : d1-d2 >0 diagonal Let m = Dy/Dx and multiply both sides by Dx, setting Dx(d1 – d2) = p k
p k = 2Dy x k - 2Dx y k + b (b a real constant)
Decision is still p k >0 - but can we work only in integers?
How does p that x k k change at next step to p k+1 ? We know increases by 1, and y stays the same. So ….
k either increases by 1 or
Si23_03 5.15
Bresenham’s Algorithm
p k = 2Dy x k - 2Dx y k + b p k+1 p k+1 = p k = p k + 2Dy - 2Dx if y increases + 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 with p 0 = 2Dy - Dx
Si23_03 5.16
Bresenham’s Algorithm - A Summary
Si23_03
To draw from A to B with slope 0
– – – –
let (x 0 ,y 0 ) = A and plot (x calculate Dx, Dy calculate p 0 = 2Dy - Dx 0 ,y 0 ) for k = 0,1,2,.. until B reached: If p k < 0 then plot (x k+1 , y k ) and set p k+1 = p k + 2 Dy If pk > 0 then plot (x k+1 , y k+1 ) and set p k+1 = p k + 2 Dy - 2 Dx 5.17
Bresenham’s Algorithm Example
Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6 – (x 0 ,y 0 ) = (0,1) – Dx = 5, Dy = 3 – p 0 = 2Dy - Dx = 1 > 0 – hence plot (1,2) - p 1 = p 0 + 2Dy -2Dx = -3 <0 hence plot (2,2) And so on …
5.18
Si23_03
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.19
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 Si23_03
5 4 3 2 1 0 0 1 2 3 4 5 6
Alternative come in structural approach comes from recognition that horizontal (0) and diagonal (1) moves 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 run 5.20
Aliasing
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 lines will have the same raster representation.
Si23_03
More noticeable on screen than laser printer - why?
5.21
Si23_03
2 1 0
Supersampling – Bresenham on Finer Grid
0 1 2 3 Each pixel is divided into 9 sub pixels.
Either 0,1,2 or 3 sub-pixels can be set in any pixel.
This can be used to set the intensity level of each pixel.
5.22
Si23_03
Supersampling
5 4 3 2 1 0 2 2 1 3 0 1 2 3 4 5 6 This shows the intensity levels for the pixels in the previous slide.
We are really using intensity levels to compensate for lack of resolution.
5.23
Si23_03
Antialiased Line
5 4 3 2 1 0 0 1 2 3 4 5 6 The end result is a thicker line, with the intensity spread out over a larger area.
To the eye, the line appears much sharper and straighter.
5.24
Bresenham’s Algorithm
d1 - d2 = 2m(x k + 1) - 2y k + 2c -1 : d1-d2 >0 diagonal Let m = Dy/Dx and multiply both sides by Dx, setting Dx(d1 – d2) = p k
p k = 2Dy x k - 2Dx y k + b (b a constant)
Decision is still p k >0 - but can we work only in integers?
p k+1 = 2Dy x k+1 - 2Dx y k+1 + b
... So
p k+1 = p k + 2Dy - 2Dx(y k+1 - y k ) - where (y k+1 - y k ) = 0 or 1
Thus we have a decision strategy that only involves
5.25
p 0 = 2Dy - Dx