Lect2 scan convertinglines
Post on 19-May-2015
142 Views
Preview:
DESCRIPTION
Transcript
Sudipta.hit@gmail.com
Computer Graphics:Line Drawing Algorithms
Sudipta Mondal
2of32
Contents
Graphics hardwareThe problem of scan conversionConsiderationsLine equationsScan converting algorithms
– A very simple solution– The DDA algorithm
Conclusion
3of32
Graphics Hardware
It’s worth taking a little look at how graphicshardware works before we go any furtherHow do things end up on the screen?
Imag
es ta
ken
from
Hea
rn &
Bak
er, “
Com
pute
r Gra
phic
s C
ver
sion
4of32
Architecture Of A Graphics System
System Bus
CPU DisplayProcessor
SystemMemory
DisplayProcessorMemory
FrameBuffer
VideoController MonitorMonitor
5of32
Output Devices
There are a range of output devicescurrently available:
– Printers/plotters– Cathode ray tube displays– Plasma displays– LCD displays– 3 dimensional viewers– Virtual/augmented reality headsets
We will look briefly at some of the morecommon display devices
6of32
Basic Cathode Ray Tube (CRT)
Fire an electron beam at a phosphor coatedscreen
Imag
es ta
ken
from
Hea
rn &
Bak
er, “
Com
pute
r Gra
phic
s C
ver
sion
7of32
Raster Scan Systems
Draw one line at a time
Imag
es ta
ken
from
Hea
rn &
Bak
er, “
Com
pute
r Gra
phic
s C
ver
sion
8of32
Colour CRT
An electron gun for each colour – red, greenand blue
Imag
es ta
ken
from
Hea
rn &
Bak
er, “
Com
pute
r Gra
phic
s C
ver
sion
9of32
Applying voltages tocrossing pairs ofconductors causesthe gas (usually amixture includingneon) to breakdown into a glowingplasma of electronsand ions
Plasma-Panel DisplaysIm
ages
take
n fro
m H
earn
& B
aker
, “C
ompu
ter G
raph
ics
C v
ersi
on
10of32
Liquid Crystal Displays
Light passingthrough the liquidcrystal is twistedso it gets throughthe polarizerA voltage isapplied using thecrisscrossingconductors to stopthe twisting andturn pixels offIm
ages
take
n fro
m H
earn
& B
aker
, “C
ompu
ter G
raph
ics
C v
ersi
on
11of32
The Problem Of Scan Conversion
A line segment in a scene is defined by thecoordinate positions of the line end-points
x
y
(2, 2)
(7, 5)
12of32
The Problem (cont…)
But what happens when we try to draw thison a pixel based display?
How do we choose which pixels to turn on?
13of32
Considerations
Considerations to keep in mind:– The line has to look good
• Avoid jaggies– It has to be lightening fast!
• How many lines need to be drawn in a typicalscene?
• This is going to come back to bite us again andagain
14of32
Line Equations
Let’s quickly review the equations involvedin drawing lines
x
y
y0
yend
xendx0
Slope-intercept lineequation:
bxmy +×=where:
0
0
xxyym
end
end
--
=
00 xmyb ×-=
15of32
Lines & Slopes
The slope of a line (m) is defined by its startand end coordinatesThe diagram below shows some examplesof lines and their slopes
m = 0
m = -1/3
m = -1/2
m = -1
m = -2m = -4
m = ∞
m = 1/3
m = 1/2
m = 1
m = 2m = 4
m = 0
16of32
A Very Simple Solution
We could simply work out the correspondingy coordinate for each unit x coordinateLet’s consider the following example:
x
y
(2, 2)
(7, 5)
2 7
2
5
17of32
A Very Simple Solution (cont…)
1
2
3
4
5
0
1 2 3 4 5 60 7
18of32
A Very Simple Solution (cont…)
x
y
(2, 2)
(7, 5)
2 3 4 5 6 7
2
5
53
2725=
--
=m
542
532 =*-=b
First work out m and b:
Now for each x value work out the y value:
532
543
53)3( =+×=y
513
544
53)4( =+×=y
543
545
53)5( =+×=y 5
24546
53)6( =+×=y
19of32
A Very Simple Solution (cont…)
Now just round off the results and turn onthese pixels to draw our line
3532)3( »=y
3513)4( »=y
4543)5( »=y
4524)6( »=y
0 1 2 3 4 5 6 7 8
0
1
23
4
56
7
20of32
A Very Simple Solution (cont…)
However, this approach is just way too slowIn particular look out for:
– The equation y = mx + b requires themultiplication of m by x
– Rounding off the resulting y coordinatesWe need a faster solution
21of32
A Quick Note About Slopes
In the previous example we chose to solvethe parametric line equation to give us the ycoordinate for each unit x coordinateWhat if we had done it the other wayaround?So this gives us:
where: and
mbyx -
=
0
0
xxyym
end
end
--
= 00 xmyb ×-=
22of32
A Quick Note About Slopes (cont…)
Leaving out the details this gives us:
We can see easily thatthis line doesn’t lookvery good!We choose which wayto work out the linepixels based on theslope of the line
0 1 2 3 4 5 6 7 8
0
1
23
4
56
7
4323)3( »=x 5
315)4( »=x
23of32
A Quick Note About Slopes (cont…)
If the slope of a line is between -1 and 1 thenwe work out the y coordinates for a line basedon it’s unit x coordinatesOtherwise we do the opposite – x coordinatesare computed based on unit y coordinates
m = 0
m = -1/3m = -1/2
m = -1
m = -2m = -4
m = ∞
m = 1/3m = 1/2
m = 1
m = 2m = 4
m = 0
24of32
A Quick Note About Slopes (cont…)
1
2
3
4
5
0
1 2 3 4 5 60 7
25of32
The DDA Algorithm
The digital differentialanalyzer (DDA) algorithmtakes an incrementalapproach in order tospeed up scan conversionSimply calculate yk+1based on yk
The or ig ina l d i f fe ren t ia lanalyzer was a physicalm a c h i n e d e ve l o p e d b yVannevar Bush at MIT in the1930’s in order to solveordinary differential equations.
26of32
The DDA Algorithm (cont…)
Consider the list of points that wedetermined for the line in our previousexample:
(2, 2), (3, 23/5), (4, 31/5), (5, 34/5), (6, 42/5), (7, 5)Notice that as the x coordinates go up byone, the y coordinates simply go up by theslope of the lineThis is the key insight in the DDA algorithm
27of32
The DDA Algorithm (cont…)
When the slope of the line is between -1 and 1begin at the first point in the line and, byincrementing the x coordinate by 1, calculatethe corresponding y coordinates as follows:
When the slope is outside these limits,increment the y coordinate by 1 and calculatethe corresponding x coordinates as follows:
myy kk +=+1
mxx kk
11 +=+
28of32
The DDA Algorithm (cont…)
Again the values calculated by the equationsused by the DDA algorithm must be roundedto match pixel values
(xk, yk) (xk+1, yk+m)
(xk, round(yk))
(xk+1, round(yk+m))
(xk, yk) (xk+ 1/m, yk+1)
(round(xk), yk)
(round(xk+ 1/m), yk+1)
29of32
DDA Algorithm Example
Let’s try out the following examples:
x
y
(2, 2)
(7, 5)
2 7
2
5
x
y (2, 7)
(3, 2)
2 3
2
7
30of32
DDA Algorithm Example (cont…)
7
2
3
4
5
6
1 2 3 4 5 60 7
31of32
The DDA Algorithm Summary
The DDA algorithm is much faster than ourprevious attempt
– In particular, there are no longer anymultiplications involved
However, there are still two big issues:– Accumulation of round-off errors can make
the pixelated line drift away from what wasintended
– The rounding operations and floating pointarithmetic involved are time consuming
32of32
Conclusion
In this lecture we took a very brief look athow graphics hardware worksDrawing lines to pixel based displays is timeconsuming so we need good ways to do itThe DDA algorithm is pretty good – but wecan do betterNext time we’ll like at the Bresenham linealgorithm and how to draw circles, fillpolygons and anti-aliasing
top related