Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Module U08181: Computer Graphics graphics primitives part 1: scan conversion and filling.

Similar presentations


Presentation on theme: "1 Module U08181: Computer Graphics graphics primitives part 1: scan conversion and filling."— Presentation transcript:

1 1 Module U08181: Computer Graphics graphics primitives part 1: scan conversion and filling

2 2 Raster graphics vector graphics systems draw lines -- will be discussed later A raster (bitmapped) graphics display is a ‘grid’ of picture elements -- pixels, dots

3 3 Raster display: an array of pixels 0 0 screenHeight screenWidth

4 4 Cartesian coordinates René Descartes y x Note: x is always from left to right some systems use y top to bottom

5 5 Scan conversion cartesian coordinates: real, continuous raster positions: integer, discrete scan conversion: ‘rasterisation’, ‘discretisation’

6 6 Raster access PROCEDURE SetPixel (x, y: INTEGER; colour: INTEGER); (* pre: (x, y) is on screen post: pixel at (x, y) now in colour colour *) FUNCTION GetPixel (x, y: INTEGER): INTEGER; (* pre: (x, y) is on screen post: GetPixel (x, y) is colour of pixel at (x, y) *)

7 7 How to set pixels to: draw lines draw curves: circles... display characters filled shapes

8 8 Simple lines PROCEDURE HorizontalLine (x, y, n: INTEGER); (* pre: all of line will lie on screen post: line of n units drawn from point (x, y) *) VAR i: INTEGER; BEGIN FOR i := 0 TO n - 1 DO SetPixel(x+ i, y, black) END END HorizontalLine;

9 9 Other lines: arbitrary slope - naive approach PROCEDURE Line (x1, y1, x2, y2: INTEGER); VAR x, dx, dy: INTEGER; y: REAL; BEGIN dx := x2 - x1; dy := y2 - y1; FOR x := x1 TO x2 DO y := (dy / dx) * (x - x1) + y1; SetPixel(x, ROUND(y), black) END END Line;

10 10 Problems with naive approach: uses real (float) arithmetic including division -- slow uses ROUND -- slow

11 11 Better method: Bresenham’s algorithm Jack Bresenham

12 12 Bresenham’s algorithm: e measures the error between true position of point and nearest pixel

13 13 Bresenham’s algorithm: e measures the error between true position of point and nearest pixel simple version first: note precondition PROCEDURE Line (x1, y1, x2, y2: INTEGER); (* pre: dx > 0 & dy > 0 & dy/dx < 1 post: line drawn from (x1, y1) to (x2, y2) *) VAR dx, dy, e, x, y: INTEGER; BEGIN dx := x2 - x1; dy := y2 - y1; y := 0; e := dx - 2*dy;

14 14 continued FOR x := 0 TO dx DO SetPixel(x1+ x, y1 + y, black); IF e < 0 THEN y := y + 1; e := e + 2*(dx - dy) ELSE e := e - 2*dy END END Line;

15 15 Advantages of Bresenham’s algorithm uses only integer arithmetic - quick multiplications only by 2 - quick, program as left shifts or leave to a (good) compiler to do this. symmetry allows easy extension to slopes where dx > dy

16 16 Circle: algorithm by B. Weibel Note symmetry - we only need to draw one octant, then reflect, q is error measure PROCEDURE Circle (x1, y1, r: INTEGER); VAR x, y, q: INTEGER; BEGIN x := 0; y := r; q := 4*r; WHILE x < y DO (* set current pixel in each octant *)

17 17 circle: algorithm by B. Weibel SetPixel(x1 + x, y1 + y, black); SetPixel(x1 + x, y1 - y, black); SetPixel(x1 - x, y1 + y, black); SetPixel(x1 - x, y1 - y, black); SetPixel(x1 + y, y1 + x, black); SetPixel(x1 + y, y1 - x, black); SetPixel(x1 -y, y1 + x, black); SetPixel(x1 - y, y1 - x, black); (x1, y1) x y

18 18 circle continued x := x + 1; q := q - (8*x - 4); IF q < 1 THEN y := y - 1; q := q + 8*y END END; IF x = y THEN SetPixel(x1 + x, y1 + y, black); SetPixel(x1 + x, y1 - y, black); SetPixel(x1 - x, y1 + y, black); SetPixel(x1 - x, y1 - y, black); END END Circle;

19 19 Other curves More general curves can be obtained by: B-spline Bézier curves: developed for the UNISURF system by P. Bézier and used at Régie Renault for design of car bodies

20 20 Displaying characters: mask raster origin 0 xminxmax ymax ymin

21 21 Italics and bold Italics and bold can be created by algorithmic approach Typographically unsophisticated

22 22 WriteChar PROCEDURE WriteChar (chMask: MaskRaster; x, y: INTEGER); VAR i, j: INTEGER; BEGIN FOR j := chMask.xmin TO chMask.xmax - 1 DO FOR i := chMask.ymin TO chMask.ymax - 1 DO IF GetPixel(chMask, i, j) <> 0 THEN SetPixel(x + i, j + y, black) END END WriteChar;

23 23 Solid areas: Rectangle - easy PROCEDURE FilledRectangle (x, y, w, h: INTEGER); VAR i, j: INTEGER; BEGIN FOR j := y TO y + h - 1 DO FOR i := x TO x + w - 1 DO SetPixel(i, j, black) END END FilledRectangle;

24 24 ‘Paintpot’ fill: Pick a point (x, y) within the shape, replace all pixels adjacent of old colour by new colour PROCEDURE Fill (x, y, oldCol, newCol: INTEGER); VAR i, j: INTEGER; BEGIN IF GetPixel(x, y) = oldCol THEN SetPixel(x, y, newCol); FOR i := -1 TO 1 DO FOR j := -1 TO 1 DO Fill(x + i, j + y, oldCol, newCol) END END Fill;

25 25 Polygon filling y x crossed odd number of boundaries  inside shape

26 26 Polygon filling generate all the points (x, y) on periphery sort points first by y, then by x each pair of points(x a, y), (x b, y) is a region inside the polygon; for each such pair fill pixels from (x a, y) to (x b, y) inclusive can use dynamic data structure for this.

27 27 References The original paper: Bresenham, J. E.: ‘Algorithm for Computer Control of a Digital Plotter,’ IBM Sys. J. 4(1):25-30, 1965 Bresenham’s home page: http://www.udayton.edu/~cps/cps560/notes/ Lines/bresnham.htm

28 28 References continued An applet that demonstrates Bresenham’s algorithm (fun): http://www.cs.rit.edu/~yxy7691/Bookmark.htm l Explanation and derivation: http://www.cs.helsinki.fi/group/goa/mallinnus/li nes/bresenh.html Explanation and derivation: http://www.cs.unc.edu/~hoff/projects/comp23 5/ bresline/bresen.html

29 29 References continued Circle algorithm: by B. Weibel, in ‘System Programming in Modula-2: Mouse and Bitmap Display’ (second edition) J. Gutknecht, Institut für Informatik, ETH Zürich, Sept 1983, p44 Bézier P. :’Mathematical and Practical Possibilities of UNISURF’, in R.E. Barnhill and R.F. Riesenfeld (eds) Computer Aided Geometric Design, Academic, New York, 1974

30 30 Next lecture: clipping, transformations Exercises –see separate exercise sheet


Download ppt "1 Module U08181: Computer Graphics graphics primitives part 1: scan conversion and filling."

Similar presentations


Ads by Google