Basic Raster Graphics Algorithms for Drawing 2 D
Basic Raster Graphics Algorithms for Drawing 2 D Primitives Prof. Lizhuang Ma Shanghai Jiao Tong University
Contents • • Architecture of a Raster Display Scan Converting Lines Filling Rectangles Filling Polygons Clipping Lines Clipping Polygons Antialiasing
Architecture of a Raster Display Video
Definitions • • • Pixel: a screen consists of N x M pixels Bilevel = monochrome, 1 bit / pixel Color: RGB/CYK/LUV… Bitmap / pixmap Frame buffer: an array of data in memory mapped to screen
Scan Converting Line • A scan-converted line showing intensified pixels as black circles
The Basic Incremental Algorithm • A scan-converted line showing intensified pixels as black circles
The Basic Incremental Algorithm void Line (intx 0, inty 0, intx 1, inty 1, value) { Int x; Float dy, dx, y, m; dy=y 1 -y 0; dx=x 1 -x 0; m=dy/dx; y=y 0; for(x=x 0; x<=x 1; x++) { Write. Pixel(x, (int)floor(y+0. 5), value); y+=m; } }
Midpoint Line Algorithm
Midpoint Line Algorithm
Midpoint Line Algorithm void Midpoint. Line(intx 0, inty 0, intx 1, inty 1, value) { int dx, dy, incr. E, incr. NE, d, x, y; dy=y 1 -y 0; dx=x 1 -x 0; d=dy*2 -dx; incr. E=dy*2; incr. NE=(dy-dx)*2; x=x 0; y=y 0; Write. Pixel(x, y, value); while(x<x 1) { if(d<=0) {d+=incr. E; x++; } else {d+=incr. NE; x++; y++; } Write. Pixel(x, y, value); } }
Filling Rectangles for(y from ymin to ymax of the rectangle) { for(x from xmin to xmax) { Write. Pixel(x, y, value); } }
Filling Polygons
Filling Polygons
Filling Polygons 1. Find the intersections of the scan line with all edges of the polygon 2. Sort the intersections by increasing x coordinate 3. Fill in all pixels between pairs of intersections that lie interior to the polygon
Special Cases Horizontal Edges Slivers
Clipping Lines
The Cohen-Sutherland Algorithm
The Cohen-Sutherland Algorithm
Clipping Polygons
The Sutherland-Hodgman Polygon-Clipping Algorithm
Liang-Barsky Algorithm • Starting point is parametric form • Compute four intersections with extended clipping rectangle • Will see that this can be avoided
Ordering of intersection points • Order the intersection points • Figure (a): 1 > a 4 > a 3 > a 2 > a 1 > 0 • Figure (b): 1 > a 4 > a 2 > a 3 > a 1 > 0
Liang-Barsky Efficiency Improvements • Efficiency improvement 1: – Compute intersections one by one – Often can reject before all four are computed • Efficiency improvement 2: – Equations for a 3, a 2 – Compare a 3, a 2 without floating-point division
Antialiasing
- Slides: 25