Title: SI23 Introduction to Computer Graphics
1SI23Introduction to Computer Graphics
2Course Outline
- Vector graphics
- Line drawing
- Area filling
- Graphical interaction
SVG Viewer
graphics algorithms
3Line 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
4Line 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)
Where to draw the line? ?
5Line Drawing - By Hand
- In a simple example, one can do by eye
... but we would like an algorithm!
6Equation 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)
Then m (4-1)/(5-0) ie 0.6 and c 1 Thus y
0.6 x 1
7Drawing 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
x 0 -gt y 1 y 1 x 1 -gt y 1.6 y 2 x
2 -gt y 2.2 y 2 x 3 -gt y 2.8 y 3 x 4
-gt y 3.4 y 3
8The Line Drawn
This gives us (after 1 multiplication, 1
addition, and 1 rounding operation per step)
Calculate y for x 0,1,2,3,4,5 and round to
nearest integer
y 0.6 x 1
x 0 -gt y 1 y 1 x 1 -gt y 1.6 y 2 x
2 -gt y 2.2 y 2 x 3 -gt y 2.8 y 3 x 4
-gt y 3.4 y 3
How do we make more efficient?
9DDA Algorithm
- We can do this more efficiently by simply
incrementing y at each stage - y y q where q (y2-y1)/(x2-x1)
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
10Slope 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)
Exercise calculate the pixels to draw line
from (1,0) to (4,5)
11Efficiency
- The DDA (Digital Differential Analyser)algorithm
just described has the problem - floating point operations (real numbers) are
expensive - add and round at each step
Yet really all we need to decide is whether to
move horizontally or diagonally (for a line of
slope lt 1). Can we do this in integer arithmetic?
12Bresenhams 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
13Bresenhams Algorithm - First Step
- Look in detail at first step
We have choice of a move to (1,1) or (1,2) Exact
y-value at x1 is y 0.6 1 1.6 Since
this is closer to 2 than 1, we choose (1,2)
2
d2
d1
1
0
0
1
2
14Bresenhams Algorithm
- In general, suppose we are at (xk,yk) - slope of
line, m, is between 0 and 1 - Next move will be to (xk1, yk) or (xk1, yk1)
d1 m(xk 1)c - yk d2 yk 1 - m(xk
1)c d1 - d2 2m(xk 1) - 2yk 2c -1 gt0
indicates (xk1, yk1) lt0 indicates (xk1, yk)
yk1
d2
d1
yk
xk
xk1
15Bresenhams Algorithm
d1 - d2 2m(xk 1) - 2yk 2c -1 d1-d2 gt0
diagonal Let m Dy/Dx and multiply both sides
by Dx, setting Dx(d1 d2) pk pk 2Dy xk -
2Dx yk b (b a real constant) Decision is still
pkgt0 - but can we work only in integers? How
does pk change at next step to pk1? We
know that xk increases by 1, and yk either
increases by 1 or stays the same. So .
16Bresenhams Algorithm
pk 2Dy xk - 2Dx yk b pk1 pk 2Dy - 2Dx
- if y increases pk1 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 with p0 2Dy - Dx
17Bresenhams Algorithm - A Summary
- To draw from A to B with slope 0ltmlt1
- 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 lt 0 then plot (xk1 , yk) and set
- pk1 pk 2 Dy
- If pk gt 0 then plot (xk1, yk1) and set
- pk1 pk 2 Dy - 2 Dx
18Bresenhams Algorithm - Example
- p0 2Dy - Dx 1 gt 0
- hence plot (1,2)
- p1 p0 2Dy -2Dx -3 lt0 hence plot (2,2)
And so on
19Bresenhams Algorithm - Other Slopes and Other
Shapes
- There are straightforward variations for lines
with slopes - m gt 1
- -1 lt m lt 0
- m lt -1
- There are similar algorithms for drawing circles
and ellipses efficiently - see Hearn Baker
textbook
20Other Ways of Drawing Lines
- Over past 38 years, researchers have strived to
improve on Bresenhams 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 run
21Aliasing
- 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.
More noticeable on screen than laser printer -
why?
22Supersampling Bresenham on Finer Grid
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.
2
1
0
0
1
2
3
23Supersampling
This shows the intensity levels for the pixels
in the previous slide. We are really
using intensity levels to compensate for lack of
resolution.
3
2
2
1
24Antialiased Line
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.
25Bresenhams Algorithm
d1 - d2 2m(xk 1) - 2yk 2c -1 d1-d2 gt0
diagonal Let m Dy/Dx and multiply both sides
by Dx, setting Dx(d1 d2) pk pk 2Dy xk -
2Dx yk b (b a constant) Decision is still pkgt0
- but can we work only in integers? pk1 2Dy
xk1 - 2Dx yk1 b ... So pk1 pk 2Dy -
2Dx(yk1 - yk) - where (yk1 - yk) 0 or 1 Thus
we have a decision strategy that only involves
integers.. we start things off with p0 2Dy -
Dx