Top Banner
[email protected] Computer Graphics: Line Drawing Algorithms Sudipta Mondal
32

Lect2 scan convertinglines

May 19, 2015

Download

Documents

BCET

created by sudipta mandal
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lect2 scan convertinglines

[email protected]

Computer Graphics:Line Drawing Algorithms

Sudipta Mondal

Page 2: Lect2 scan convertinglines

2of32

Contents

Graphics hardwareThe problem of scan conversionConsiderationsLine equationsScan converting algorithms

– A very simple solution– The DDA algorithm

Conclusion

Page 3: Lect2 scan convertinglines

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

Page 4: Lect2 scan convertinglines

4of32

Architecture Of A Graphics System

System Bus

CPU DisplayProcessor

SystemMemory

DisplayProcessorMemory

FrameBuffer

VideoController MonitorMonitor

Page 5: Lect2 scan convertinglines

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

Page 6: Lect2 scan convertinglines

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

Page 7: Lect2 scan convertinglines

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

Page 8: Lect2 scan convertinglines

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

Page 9: Lect2 scan convertinglines

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

Page 10: Lect2 scan convertinglines

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

Page 11: Lect2 scan convertinglines

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)

Page 12: Lect2 scan convertinglines

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?

Page 13: Lect2 scan convertinglines

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

Page 14: Lect2 scan convertinglines

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 ×-=

Page 15: Lect2 scan convertinglines

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

Page 16: Lect2 scan convertinglines

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

Page 17: Lect2 scan convertinglines

17of32

A Very Simple Solution (cont…)

1

2

3

4

5

0

1 2 3 4 5 60 7

Page 18: Lect2 scan convertinglines

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

Page 19: Lect2 scan convertinglines

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

Page 20: Lect2 scan convertinglines

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

Page 21: Lect2 scan convertinglines

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 ×-=

Page 22: Lect2 scan convertinglines

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

Page 23: Lect2 scan convertinglines

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

Page 24: Lect2 scan convertinglines

24of32

A Quick Note About Slopes (cont…)

1

2

3

4

5

0

1 2 3 4 5 60 7

Page 25: Lect2 scan convertinglines

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.

Page 26: Lect2 scan convertinglines

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

Page 27: Lect2 scan convertinglines

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 +=+

Page 28: Lect2 scan convertinglines

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)

Page 29: Lect2 scan convertinglines

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

Page 30: Lect2 scan convertinglines

30of32

DDA Algorithm Example (cont…)

7

2

3

4

5

6

1 2 3 4 5 60 7

Page 31: Lect2 scan convertinglines

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

Page 32: Lect2 scan convertinglines

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