Drawing Lines - PowerPoint PPT Presentation

About This Presentation
Title:

Drawing Lines

Description:

It is an 'iterative' algorithm: each step is ... Bucket-Sort. 0. 1. 2. 3. 4. 5. 6. 7. 8. 10. 11. 12. 8. 8. 7. 9. 6. 10. 11. 12. 13. 14. 15. 16. 5. 7. 9. 11. 13 ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 21
Provided by: ProfessorA2
Learn more at: https://www.cs.usfca.edu
Category:
Tags: bucket | drawing | lines

less

Transcript and Presenter's Notes

Title: Drawing Lines


1
Drawing Lines
  • The Bresenham Algorithm for drawing lines and
    filling polygons

2
Plotting a line-segment
  • Bresenham published algorithm in 1965
  • It was originally to be used with a plotter
  • It adapts well to raster scan conversion
  • It uses only integer arithmetic operations
  • It is an iterative algorithm each step is
    based on results from the previous step
  • The sign of an error term governs the choice
    among two alternative actions

3
Scan conversion
The actual line is comprised of points drawn from
a continuum, but it must be approximated using
pixels from a discrete grid.
4
The various cases
  • Horizontal or vertical lines are easy cases
  • Lines that have slope 1 or -1 are easy, too
  • Symmetries leave us one remaining case 0 lt
    slope lt 1
  • As x-coodinate is incremented, there are just two
    possibilities for the y-coordinate (1)
    y-coordinate is increased by one or (2)
    y-coordinate remains unchanged

5
0 lt slope lt 1
Y-axis
X-axis
y increases by 1
y does not change
6
Integer endpoints
?Y Y1 Y0 ?X X1 X0
(X1,Y1)
0 lt ?Y lt ?X
?Y
(X0,Y0)
?X
slope ?Y/?X
7
Which point is closer?
y mx b
A
yi -11
yi -1
B
ideal line
xi -1
xi
error(A) (yi -1 1) y error(B) y - (yi
-1)
8
The Decision Variable
  • Choose B if and only if error(B)lterror(A)
  • Or equivalently error(B) error(A) lt 0
  • Formula error(B) error(A) 2m(xi x0)
    2(y0 yi -1) -1
  • Remember m ?y/?x (slope of line)
  • Multiply through by ?x (to avoid fractions)
  • Let di ?x( error(B) error(A) )
  • Rule is choose B if and only if di lt 0

9
Computing di1 from di
  • di1 2(?y)(xi1 x0) 2(?x)(y0 yi) ?x
  • di 2(?y)(xi x0) 2(?x)(y0 yi-1) ?x
  • The difference can be expressed as
  • di1 di 2(?y)(xi1 xi) 2(?y)(yi yi-1)
  • Recognize that xi1 xi 1 at every step
  • And also yi yi-1 will be either 0 or 1
    (depending on the sign of the previous d)

10
How does algorithm start?
  • At the outset we start from point (x0,y0)
  • Thus, at step i 1, our formula for di is
    d1 2(?y) - ?x
  • And, at each step thereafter if ( d i lt 0 )
    di1 di 2(?y) yi1 yi else di1 di
    2(?y-?x) yi1 yi 1 xi1 xi 1

11
bresdemo.cpp
  • The example-program is on class website
  • http//nexus.cs.usfca.edu/cruse/cs686/
  • It draws line-segments with various slopes
  • The Michener algorithm (for a circle-fill) is
    also included, for comparative purposes
  • Extreme slopes (close to zero or infinity) are
    not displayed in this demo program
  • They can be added by you as an exercise

12
Filling a triangle or polygon
  • The Bresenhams method can be adapted
  • But an efficient data-structure is needed
  • All the sides need to be handled together
  • We let the y-coordinate steadily increment
  • For sides which are nearly horizontal the
    x-coordinates can change by more than 1

13
Triangle Illustration
14
Non-Convex Polygons
15
Bucket-Sort
Y
0
XLO
XHI
1
2
8
8
3
7
9
4
6
10
5
11
5
6
12
7
7
13
9
8
14
11
10
15
13
11
16
15
12
17
17
13
16
Handling Corners
17
Legacy Software
  • We have a polygon-fill demo polyfill.cpp
  • You can find it now on our class website
  • But it was written in 1997 for MS-DOS
  • Wed like to run it on our Linux system
  • But it will need a number of modifications
  • This is our first programming assignment to
    adapt this obsolete application so it can
    execute on a contemporary platform

18
What are some issues?
  • GNU compiler enforces stricter standards
  • Sizes of some data-types are now larger
  • Physical VRAM now must be mapped
  • I/O instructions now require permissions
  • Real-Mode addresses must be converted
  • Not all the header-files are still supported
  • Interface to CPU registers is a bit different

19
Converting addresses
  • TURBO-C allowed use of far pointers
  • Use a macro to create a pointer to VRAM
  • uchar vram MK_FP( 0xA000, 0x0000 )
  • Real-mode address has two components (16-bit
    segment and 16-bit offset)
  • For Linux we convert to a 32-bit address
  • uchar vram (uchar)0x000A0000
  • Formula address 16segment offset

20
Hardware Issues?
  • Pentium CPUs are backward compatible
  • BIOS firmware can use Virtual-8086 mode
  • SVGA still supports older graphics modes
  • The keyboards mechanism is unchanged
  • Feasibility test if we will boot MS-DOS, we
    can easily run the polyfill application
  • So its just a software problem to give our
    MS-DOS program a new life under Linux!
Write a Comment
User Comments (0)
About PowerShow.com